[E-6]
Write a function that evaluates to the maximum of an integer array.
Here is a framework:
#include <stdio.h>
#include <stdlib.h>
/* Puzzle D21 -- find the maximum element in an integer array */
int findLargest( int size, int arr[] )
{
...
}
/* Functions from previous puzzles */
int randInt( int min, int max );
void printArray( int size, int arr[] );
void fillArrayRandom( int size, int arr[], int low, int high );
int main()
{
const int SIZE = 60;
int x[ SIZE ];
int max;
srand( time(NULL) );
fillArrayRandom( SIZE, x, 0, 99 );
printArray( x, SIZE );
printf("\n");
max = findLargest( x, SIZE );
printf("Max = %d\n", max );
printf("\n\n");
system("pause"); /* Remove if not needed */
return 0;
}
Here is sample output:
19 51 58 47 30 27 58 87 32 52 17 28 78 32 63 9 50 62 38 65 91 41 17 14 85 41 63 79 69 83 60 17 3 45 50 7 72 64 0 79 53 45 28 33 29 46 10 36 60 54 17 31 15 20 73 58 73 62 9 21 Max = 91
For any array with one or more elements there will be a largest element. If all elements are the same value, then that is the maximum. Be sure to consider the possibility that some or all elements are negative.
You will need a variable that holds the current maximum. How should it be initialized?