Write a program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to gather the remaining input. Each time the user enters a new number that is not -1, compare it to the variables and update them if necessary. When the user enters -1, display the smallest and largest numbers that the user entered. If the user did not enter any numbers before entering -1, then simply exit the program. Run the program and test it on several different sets of values to see if it works. Modify the program to ask the user how many numbers they wish to enter and use a for loop to ask for that many numbers. The sentinel value of -1 should no longer be necessary.

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
**Programming with While Loops: Smallest and Largest Numbers**

**Objective:**
Write a program utilizing a `while` loop to perform the following tasks:

1. **User Input:**
   - Continuously prompt the user to enter a number.
   - Use `-1` as an exit signal for the program.

2. **Track Numbers:**
   - Maintain two variables: one for the smallest number and another for the largest number.
   - **Initialization:**
     - Read the first number from the user.
     - If it is not `-1`, assign this number to both the smallest and largest variable.
   - **Processing Input:**
     - Use a loop to collect subsequent numbers.
     - For every new number entered (excluding `-1`), compare it to the current smallest and largest numbers and update these variables if necessary.

3. **Display Results:**
   - Once the user inputs `-1`, display the smallest and largest numbers entered.
   - If no numbers were entered before `-1`, terminate the program with no output.

**Testing:**
- Execute the program with various sets of numbers to ensure its functionality.

**Modification (Advanced):**
- Alter the program so the user specifies how many numbers they wish to enter.
- Replace the while loop with a loop that runs for the exact count of numbers specified.
- Remove the necessity for `-1` as a sentinel value.

This approach ensures a thorough understanding of loops, conditionals, and input handling in programming.
Transcribed Image Text:**Programming with While Loops: Smallest and Largest Numbers** **Objective:** Write a program utilizing a `while` loop to perform the following tasks: 1. **User Input:** - Continuously prompt the user to enter a number. - Use `-1` as an exit signal for the program. 2. **Track Numbers:** - Maintain two variables: one for the smallest number and another for the largest number. - **Initialization:** - Read the first number from the user. - If it is not `-1`, assign this number to both the smallest and largest variable. - **Processing Input:** - Use a loop to collect subsequent numbers. - For every new number entered (excluding `-1`), compare it to the current smallest and largest numbers and update these variables if necessary. 3. **Display Results:** - Once the user inputs `-1`, display the smallest and largest numbers entered. - If no numbers were entered before `-1`, terminate the program with no output. **Testing:** - Execute the program with various sets of numbers to ensure its functionality. **Modification (Advanced):** - Alter the program so the user specifies how many numbers they wish to enter. - Replace the while loop with a loop that runs for the exact count of numbers specified. - Remove the necessity for `-1` as a sentinel value. This approach ensures a thorough understanding of loops, conditionals, and input handling in programming.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Basics 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