Exercise 4: Practice to manipulate data in arrays
Write a C program that read numbers from an integer array and graph the information in the form of 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 values[]={0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9};
int counterValues[10]={0};
int i,j;
for(i=0;i<sizeof(values)/sizeof(values[0]);i++){
counterValues[values[i]]++;
}
for(i=0;i<10;i++){
printf("%d %d ",i,counterValues[i]);
for(j=0;j<counterValues[i];j++){
printf("*");
}
printf("\n");
}
getchar();
getchar();
}
Comments
Leave a comment