Just a heads up before you guys attempt to tackle this program. I\'m currently i
ID: 637988 • Letter: J
Question
Just a heads up before you guys attempt to tackle this program. I'm currently in a computer science I course, so this program has to be done at a basic level of c++. I've only covered up to for loops, while loops, switch statements, if-else statements and similar topics. If someone could figure out this program as well as explain the steps in a manor a beginner like myself can figure out that would be great!
For this assignment you are to write a program that finds the minimum and maximum of a set of numbers entered by the user. Your program should enter a loop in which it repeatedly asks the user to enter a number. The program should exit the loop if the user enters a
Explanation / Answer
// Online URL : http://ideone.com/NybCeQ
// Header imported for cin and cout opertations.
#include<iostream>
// Namespace std is used to call cout and cin method from "std" standard namespace as they can be defined into other namespaces as well.
using namespace std;
// Main method for program execution
int main(){
int n;
// Primary Loop for program to contine or not.
while (true){
// Integer variables to hold min and max numbers.
// Datatype Integers variables defined since the numbers should be 0 - 9999 range and not decimals or fractions.
// Value of max set to lowest number range. On the first run the comparison will always fail and it would be set correctly.
int max = -2;
// Value of min set to highest number range. On the first run the comparison will always fail and it would be set correctly.
int min = 10000;
// Header message displayed to console through "cout" (console out) method.
cout << "Please enter as many numbers as you like. Enter -1 to stop." << endl;
// Inner Loop for finding out max and min number based on user inputs.
while (true){
// Message to enter a number
cout << "Please enter a number: ";
cin >> n;
// If-else block handle the input number
if (n == -1) {
// Breaks the loop if number entered is -1
break;
}
else if (n <= 0) {
// Displays the error message if user enters any number below 0
cout << "I accept positive numbers less than 9999 ( or -1 to stop)" << endl;
}
else{
// If else block to compare the input number with existing min and max variables
if (n < min) {
// Sets the min variable with input number N if less than existing min value. First run it will always set to min.
min = n;
}
if (n > max) {
// Sets the max variable with input number N if greater than existing max value. First run it will always set to max.
max = n;
}
}
}
// Prints out the max and min number post the above loop breaks with -1 entry.
if (max==-2) {
// Max variable will be set to -2 if user exits with -1 entry at first run. This means none of the numbers are entered.
cout<<"None of the numbers are entered.";
} else {
// Else print out the max and min numbers
cout << "max: " << max << endl;
cout << "min: " << min << endl;
}
// Ask user input to continue or not
cout << "Do you wish to continue? (y/n): ";
char ch;
// Take user inputs in char variable
cin >> ch;
// if input value is "N" or "n" then exits the current loop.
if (ch == 'N' || ch == 'n')
break;
}
// Print out Goodbye message once the primary loop is exited.
cout << "Goodbye ! " << endl;
// Exit the main function with EXITCODE 0
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.