I'd like some help please. I'm trying to find the maximum and minimum value in an array. The looping, and the random allocation of values to the array works, but the if statements aren't doing their jobs, or my logic is flawed.
nb. initial variables assigned ... max possible value = minimum and min possible = maximum.
/*Finding out the max and min values of the elements in an array*/
#include <stdio.h>
#include <stdlib.h>
main()
{
int element=0,maximum=0,minimum=32767,randomValues[100];
/* Each element in the array is given a random value 0 .. 32767 */
for (element=0; element<100; element++) {
randomValues[element]=rand();
}
/* Loop - examines whether the element 0 .. 99 is greater than the maximum and less than the
minimum, if a statement is true it will evaluate the element equal to maximum or/and minimum respectively
*/
for (element=0; element<=99; element++)
{ printf("Element %i = %i\n",element, randomValues[element]);
if (randomValues[element] > maximum){
randomValues[element] = maximum;
}
if (randomValues[element] < minimum){
randomValues[element] = minimum;
}
}
/* Printing the max and min values*/
printf("The minimum value is %i\n",minimum);
printf("The maximum value is %i\n",maximum);
return 0;
}