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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for C Internship
- Buy Computer Science Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice BCA MCQs