[E-5]
Write function that checks that the elements of an array are in ascending order.
Return 0 if the array is in order; otherwise, return the number of elements
that are out of order. Here are some sample runs:
1 2 3 4 6 7 8 7 10 11 13 20 31 41 41 45 50 53 52 52 2 elements out of order. 1 1 1 4 6 7 8 8 10 11 13 20 31 41 41 45 50 51 52 52 All elements are in order.
The phrase "number of elements that are out of order" is vague. Decide on a simple meaning for the phrase.
Here is a testing framework:
/* Puzzle D48 -- check that the elements of an array are in
| ascending order
|
| Return 0 if in order, otherwise return the number of times
| an element is out of order.
*/
int inOrder( int size, int arr[] )
{
. . . .
}
void printArray( int size, int arr[] )
{
const int N = 10;
int j;
for ( j=0; j<size; j++ )
{
if ( j%N == N-1 )
printf("%4d\n", arr[j] );
else
printf("%4d ", arr[j] );
}
}
int main()
{
int x[] = { 1, 1, 1, 4, 6, 5, 8, 8, 10, 11, 13, 20,
31, 41, 41, 45, 50, 51, 52, 52 };
const int SIZE = sizeof(x)/sizeof(int);
int num;
printArray( SIZE, x );
printf("\n\n");
num = inOrder( SIZE, x );
if ( num>0 )
printf("%d elements out of order.\n", num ) ;
else
printf("All elements are in order.\n");
printf("\n\n");
return 0;
}