1

i have written this code for a question on codechef (A4)....when i give the input: 2 4 2

This program stops unexpectedly without taking further input ....can some please point out the mistake in the code?

#include <stdio.h>
#include<math.h>
void number(long int a,int b)
{
    int c;
    c=b;
    int first[c],last[c],e=1,i;
    long int d;
    d=pow(a,a);
    for(i=(c-1);i>=0;i--)
    {
     last[i]=fmod(d,pow(10,e));
     e++;
    }
    e=1;
    while(d>pow(10,(b-1)))
        d/=10;

    for(i=(c-1);i>=0;i--)
    {
        first[i]=fmod(d,pow(10,e));
        e++;
    }

    for(i=0;i<c;i++)
        printf("%d",first[i]);
    printf(" ");
    for(i=0;i<c;i++)
        printf("%d",last[i]);
    printf("\n");

}
int main()
{ int T;
  scanf("%d",&T);
  while(T--)
  {   long int a;
      int b;
      scanf("%ld %d",a,b);
      number(a,b);
  }

    return 0;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
P parker
  • 81
  • 1
  • 4
  • You need to help us help you. We don't know what problem you are trying to solve - can you give us more info. All programmers need to learn how to find the bugs in their programs. What have you done so far to track down the fault (other than ask us to do it for you)? I can see several beginners faults in the program, but I can't fix them because I don't know what you are tryig to achieve. – Brian Tompsett - 汤莱恩 Jan 08 '15 at 16:17
  • this line: 'scanf("%ld %d",a,b);' has a few problems. 1) always check the returned value from scanf() to assure the input/conversion operations were successful 2) the parameters must be 'address of' so the line should be: 'scanf("%ld %d",&a,&b);' 3) it is best to always begin a format string for the scanf family of functions with a ' ' (space) to consume any left over white space (like the newline) – user3629249 Jan 08 '15 at 17:26
  • Always prompt before getting input from a user, so the user will know what they are expected to input. A printf() before each scanf() would do the job. – user3629249 Jan 08 '15 at 17:28
  • for readability, please do not place statements on the same line after a '{' nor before a '}' – user3629249 Jan 08 '15 at 17:31
  • in C, indentation has no effect on how the code executes, and readability and for maintainability, wrap all code blocks with braces '{' and '}' – user3629249 Jan 08 '15 at 17:34
  • The code should be commented so we do not have to 'reverse-engineer' the code to determine what you are trying to perform. – user3629249 Jan 08 '15 at 17:35
  • regarding this line: '#include' the compiler can handle the lack of spacing between such tokens, but the human eye and many editors and code checkers often fail to properly handle such 'packing' please properly space code for readability and maintainability – user3629249 Jan 08 '15 at 17:38
  • please use meaningful names for variables and passed parameters. some short variable names, like 'i', 'j' for loop counters are qauite common, but passed variable names like 'a', 'b' are meaningless. – user3629249 Jan 08 '15 at 17:41
  • you need to know the parameters and returned value type for the system functions you use. for instance, the prototype: 'double pow(double, double)' and your code is using 'long int pow( long int, long int )' which results in lots of compiler generated conversions and is misleading to the reader (and if 'a' is of any reasonably large value, will result in a overflow and incorrect results – user3629249 Jan 08 '15 at 17:48
  • if parameter 'int b' contains a value 0 or less, then several places in the number() function will fail. – user3629249 Jan 08 '15 at 17:50

3 Answers3

5
scanf("%ld %d",&a,&b);

Using uninitialized variables lead to UB. You should use &a and &b to scan variables

Gopi
  • 19,784
  • 4
  • 24
  • 36
2

In your code you have

scanf("&ld %d",a,b);

It means you're trying to input integers to the memory locations of values of a and b. For and example let value of a = 1234566466 (long int), and b = 1234 (int). Accordingly 1234 is a memory location which is at the start of the RAM. Tn that area System files are loaded. So you are going to change system behaviour. That is not allowed.

Further when the complier allocate some memory space to your program, you can access only the memory which is inside your memory segment directly. But above statement trying to access another segment.

That's why you get segmentatin fault.

Isuru Herath
  • 298
  • 6
  • 20
1

You are passing an integer to a function that expects a pointer, for scanf the "%d" and "%ld" specifiers expect int * and long int * respectively, and you pass int and long int, so when trying to access the integers as if they were memory addresses the segmentation fault occurs.

The correct way to call scanf would be as Gopi said

scanf("%ld %d", &a, &b);

there you pass a and b addresses instead of their values.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97