Traverse all elements in array, then store greater number in a variable by comparing with current element
int greatest_numers_in_array(const int numbers[], long unsigned int max_ele){
int result=0;
for(long unsigned int i=0; i < max_ele; i++){
if(numbers[i] > result){
result = numbers[i];
}
}
return result;
}
Complete code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_LENGTH(ARR) \
(sizeof(ARR)/sizeof(ARR[0]))
int greatest_numers_in_array(const int numbers[], long unsigned int max_ele){
int result=0;
for(long unsigned int i=0; i < max_ele; i++){
if(numbers[i] > result){
result = numbers[i];
}
}
return result;
}
int main(int argc, const char *argv[]){
int numbers[10]={0x00};
srand(time(NULL));
for( int i=0; i< 10; i++){
for( int j=0; j < 10; j++){
numbers[j] = rand()%1000;
}
printf("Greatest among (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d) is %d\n", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5], numbers[6], numbers[7], numbers[8], numbers[9], greatest_numers_in_array(numbers, ARRAY_LENGTH(numbers)) );
}
return 0;
}
code compilation and out put
rajesh@ideapad:~/Rajesh/Blog/findgreatest$ gcc find_greatest_number.c
rajesh@ideapad:~/Rajesh/Blog/findgreatest$ ./a.out
Greatest among (978,235,957,270,361,360,637,514,64,558) is 978
Greatest among (635,491,917,22,547,762,78,609,7,845) is 917
Greatest among (291,277,135,181,738,581,269,845,254,262) is 845
Greatest among (943,232,849,900,502,210,261,139,77,677) is 943
Greatest among (49,64,169,318,438,716,432,869,677,440) is 869
Greatest among (714,969,69,849,150,807,431,772,4,685) is 969
Greatest among (386,947,269,235,199,771,798,460,262,227) is 947
Greatest among (490,663,291,11,981,730,79,413,951,757) is 981
Greatest among (205,665,726,274,514,228,433,297,352,789) is 789
Greatest among (982,738,736,603,974,288,374,124,748,988) is 988