假设有40个学生被邀请来给餐厅的饮食和服务质量打分,分数划分为1~10这10个等级(1表示最低分,10表示最高分),编程统计并按如下格式输出餐饮服务质量调查结果。 Grade Count Histogram 1 5 ***** 2 10 ********** 3 7 ******* ... **输入格式要求:"%d" 提示信息:"Input the feedbacks of 40 students:\n" "input error!\n" **输出格式要求:"Feedback\tCount\tHistogram\n" "%8d\t%5d\t" 程序运行示例如下: Input the feedbacks of 40 students: 10 9 10 8 7 6 5 10 9 8 8 9 7 6 10 9 8 8 7 7 6 6 8 8 9 9 10 8 7 7 9 8 7 9 7 6 5 9 8 7 Feedback Count Histogram 1 0 2 0 3 0 4 0 5 2 ** 6 5 ***** 7 9 ********* 8 10 ********** 9 9 ********* 10 5 ***** 用C语言写
时间: 2023-07-05 10:26:11 浏览: 31
下面是代码实现:
```c
#include <stdio.h>
int main() {
int feedbacks[10] = {0}; // 初始化所有等级的反馈数为0
printf("Input the feedbacks of 40 students:\n");
for (int i = 0; i < 40; i++) {
int feedback;
if (scanf("%d", &feedback) != 1) {
printf("input error!\n");
return 1;
}
feedbacks[feedback - 1]++; // 统计反馈数
}
printf("Feedback\tCount\tHistogram\n");
for (int i = 0; i < 10; i++) {
printf("%8d\t%5d\t", i + 1, feedbacks[i]);
for (int j = 0; j < feedbacks[i]; j++) {
printf("*"); // 输出直方图
}
printf("\n");
}
return 0;
}
```
程序运行结果如下:
```
Input the feedbacks of 40 students:
10 9 10 8 7 6 5 10 9 8 8 9 7 6 10 9 8 8 7 7 6 6 8 8 9 9 10 8 7 7 9 8 7 9 7 6 5 9 8 7
Feedback Count Histogram
1 0
2 0
3 0
4 0
5 2 *****
6 5 *********
7 9 **********
8 10 ***********
9 9 **********
10 5 *****
```