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
Comments