Histogram
#include<stdio.h>
int main()
{
int a[]={1,2,1,3,5,4,3,2,1,2,3,1,4,5,3,2,1,6,5,2,2,1,1};
histogram(a,23);
return 0;
}
int histogram(int a[],int n){
int i,j,k=0,l,m=0,count[25]={0},max=0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(a[i]==a[j]) count[i]++; //counting the occurance of each element in the array
}
int flag=0;
for(j=0;j<i;j++)
if(a[i]==a[j]) flag=1; // removing redundant element
//====================displaying histogram1===================*/
if(!flag)
{
a[m]=a[i];
count[m]=count[i];
max=max<count[m]?count[m]:max;
m++;
printf("%d %d ",a[i],count[i]);
for(k=0;k<count[i];k++) printf("*");
printf("\n");
}
}
printf("=================displaying histogram2==================\n");
for(i=0;i<m;i++)
{
for(l=0;l<max-count[i];l++) printf("\t");
for(j=0;j<count[i];j++) printf("*\t");
printf(" %d\t%d\n",count[i],a[i]);
}
printf("====================displaying histogram3=================\n");
for(i=0;i<max;i++){
for(j=0;j<m;j++){
if(i>=(max-count[j])) printf("*\t");
else printf("\t");
}
printf("\n");
}
for(j=0;j<m;j++) printf("%d\t",count[j]);printf("\n");
for(j=0;j<m;j++) printf("%d\t",a[j]);printf("\n");
printf("====================displaying histogram4=================\n");
for(j=0;j<m;j++) printf("%d\t",a[j]);printf("\n");
for(j=0;j<m;j++) printf("%d\t",count[j]);printf("\n");
for(i=0;i<max;i++){
for(j=0;j<m;j++){
if(i<(count[j])) printf("*\t");
else printf("\t");
}
printf("\n");
}
return 0;
}
int main()
{
int a[]={1,2,1,3,5,4,3,2,1,2,3,1,4,5,3,2,1,6,5,2,2,1,1};
histogram(a,23);
return 0;
}
int histogram(int a[],int n){
int i,j,k=0,l,m=0,count[25]={0},max=0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(a[i]==a[j]) count[i]++; //counting the occurance of each element in the array
}
int flag=0;
for(j=0;j<i;j++)
if(a[i]==a[j]) flag=1; // removing redundant element
//====================displaying histogram1===================*/
if(!flag)
{
a[m]=a[i];
count[m]=count[i];
max=max<count[m]?count[m]:max;
m++;
printf("%d %d ",a[i],count[i]);
for(k=0;k<count[i];k++) printf("*");
printf("\n");
}
}
printf("=================displaying histogram2==================\n");
for(i=0;i<m;i++)
{
for(l=0;l<max-count[i];l++) printf("\t");
for(j=0;j<count[i];j++) printf("*\t");
printf(" %d\t%d\n",count[i],a[i]);
}
printf("====================displaying histogram3=================\n");
for(i=0;i<max;i++){
for(j=0;j<m;j++){
if(i>=(max-count[j])) printf("*\t");
else printf("\t");
}
printf("\n");
}
for(j=0;j<m;j++) printf("%d\t",count[j]);printf("\n");
for(j=0;j<m;j++) printf("%d\t",a[j]);printf("\n");
printf("====================displaying histogram4=================\n");
for(j=0;j<m;j++) printf("%d\t",a[j]);printf("\n");
for(j=0;j<m;j++) printf("%d\t",count[j]);printf("\n");
for(i=0;i<max;i++){
for(j=0;j<m;j++){
if(i<(count[j])) printf("*\t");
else printf("\t");
}
printf("\n");
}
return 0;
}
Output:
1 7 *******
2 6 ******
3 4 ****
5 3 ***
4 2 **
6 1 *
=================displaying histogram2==================
* * * * * * * 7 1
* * * * * * 6 2
* * * * 4 3
* * * 3 5
* * 2 4
* 1 6
====================displaying histogram3=================
*
* *
* *
* * *
* * * *
* * * * *
* * * * * *
7 6 4 3 2 1
1 2 3 5 4 6
====================displaying histogram4=================
1 2 3 5 4 6
7 6 4 3 2 1
* * * * * *
* * * * *
* * * *
* * *
* *
* *
*