C++ 如何在c++;是否按计数和百分比显示调查响应?

C++ 如何在c++;是否按计数和百分比显示调查响应?,c++,arrays,loops,if-statement,C++,Arrays,Loops,If Statement,问题是这样的 “30名学生被要求对学生自助餐厅的食物质量进行评分 1到5的刻度(1=差,2=公平,3=中性,4=好,5=极好)。编写一个C++程序,将45个响应存储到一维数组中,并给出计数和百分比“”的每种情况的摘要。 我已经写了一个程序,它可以运行,但是,我认为它很长,我很难找到替代方案 #include <iostream> using namespace std; int main() { int count1 = 0, count2 = 0, count3 = 0,

问题是这样的

“30名学生被要求对学生自助餐厅的食物质量进行评分 1到5的刻度(1=差,2=公平,3=中性,4=好,5=极好)。编写一个C++程序,将45个响应存储到一维数组中,并给出计数和百分比“

”的每种情况的摘要。 我已经写了一个程序,它可以运行,但是,我认为它很长,我很难找到替代方案

#include <iostream>
using namespace std;
int main()
{
    int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0;
    //  int count=0;
    int rate[45];
    for (int i = 0; i < 45; i++) {
        cout << "Please rate the school's food on 1-5 scale" << endl;
        cin >> rate[i];
    }
    for (int i = 0; i < 45; i++) {
        cout << i + 1 << "." << rate[i] << endl;
    }

    for (int i = 0; i < 45; i++) {
        if (rate[i] == 1) {
            count1 = count1 + 1;
        }
        if (rate[i] == 2) {
            count2 = count2 + 1;
        }
        if (rate[i] == 3) {
            count3 = count3 + 1;
        }
        if (rate[i] == 4) {
            count4 = count4 + 1;
        }
        if (rate[i] == 5) {
            count5 = count5 + 1;
        }
    }

    cout << "------------SUMMARY--------------" << endl;
    cout << "Students who rated:" << endl;
    cout << "The school's food deserves a 1: " << count1 << " " << (count1 * 100) / 45 << "% of the students" << endl;
    cout << "The school's food deserves a 2: " << count2 << " " << (count2 * 100) / 45 << "% of the students" << endl;
    cout << "The school's food deserves a 3: " << count3 << " " << (count3 * 100) / 45 << "% of the students" << endl;
    cout << "The school's food deserves a 4: " << count4 << " " < (count4 * 100) / 45 << "% of the students" << endl;
    cout << "The school's food deserves a 5: " << count5 << " " << (count5 * 100) / 45 << "% of the students" << endl;
    cout << "-------------END OF SUMMARY----------" << endl;

    return 0;
}
#包括
使用名称空间std;
int main()
{
int count1=0,count2=0,count3=0,count4=0,count5=0;
//整数计数=0;
积分率[45];
对于(int i=0;i<45;i++){
cout rate[i];
}
对于(int i=0;i<45;i++){

这可能是代码审查的一个问题。无符号计数[5]={0};++count[rate[i]-1]等。一个std::unordered\u映射可以帮助减少一些代码