These are my Instructions: "The program is supposed to use a loop to let the user enter positive integers. Your program should keep track of the biggest positive number it has found. Use a loop to continue asking the user to enter a positive number until the user enters the sentinel value (which is -1). Once the user enters -1, that should trigger the loop to stop, and then display the maximum number. The variable userNumber should be used for storing the input the user gives you. The variable maxVal keeps track of the largest integer found so far. In the loop, prompt and store the user's number. Then check, if userNumber is positive and userNumber is greater than maxVal. If it is, then update maxVal to equal userNumber. Repeat this process until userNumber is equal to the SENTINEL variable. You will be creating three different versions of this logic. For the first TODO, try to implement the above logic using a while loop. Once you have that working, comment out your while loop. The second TODO is to try creating a do-while loop that replicates the same logic. Repeat this again for the third TODO (remembering to comment out the previous attempts), but now using a for loop."     This is what I have thus far: #include using namespace std; int main() { // variables int maxVal = 0, userNumber = 0; const int SENTINEL = -1; // TODO: create a `while` loop version of finding the maximum number cout << "Enter a number: "; cin >> userNumber; while (userNumber != SENTINEL) { userNumber = ++maxVal; cout << userNumber << endl; } // TODO: create a `do-while` loop version of finding the maximum number do { cout << "Enter a number: "; cin >> userNumber; if(userNumber < -1) cout << "Error, invalid input! Try again!\n"; userNumber++; } while (userNumber < -1); // TODO: create a `for` loop version of finding the maximum number for (userNumber= 0; maxVal <= 200;) { cout << "Enter a number: "; cin >> userNumber; } // display results cout << "\nMaximum number entered: " << maxVal << endl; // terminate return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

These are my Instructions:

"The program is supposed to use a loop to let the user enter positive integers. Your program should keep track of the biggest positive number it has found. Use a loop to continue asking the user to enter a positive number until the user enters the sentinel value (which is -1). Once the user enters -1, that should trigger the loop to stop, and then display the maximum number.

The variable userNumber should be used for storing the input the user gives you. The variable maxVal keeps track of the largest integer found so far.

In the loop, prompt and store the user's number. Then check, if userNumber is positive and userNumber is greater than maxVal. If it is, then update maxVal to equal userNumber. Repeat this process until userNumber is equal to the SENTINEL variable.

You will be creating three different versions of this logic. For the first TODO, try to implement the above logic using a while loop. Once you have that working, comment out your while loop. The second TODO is to try creating a do-while loop that replicates the same logic. Repeat this again for the third TODO (remembering to comment out the previous attempts), but now using a for loop."

 

 

This is what I have thus far:

#include <iostream>

using namespace std;

int main()
{
// variables
int maxVal = 0, userNumber = 0;
const int SENTINEL = -1;

// TODO: create a `while` loop version of finding the maximum number


cout << "Enter a number: ";
cin >> userNumber;
while (userNumber != SENTINEL)
{

userNumber = ++maxVal;
cout << userNumber << endl;
}


// TODO: create a `do-while` loop version of finding the maximum number

do
{
cout << "Enter a number: ";
cin >> userNumber;
if(userNumber < -1)
cout << "Error, invalid input! Try again!\n";

userNumber++;

} while (userNumber < -1);


// TODO: create a `for` loop version of finding the maximum number

for (userNumber= 0; maxVal <= 200;)
{
cout << "Enter a number: ";
cin >> userNumber;
}


// display results
cout << "\nMaximum number entered: " << maxVal << endl;

// terminate
return 0;
}

Expert Solution
Step 1

Code:

 

#include <iostream>

using namespace std;

int main()
{
    //declating required variables
    int userNumber,maxVal=0;
    int SENTINEL = -1;
    //TODO while loop for getting the maximum value
    while(true) {
        cout << "Enter Any Positive Number : " << std::endl;
        cin >> userNumber;
        if(userNumber > maxVal) {
            maxVal = userNumber;
        }
        if (userNumber==SENTINEL) {
            break;
        }
    }
    //TODO do while loop for getting the maximum value
    
   /* do {
        std::cout << "Enter Any Positive Number : " << std::endl;
        std::cin >> userNumber;
        if(userNumber > maxVal) {
            maxVal = userNumber;
        }
    } while (userNumber != SENTINEL); */
    
    
    //TODO for loop for getting the maximum value
    /* for(;;) {
        std::cout << "Enter Any Positive Number : " << std::endl;
        std::cin >> userNumber;
        if(userNumber > maxVal) {
            maxVal = userNumber;
        }
        if (userNumber==SENTINEL) {
            break;
        }
    } */
    //printing the result
    std::cout << "maxVal : " << maxVal << std::endl;

    return 0;
}

 

Code Screenshot:

Computer Science homework question answer, step 1, image 1

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education