質問

void quicksort(float x[],int first,int last)
{
    float temp;int  j, pivot, i;

    if(first<last){
         pivot=first;
         i=first;
         j=last;
         while(i<j){
             while(x[i]<=x[pivot]&&i<last)
                 i++;
             while(x[j]>x[pivot])
                 j--;
             if(i<j){
                 temp=x[i];
                  x[i]=x[j];
                  x[j]=temp;
             }
         }

         temp=x[pivot];
         x[pivot]=x[j];
         x[j]=temp;
         quicksort(x,first,j-1);
         quicksort(x,j+1,last);
    }
}

main()
{
    float a[]={4.0,6.0,8.5,9.1,6,11.1,1.1};
    //int len=6;
    int len=7;
    quicksort(a,0,len);
    int i=0;
    for(i=0;i<len;i++)
        printf("%d\n",a[i]);
}

During run-time on code-blocks ,prints gibberish and the loop does not ends after 7 iterations the execution goes on until it gives segmentation fault at the end. compiling it with gcc on command line does not helps .

but runs fine on my codeforces custom test USING GNU C++ 4.7 ie it ends after printing gibberish for 7 times. but gives

Runtime error: exit code is 11

when using GNU C 4

How to resolve this ?

役に立ちましたか?

解決 2

You're trying to print floats using the %d specifier, which is for integers. Try

printf("%f\n", a[i]);

instead.

他のヒント

There are two issues with your code:

  1. As mentioned by @Chowlett, to print float you need to use %f, not %d, and that covers the gibrrish part...
  2. Since the size of the array is 7, the last index is 6, not 7, and that probably covers the crash part..
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top