Practise Problem – 2: Write a program in C++to find the maximum from a sequence
ID: 3855411 • Letter: P
Question
Practise Problem – 2: Write a program in C++to find the maximum from a sequence of ten numbers. Your program should take 10 real numbers as user input. You can select double as the datatype for reading the numbers as input. The program should also print the position of maximum number in the sequence.Note: You can follow this flow-chart to write the program without using scanf and printf Practise Problem – 2: Write a program in C++to find the maximum from a sequence of ten numbers. Your program should take 10 real numbers as user input. You can select double as the datatype for reading the numbers as input. The program should also print the position of maximum number in the sequence.
Note: You can follow this flow-chart to write the program without using scanf and printf Practise Problem – 2: Write a program in C++to find the maximum from a sequence of ten numbers. Your program should take 10 real numbers as user input. You can select double as the datatype for reading the numbers as input. The program should also print the position of maximum number in the sequence.
Note: You can follow this flow-chart to write the program without using scanf and printf
Explanation / Answer
So here we go, the code in C++ to find the maximum from the sequence of 10 Numbers is given below: -
#include <iostream>
using namespace std;
int main ()
{
cout << "Enter 10 Integers ";
cout << "Please enter your numbers: ";
int num[9];
int high;
int pos;
int i=0;
cin >> num[0];
high=num[0];
for(i = 1; i <= 9; i++ )
{
cin >> num[i];// read input
if( num[i] > high )// compare to highest number
high = num[i];// replace highest
pos=i;
}
cout << "The highest number entered was: " << high<<" ";
if( num[0]==high){
cout << "The highest number's Position is: 1";
}
else{
cout << "The highest number's Position is: " << pos+1 <<" ";
}
return 0;
}
This code tells the highest number out of the 10 numbers entered and also prints the position of the Number.
In the given code we first accept the 10 Integers and then store them in an array and compare all the numbers with the first number if the number entered is greater than the first number then make that number the highest, and if first number is highest then print position 1, else print the position of i+1, as in array the first element is at position 0.
This is all for the question.
Thank You for using Chegg...!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.