Write a C program that read numbers from an integer array and graph the information in the form of bar chat. Sample output is given below.
Element Value Histogram
0 19 *******************
1 3 ***
2 15 ***************
3 7 *******
4 11 ***********
5 9 *********
6 13 *************
7 5 *****
8 17 *****************
9 1 *
Source code
#include <stdio.h>
void printStar(int n){
  for(int i=0;i<n;i++){
    printf("*");
  }
}
void main()
{
  int array1[100];
  int frequency[100];
  int num, i, j, count;
printf("Enter the number of elements to be stored in the array :");
    scanf("%d",&num);
 Â
    printf("Input %d elements in the array :\n",num);
    for(i=0;i<num;i++)
      {
     scanf("%d",&array1[i]);
   frequency[i] = -1;
  }
  for(i=0; i<num; i++)
  {
    count = 1;
    for(j=i+1; j<num; j++)
    {
      if(array1[i]==array1[j])
      {
        count++;
        frequency[j] = 0;
      }
    }
    if(frequency[i]!=0)
    {
      frequency[i] = count;
    }
  }
  printf("\nElement \tValue \tHistogram: \n");
  for(i=0; i<num; i++)
  {
    if(frequency[i]!=0)
    {
      printf("%d \t%d \t", array1[i], frequency[i]);
      printStar(frequency[i]);
      printf("\n");
    }
  }
}
Output
Comments
Leave a comment