C Program to Find the Mode of a Data Set

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.

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<time.h>
  4. #include<stdlib.h>
  5.  
  6. int N = 20;
  7. int sequence[20];
  8.  
  9. int mode() {
  10.     int maxValue = 0, maxCount = 0, i, j;
  11.  
  12.     for (i = 0; i < N; ++i) {
  13.         int count = 0;
  14.         for (j = 0; j < N; ++j) {
  15.             if (sequence[j] == sequence[i])
  16.                 ++count;
  17.         }
  18.         if (count > maxCount) {
  19.             maxCount = count;
  20.             maxValue = sequence[i];
  21.         }
  22.     }
  23.  
  24.     return maxValue;
  25. }
  26.  
  27. int main(int argc, char **argv) {
  28.     int i;
  29.     time_t seconds;
  30.     time(&seconds);
  31.     srand((unsigned int) seconds);
  32.  
  33.     for (i = 0; i < N; i++)
  34.         sequence[i] = rand() % (100 - 1 + 1) + 1;
  35.  
  36.     printf("The set of numbers are: ");
  37.     for (i = 0; i < N; i++)
  38.         printf("%d ", sequence[i]);
  39.  
  40.     printf("\nThe mode of the set is: %d", mode());
  41. }

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
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.