Question #54302

After you have written a C++ program that selects and displays the maximum value of five numbers to be entered when the program is executed.
(Hint: Using a for loop with both a cin and if statement inside the loop)
how can I modify the program so that it displays both the maximum value and the position in the input set of numbers where the maximum occurs.
1

Expert's answer

2015-08-29T13:11:09-0400

Answer to Question #54302 - Programming - C++

Question

After you have written a C++ program that selects and displays the maximum value of five numbers to be entered when the program is executed.

(Hint: Using a for loop with both a cin and if statement inside the loop)

how can I modify the program so that it displays both the maximum value and the position in the input set of numbers where the maximum occurs.

Answer

#include <iostream>
using namespace std;
int main()
{
    double a[5];
    double max;
    int im = 0;
    cout << "Your numbers:" << endl;
    for (int i = 0; i < 5; i++)
    {
        cin >> a[i];
        if (i == 0)
        {
            max = a[i];
            im = i;
        }
        else if (a[i] > max)
        {
            max = a[i];
            im = i;
        }
    }
    cout << "Max number is " << max << "\n\twhich have " << im + 1 << " position" << endl;
}


https://www.AssignmentExpert.com

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!
LATEST TUTORIALS
APPROVED BY CLIENTS