This is a C Program to find the mode in a data set. The mode in statistics returns the highest frequency appearing element. If two or more values are same as the highest appearing, the set can be said to be bimodal or multimodal.
Here is source code of the C Program to Find the Mode in a Data Set . The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
int N = 20;
int sequence[20];
int mode() {
int maxValue = 0, maxCount = 0, i, j;
for (i = 0; i < N; ++i) {
int count = 0;
for (j = 0; j < N; ++j) {
if (sequence[j] == sequence[i])
++count;
}
if (count > maxCount) {
maxCount = count;
maxValue = sequence[i];
}
}
return maxValue;
}
int main(int argc, char **argv) {
int i;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
for (i = 0; i < N; i++)
sequence[i] = rand() % (100 - 1 + 1) + 1;
printf("The set of numbers are: ");
for (i = 0; i < N; i++)
printf("%d ", sequence[i]);
printf("\nThe mode of the set is: %d", mode());
}
Output:
$ gcc ModeOfSequence.c $ ./a.out The set of numbers are: 53 87 59 91 91 77 53 97 5 5 21 48 33 58 76 39 79 37 9 42 The mode of the set is: 53
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
Related Posts:
- Apply for C Internship
- Practice Computer Science MCQs
- Check C Books
- Check Computer Science Books
- Watch Advanced C Programming Videos