Vision and dreams are the blueprints of soul and achievements.
-Mohammed Ahmed F

Counting Frequencies of Elements in an 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(" \n Enter a[%d] element: ", i);
       scanf(" %d ", &a[i]);
       freq[i] = 1;
     }
     printf(" Original Array\n ");
     for(i = 0; i < S; i++)
     printf(" %d   ", 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  Freq\n ");
     for(i = 0; i < n; i++)
     printf("%d  %d\n ", a[i], freq[i]);
     getch();
   }

Share this

Related Posts

Dear User,

Thank you for your comment. Hope you like this blog. Kindly share us on Social Media so others can be updated.

-Chief Administrative Officer.