原题:假设有40个学生被邀请来给餐厅的饮食和服务质量打分,分数划分为1~10这10个等级(1表示最低分,10表示最高分,编程统计并按如下格式输出餐饮服务质量调查结果
Grade Count Histogram
1 5 *****
2 10 **********
3 7 *******
......
因为自己一个一个输出40个等级太麻烦了,就设置了随机种子来帮我给40个学生打分
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 40
void score(int a[]);
void count(int a[],int b[]);
void print(int a[],int b[]);
int main(void)
{
int a[40],b[10]={0};
score(a);
count(a,b);
print(a,b);
}
//函数功能:随机产生40个评价并存在a数组中
void score(int a[])
{
srand(time(NULL));
for(int i=0;i<40;i++)
{
a[i] = rand()%10 + 1;
}
}
//函数功能:统计a数组中1-10评价的个数,并存放在数组b中
void count(int a[],int b[])
{
for(int i=0;i<40;i++)
{
int c = a[i];
switch(c)
{
case 1:b[0]++;break;
case 2:b[1]++;break;
case 3:b[2]++;break;
case 4:b[3]++;break;
case 5:b[4]++;break;
case 6:b[5]++;break;
case 7:b[6]++;break;
case 8:b[7]++;break;
case 9:b[8]++;break;
case 10:b[9]++;break;
}
}
}
//函数功能:打印输出餐饮服务质量调查结果
void print(int a[],int b[])
{
printf("Grade\tCount\tHistogram\n");
for(int i=0;i<10;i++)
{
printf("%d\t%d\t",i+1,b[i]);
for(int j=0;j<b[i];j++)
printf("*");
printf("\n");
}
}
参考输出