Write a program with a loop that lets the user enter a series of positve integer
ID: 3605713 • Letter: W
Question
Write a program with a loop that lets the user enter a series of positve integers. The user should enter -99 as a sentinel to signal the end of the series. If the user enters a negative integer (other than -99), your program should reject the input and prompt the user again for a positive integer, reminding them that -99 is the sentinel After all the numbers have been entered, the program should display the largest and smallest numbers entered. A sample program execution may look like this Please enter a series of positive integers Input as many as you want. hen you are finished, enter"-99" Integer: 3 Integer: 42 Integer: 12 Integer: 5 Integer: -8 WARNING:That is not a positive integer Please continue to enter a series of positive integers When you are finished, enter "-99" Integer: 33 Integer: 11 Integer: 72 Integer: 23 Integer:-99 nimum was 3 Maximum was: 72Explanation / Answer
// CPP code to find min and max of given integers
#include<iostream>
using namespace std;
main()
{
// TAKING input of first number and making that only min and max
cout << "Please enter a series of positive integers." << endl << "Input as many as you want." << endl << "When you are finished, enter '-99'"<< endl << endl;
int max, min, temp;
cout << "Integer: ";
cin >> max;
while(max<0)
{
cout << endl << "WARNING: That is not a positive integers" << endl << "Pkease continue to enter a series of positive integers." << endl << "When you are finished, end '-99'" << endl << endl;
cout << "Integer: ";
cin >> max;
}
min = max;
temp = max;
// taking input from second number on wards
while(temp!=-99)
{
// checking if it is maximum
if(temp > max)
max = temp;
// chekcking if it is minimum
if(temp < min)
min = temp;
cout << "Integer: ";
cin >> temp;
while(temp<0 && temp!=-99)
{
cout << endl << "WARNING: That is not a positive integers" << endl << "Pkease continue to enter a series of positive integers." << endl << "When you are finished, end '-99'" << endl << endl;
cout << "Integer: ";
cin >> temp;
}
}
// printing output
cout << endl << "Minimum was: "<< min << endl;
cout << "Maximum was: " << max << endl;
}
/*
SAMPLE OUTPUT
Please enter a series of positive integers.
Input as many as you want.
When you are finished, enter '-99'
Integer: 3
Integer: 42
Integer: 12
Integer: 5
Integer: -8
WARNING: That is not a positive integers
Pkease continue to enter a series of positive integers.
When you are finished, end '-99'
Integer: 33
Integer: 11
Integer: 72
Integer: 23
Integer: -99
Minimum was: 3
Maximum was: 72
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.