/* The function maxToFront should take an array as input and * move the maximum element to the front, keeping all other * elements in their initial order. */ #include #include void maxToFront(int a[], int n) { /* BEGIN ANSWER -- do not delete this line */ int im=0, i,tmp; /* im is index of maximal element */ for (i=0; i < n; i++) { /* With > in the next conditional, we find the first of equal maximal elements; with >= we would find the last */ if ( a[i] > a[im] ) { im = i; } } tmp = a[im]; for (i=im; i > 0; i--) { a[i] = a[i-1]; } a[0] = tmp; /* END ANSWER -- do not delete this line */ } /* PLEASE DO NOT EDIT THE 'main' FUNCTION. IT IS THERE * FOR TESTING PURPOSES. */ int main (void) { int i; int b[10]={6,2,4,44,5,-10,-6, 5, 8,2}; int c[15]={2,3,44,-6,4,8,-2,44,9,6,1,3,4,-11,0}; maxToFront(b, 10); printf("After maxToFront, b is %d", b[0]); for(i=1; i<10; i++) printf(", %d", b[i]); printf(".\n"); maxToFront(c, 15); printf("After maxToFront, c is %d", c[0]); for(i=1; i<15; i++) printf(", %d", c[i]); printf(".\n"); return EXIT_SUCCESS; }