This C program finds mode in an array.
Here is the source code of the C program to display mode of an array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C progarm to find mode in an array
*/
#include <stdio.h>
int main()
{
int i, j, a[20] = {0}, sum = 0, n, t, b[20] = {0}, k = 0, c = 1, max = 0, mode;
float x = 0.0, y = 0.0;
printf("\nEnter the limit\n");
scanf("%d", &n);
printf("Enter the set of numbers\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < n - 1; i++)
{
mode = 0;
for (j = i + 1; j < n; j++)
{
if (a[i] == a[j]) {
mode++;
}
}
if ((mode > max) && (mode != 0)) {
k = 0;
max = mode;
b[k] = a[i];
k++;
}
else if (mode == max) {
b[k] = a[i];
k++;
}
}
for (i = 0; i < n; i++)
{
if (a[i] == b[i])
c++;
}
if (c == n)
printf("\nThere is no mode");
else
{
printf("\nMode\t= ");
for (i = 0; i < k; i++)
printf("%d ",b[i]);
}
printf("\n");
return 0;
}
$ gcc mode.c $ a.out Enter the limit 10 Enter the set of numbers 1 2 2 3 4 5 5 6 7 8 Mode = 2 5
Sanfoundry Global Education & Learning Series – 1000 C Algorithms.
advertisement
advertisement
If you wish to look at all C Algorithms and Solutions, go to C Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for C Internship
- Practice BCA MCQs