c - Recursive Sort Function -
i've written program recursively sort array. however, following error on line 11: syntax error before ']' token.
here code:
//this program recursively sorts array #include<stdio.h> void rec_sort(int values[], int n); main() { int vals[4]; vals[0] = 37; vals[1] = 48; vals[2] = 56; vals[3] = 63; printf("this array sorted: %x\n", rec_sort(vals[], 4)); system("pause"); return 0; } void rec_sort(int values[], int n) { //base case if (n<2) return; int maxindex=0; int i; //find max item in array in indexes 0 through n-1 for(i=1; i<n;i++) { if(values[i] > values[maxindex]) maxindex=i; } //swap element 1 stored in n-1 //set temp n-1, set n-1 in array max, set max temp int temp = values[n-1]; //store temp last element in array values[n-1] = values[maxindex]; //store last element max value in array values[maxindex] = temp; //temp keep on changing, array sorted //recursively sort array values of length n-1 sort(values, n-1); }
it looks you're trying print out whole array, c won't in 1 call printf
. instead, need loop iterate through array , print out each number individually:
for (i=0; i<4; i++) printf("%x\n", vals[i]);
since rec_sort
isn't returning array, need invoke separately call printf, you'd like:
// sort data: rec_sort(vals, 4); // print sorted values: (i=0; i<4; i++) printf("%x\n", vals[i]);
Comments
Post a Comment