Question #52414

Write a C++ program that reads 40 student scores (each between 0 and 20)
into an array, and then prints out the distribution of the scores, i.e., it prints, for each
possible score, the number of students who got that score.

Expert's answer

#include <iostream>
using namespace std;
int main()
{
    int a[40], score[20] = {0};
    for (int i = 0; i < 40; i++)
    {
        cout << "Student" << i + 1 << " score is ";
        cin >> a[i];
        if (a[i] < 0 | a[i] > 20)
        {
            cout << "mistaken score" << endl;
            i--;
        }
    }
    for (int i = 0; i < 40; i++)
    {
        score[a[i]]++;
    }
    for (int i = 0; i < 20; i++)
    {
        cout << score[i] << " students have " << i << endl;
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS