Sudarshan
Sudarshan

Reputation: 96

C program shows different result in Linux and online compiler

I wrote a program in C to arrange the data in ascending order. When I compiled the code it showed no error but when it runs it shows a very different result than expected. However, when I ran the code in online C compiler it shows the correct result. I entered 5 different numbers 2 ,3 ,1 ,5 ,4.

Result in Linux: 0 1 2 3 4

Result in online compiler: 1 2 3 4 5

Why is this happening?

#include<stdio.h>

int * array(int x[],int l){
    int i,j,k;
    for(i=0;i<l;i++){
        for(j=0;j<l;j++){
            if(x[j]>x[j+1]){
                k=x[j];
                x[j]=x[j+1];
                x[j+1]=k;
            }
        }
    }
    return x;
}

void main(){
    int i,n;
    int *b;
    printf("enter n\n");
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    b=array(a,n);
    printf("the ascending order is: ");
    for(i=0;i<n;i++){
        fflush(stdout);
    printf("%d\t",b[i]);
    }
}

Upvotes: 1

Views: 153

Answers (2)

James K. Lowden
James K. Lowden

Reputation: 7837

You may find it easier to write programs to get their input from the command line instead of prompting for it. Using C11, you can allocate an array using the length provided by argc, and process the argv array directly:

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

int main( int argc, char *argv[] ) {
    if( argc == 1 ) {
      errx(EXIT_FAILURE, "syntax: %s values...", argv[0]);
    }
    int a[argc - 1];

    for( int i=0; i < argc-1; i++ ) {
      if( 1 != sscanf(argv[i + 1], "%d", a + i) ) {
        errx(EXIT_FAILURE, "could not scan '%s'", argv[i + 1]);
      }
    }

    array(a, sizeof(a)/sizeof(a[0]));
    printf("the ascending order is:");
    for (i = 0; i < n; i++) {
        printf(" %d", a[i]);
        fflush(stdout);
    }
    printf("\n");
}

Upvotes: 0

MrCryo
MrCryo

Reputation: 681

Your code accesses memory beyond your array:

for(j=0;j<l;j++){
    if (x[j] > x[j + 1]) {
         x[j] = x[j + 1];
         x[j + 1] = k;

In your case, when n = 5, you allocate the array for 5 elements with indices 0,1,2,3,4. The latest element of the array is x[4]. But when your code runs and j == l-1, you try to compare and even modify the element x[5]. In fact, your program should crash as it tries to access the "unallocated" memory. But probably because of aligning, the "x[5]" addresses the allocated memory. And, probably, x[5] = 0 on your computer, and your algorithm uses this element as a part of the sorting process. So your function array() returns the array of [0,1,2,3,4,5] and then your main() prints first five elements of this array.

That's why you've got sorted elements [0,1,2,3,4] instead of [1,2,3,4,5].

BTW, the bubble algorithm can be optimized to not touch already sorted elements.

Also, remember the array doesn't copy to pass into the function, the array always passes by its address, so it is not needed to "return" the modified array.

So, the final code can look like:

#include <stdio.h>

void array(int x[], int l)
{
    int i, j, k;

    for (i = l; i > 1; i--) {
        for (j = 1; j < i; j++) {
            if (x[j - 1] > x[j]) {
                k = x[j - 1];
                x[j - 1] = x[j];
                x[j] = k;
            }
        }
    }
}

void main()
{
    int i, n;

    printf("enter n\n");
    scanf("%d", &n);

    int a[n];

    for (i = 0; i < n; i++)
        scanf("%d",&a[i]);
    array(a, n);
    printf("the ascending order is:");
    for (i = 0; i < n; i++) {
        printf(" %d", a[i]);
        fflush(stdout);
    }
    printf("\n");
}

Of course, there are lots of things to be done in this code, like human-readable variables, formatting, further optimization. But I hope you can do it yourself.

Upvotes: 3

Related Questions