Answer to Question #114224 in C++ for Reshma

Question #114224
conducting a survey to rate the quality of their services in order to improve their
services. In the survey, 30 students were asked to rate the quality of the service in the library on a
scale of 1 to 5 (1 indicating very bad and 5 indicating excellent). You have to store the 30 responses of
the students in an array named responses[]. Then, you have to count the frequency of each scale
and store it in an array named frequency[]. Use the appropriate looping structure to enter the
responses and to count the frequency. You are also required to display the percentage of the
frequency of each scale. Display the scale, frequency and its percentage as shown below.
The program also allows the user to repeat this process as often as the user wishes.
1
Expert's answer
2020-05-08T12:48:09-0400
#include <iostream>
#include <iomanip>

using namespace std;

/** \brief Input integers.
  * \param s invitation string
  * \return int entered number
  * \details The method displays invitations to enter an integer number from the console.
  * It checks that the input is correct. If illegal characters appear, the prompt is duplicated and the input is repeated.
 */
int inputInt(string s)
{
    int d;
    bool b;

    do {
        b = true;
        cout << s;
        cin >> d;
        if (!cin)
        {
            cout << "The input is not correct. Try again.\n";
            cin.clear();
            while (cin.get() != '\n');
        }
        else
        {
            b = false;
        }
    } while (b);

    while (cin.get() != '\n');
    return d;
}



int main()
{
    int responses[30];
    bool finish = false;
    int i, k;

    do
    {
        int frequency[5] = { 0, 0, 0, 0, 0 };


        for (i = 0; i < 30; i++)
        {
            do
            {
                cout << "Student number " << i + 1 << ", please enter the library grade (1-5) : ";
                k = inputInt("> ");
                if ((k < 1 || k > 5))
                {
                    cout << "The evaluation should be in the range [1-5]. Try again." << endl;
                }
            } while (k < 1 || k > 5);
            responses[i] = k;
        }


        for (i = 0; i < 30; i++)
        {
            frequency[responses[i] - 1]++;
        }


        cout << endl;
        for (i = 0; i < 5; i++)
        {
            cout << "Score " << i + 1 << " met " << frequency[i] << " times. ";
            cout << "This represented " << fixed << setw(3) << setprecision(2) << right << setfill(' ') << ((double)frequency[i]) / 30 * 100 << "% of the total number of evaluations." << endl;
        }


        if (inputInt("Press \'0\' to exit or \'1\' to repeat. > ") == 0)
        {
            finish = true;
        }
    } while (!finish);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS