Write a C program that reads numbers from an integer array and graph the information in the form of a bar chart. The 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 *
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void main( void){
int integerArray[]={1,2,3,5,8,4,5,8,7,4,2,4,5,6,5,1,2,2,5,6,2,2,5,5,5,6,9,5,2,3,6,5,4,5,5,5,6};
int counterArray[10]={0};
int i,j;
for(i=0;i<sizeof(integerArray)/sizeof(integerArray[0]);i++){
counterArray[integerArray[i]]++;
}
for(i=0;i<10;i++){
printf("%d %d ",i,counterArray[i]);
for(j=0;j<counterArray[i];j++){
printf("*");
}
printf("\n");
}
getchar();
getchar();
}
Comments
Leave a comment