#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define N 20
int a[N];
void get_random(int uper_num);
int main(void)
{
int i,j,histogram[10]={0};
//水平直方图
get_random(10);
for(i=0;i<N;i++)
{
histogram[a[i]]++;
}
for(i=0;i<10;i++)
{
printf("%d ",histogram[i]);
}
printf("\n");
int max=histogram[0];
int tmp;
printf("水平直方图\n");
for(i=0;i<10;i++)
{
printf("%d:",i);
for(j=0;j<histogram[i];j++)
{
printf("* ");
}
if(max<=histogram[i])
max=histogram[i];
printf("\n");
}
printf("%d\n",max);
printf("竖直直方图\n");
for(int i=0;i<10;i++)
printf("%d ",i);
printf("\n");
for(int i=0;i<max;i++)
{
for(int j=0;j<10;j++)
{
if(histogram[j]-->0)
{
printf("* ");
}
else
printf(" ");
}
printf("\n");
}
printf("\n");
}
void get_random(int uper_num)
{
srand((unsigned)time(NULL));
for(int i=0;i<N;i++)
{
a[i]=rand()%(uper_num-1)+0;
}
}
打印水平和竖直直方图
最新推荐文章于 2022-02-09 07:10:32 发布