#include #include #include // Most Frequent Number // // Write a complete C program that generates 100 random numbers in // the range -25 to 25 (inclusive) and counts how many times each of // the numbers has been generated. The program must then print: // 1) the number of occurrences for each of the numbers; // 2) the most frequently generated number and its // corresponding number of occurrences. int main() { int i; int count[51]; // count[0] -> -25 // 0 // count[1] -> -24 // 3 //... // count[25] -> 0 // 5 // ... // count[49] -> 24 // count[50] -> 25 for(i=0; i<51; i++) count[i]=0; srand(time(NULL)); //generates 100 random numbers for(i=0; i<100; i++) { int r= rand()%51; // [0,50] r = r -25; // [-25, 25] printf("%d, ",r); count[r+25] ++; } // debug for(i=0; i<51; i++) printf("% 2d --> %2d\n", i-25, count[i]); int max=count[0]; int idx=0; for(i=1; i<51; i++) { if(count[i] > max) { max=count[i]; idx=i; } } printf("The max is: %d\n", idx-25); system("pause"); }