Skip to main content

Sample C Program Counting Frequencies Of Elements Of Array.



#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define S 6

main()
{

int a[S], freq[S];
int i, j, k,n = S;
clrscr();

for(i = 0; i < S; i++)
  {
     printf(" \nEnter a[%d] element: ", i);
     scanf(" %d ", &a[i]);
     freq[i] = 1;
  }

clrscr();
printf(" Original Array\n ");

for(i = 0; i < S; i++)
      printf(" %d\t ", a[i]);

/* Main Logic Starts Here */

for(i = 0; i < n; i++)
  for(j = i + 1; j < n; j++)
    {
        if(a[i] == a[j])
          {
             for(k = j; k < n; k++)
                   a[k] = a[k+1];
           
             freq[i]++;
             n--;
          }
    }

printf(" \nArray with freq\n ");
printf(" \nElement\tFreq\n ");

for(i = 0; i < n; i++)
      printf(" %d\t%d\n ", a[i], freq[i]);

getch();

}



OUTPUT:

Enter a[0] element: 3
Enter a[1] element: 4
Enter a[2] element: 7
Enter a[3] element: 8
Enter a[4] element: 4
Enter a[5] element: 6


Original Array
3 4 7 8 4 6

Array with freq
Element Freq

3 1
4 2
7 1
8 1
6 1


Comments

Post a Comment

Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...

Experienced guys can share their resumes at admin@interview-made-easy.com