Answer to Question #260886 in C++ for Charmaine

Question #260886

Write a program that loads several numbers from the keyboard,

asking in the meantime, the question "Do you want to finish: Y / N?"

At the end of the loop, program should display calculated values of:

the arithmetic averagemaximumminimum of given numbers.


1
Expert's answer
2021-11-03T17:30:27-0400
#include <iostream>
#include <string>
using namespace std;

int main() {
    double sum=0, min_val, max_val, val;
    int n=0;
    string ans;
    
    do {
        cout << "Enter a integer value ";
        cin >> val;
        if (n == 0) {
            min_val = max_val = val;
        }
        sum += val;
        n++;

        if (min_val > val) {
            min_val = val;
        }

        if (max_val < val) {
            max_val = val;
        }

        while (true) {
            cout << "Do you want to finish: Y/N: ";
            cin >> ans;
            if (ans[0] == 'Y' || ans[0] == 'N') {
                break;
            }
        }
    } while (ans[0] == 'N');

    double avg = sum / n;
    cout << endl;
    cout << "Arithmetic average: " << avg << endl;
    cout << "Maximum: " << max_val << endl;
    cout << "Minimum: " << min_val << endl;

    return 0;
}

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