Mega Code Archive

 
Categories / C / Development
 

Locate highest, lowest nr in array

#include <stdio.h> #include <stdlib.h> #define MAXARRAY 5 /* return the largest of the two passed numbers */ int high(int one, int two) { if(one > two) return one; else return two; } /* return smallest of the two passed numbers */ int low(int one, int two) { if(one < two) return one; else return two; } int main(void) { int array[MAXARRAY] = {0}; int small = 999, big = 0; int i = 0; /* load some random values into the array */ for(i = 0; i < MAXARRAY; i++) array[i] = rand() % 100; /* print the set of numbers */ printf("Set of numbers: "); for(i = 0; i < MAXARRAY; i++) printf(" %d ", array[i]); printf("\n"); /* loop over numbers, saving the desired nr */ for(i = 0; i < MAXARRAY; i++) { big = high(array[i], big); small = low(array[i], small); } printf("high: %d\n", big); printf("low : %d\n", small); return 0; }