How many times does each element occur in the given array? Let n=7, and the elements are: 12, 34, 32, 12, 36, 32, 32. The output should be: (12: 2); (34: 1); (32: 3); (36,1) but should not be (12: 2); (34: 1); (32: 3); (12: 2); (36: 1); (32: 3); (32: 3).


Program:

#include<stdio.h>
#define undefined 10000    /* take a value which is beyond the range of values in the program */
int main()
{
       int n;
       int i, j,count;
       printf("n: ");
       scanf("%d",&n);
       int a[n];
       printf("enter elements\n");
       for(i=0;i<n;i++)
       scanf("%d",&a[i]);
       for(i=0;i<n;i++)
      {
             count=1;
             if(a[i]!=undefined)
            for(j=i+1;j<n;j++)
            {
                  if(a[i]==a[j])
                  {
                         count++;
                         a[j]=undefined;
                   }
            }
            if(a[i]!=undefined)
            printf("(%d,%d); ",a[i],count);
     }
     return 0;
}

No comments: