1

i tried counting sort and complied in "http://www.tutorialspoint.com/compile_c_online.php" and it ran perfectly but when i tried to compile in "http://codepad.org" ,it said segmentation fault.i tried using gdb but it didnot show any error. here is the code can anyone find the line causing it.

#include<stdio.h>
int main(void)
{
long long int t;
int i=0,j,max,min,temp,pos;
scanf("%lld",&t);//enter total numbers to be sorted
long long int a[t];
while(i<t)
    {   
        scanf("%lld",&a[i]);
        if(i==0) max=min=a[i];
        else
        {
            if(a[i]>=max) max=a[i];
            if(a[i]<min) min=a[i];
        }
        ++i;
    }
temp=(max-min+1);
long long int b[temp];
for(i=0;i<t;i++) 
    for(j=min;j<=max;j++)
        {
         if(i==0) b[j-min]=0;
         if(a[i]==j) ++b[j-min];
        }
for(i=0;i<temp;i++) if(i!=0) b[i]=b[i]+b[i-1];
long long int c[t];
for(i=0;i<t;i++)
{
    for(j=0;j<temp;j++)
        {
            if(a[i]==(j+min))
                {
                    pos=(b[j]-1);
                    c[pos]=j+min;
                    --b[j];
                }
        }
}
for(j=0;j<t;j++) printf("%lld\n",c[j]);
return(0);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Bazooka47
  • 17
  • 1
  • 1
    It means that the program has undefined behaviour.:) – Vlad from Moscow Jul 13 '16 at 13:15
  • I am a beginner,can you tell me which line is causing it ??? – Bazooka47 Jul 13 '16 at 13:18
  • Compile the code in a Linux environment using gcc, then run it under [valgrind](http://valgrind.org). If you're reading/writing memory you're not supposed to, it will tell you. – dbush Jul 13 '16 at 13:20
  • for ease of readability and understanding: 1) please indent your code consistently. NEVER use tabs for indenting. Suggest 4 spaces for each indent level as that is wide enough to be visible even with variable width fonts. Indent after every opening brace '{''. Un indent before every closing brace '}'. 2) separate code blocks (for, if, else, while, do...while, switch, case, default) via a blank line. 3) use meaningful variable names. A variable name should indicate `contents` or `usage` (or better, both) – user3629249 Jul 14 '16 at 15:48
  • Please follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* – user3629249 Jul 14 '16 at 15:49
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter value) to assure the operation was successful – user3629249 Jul 14 '16 at 15:53
  • this line: `while(i – user3629249 Jul 14 '16 at 15:56
  • to minimize mis-understandings and maintenance errors, always insert the opening an closing braces around code block bodies – user3629249 Jul 14 '16 at 15:57
  • when asking a user to enter a value, always prompt the user, so they know what they are expected to do. Otherwise the user is left with a blinking cursor and no idea what to do next – user3629249 Jul 14 '16 at 16:02
  • the variables `min` and `max` are integers, but the variable: `a[i]` is a long long integer, so errors can occur due to the conversions necessary for the assignment. Similar considerations exist for the variable `temp` and variable `j` and `pos`. – user3629249 Jul 14 '16 at 16:12
  • next time, when using `gdb`, check the actual values being input (by the user) to assure they are correct. – user3629249 Jul 14 '16 at 16:24

2 Answers2

0

It looks like that codepad does not support reading user input:

#include<stdio.h>
int main(void)
{
long long int t;
scanf("%lld",&t);//enter total numbers to be sorted
printf("%lld\n",t );
return 0;
}

Produces

Output:
134513968

No input is read, but a random number is there. With that number your program crash. Here is a solution for c++.

Community
  • 1
  • 1
kwarnke
  • 1,424
  • 1
  • 15
  • 10
  • if the posted code had checked the returned value (not the parameter value) from each of the calls to `scanf()`, then it would have found the problem of the codepad not allowing for user inputs. This is just another prime example that system function return values must be checked for errors. – user3629249 Jul 14 '16 at 16:19
0

after applying the changes listed in the comments to the question

The following code results:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    long long int t;
    int i=0;
    long long int j;
    long long int max;
    long long int min;
    long long int temp;
    long long int pos;

    printf( "%s", "enter total numbers to be sorted" );
    if( 1 != scanf("%lld",&t) )
    {
        printf("%s\n","first scanf failed");
        exit( EXIT_FAILURE );
    }

    long long int a[t];

    for(i=0; i<t; i++)
    {
        printf( "%s %d", "enter number: ", i );
        if( 1 != scanf("%lld",&a[i]) )
        {
            printf( "%s\n", "second scanf failed" );
            exit( EXIT_FAILURE);
        }

        if(i==0)
        {
           max=min=a[i];
        }

        else
        {
            if(a[i]>=max)
            {
                max=a[i];
            }

            if(a[i]<min)
            {
                min=a[i];
            }
        }
    }

    temp=(max-min+1);

    long long int b[temp];

    for(i=0;i<t;i++)
    {
        for(j=min;j<=max;j++)
        {
             if(i==0)
             {
                 b[j-min]=0;
             }

             if(a[i]==j)
             {
                 ++b[j-min];
             }
        }
    }

    for(i=0;i<temp;i++)
    {
        if(i!=0)
        {
            b[i]=b[i]+b[i-1];
        }
    }

    long long int c[t];

    for(i=0;i<t;i++)
    {
        for(j=0;j<temp;j++)
        {
            if(a[i]==(j+min))
            {
                pos=(b[j]-1);
                c[pos]=j+min;
                --b[j];
            }
        }
    }

    for(j=0;j<t;j++)
    {
        printf("%lld\n",c[j]);
    }


    return(0);
}

NOTE: It is better to use perror() rather than printf() for outputting error messages, but codepad will then fail to compile the code due to a call to sys_dup()

the output is:

enter total numbers to be sorted
first scanf failed
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • note: when run under a local computer, rather than on a web page, it works correctly – user3629249 Jul 14 '16 at 16:53
  • to avoid such problems in the future I suggest using: `http://ideone.com/` which allows you to 'pre define' all the user inputs – user3629249 Jul 14 '16 at 17:05
  • Should i use getchar to take the digits then transfer it to the required variable,if in this case scanf showing error ??? – Bazooka47 Jul 15 '16 at 06:36
  • the source of the problem is NOT the posted code, but rather the web page being used does not allow user input. So what ever trash in on the stack at the location of the variables is what is being used. – user3629249 Jul 15 '16 at 07:35