I have this program that asks the user to enter a series ofintegers, and to type
ID: 3613966 • Letter: I
Question
I have this program that asks the user to enter a series ofintegers, and to type -99 when done:#include <iostream> using namespace std;
int main () { int pos = 1; //to specify ordering int num; int large, small; cout << "Type in a series of numbers with an 'enter'following each number. "; cout << "When you are finished, simply type in -99."<< endl; cout << "Enter integer number 1: "; cin >> num; large=small=num; while (num!= -99) { pos++; cout << "Enter integer number " << pos << ":"; cin >> num; if (num==-99) continue; if (num > large) large=num; if (num < small) small=num; }
cout << "The largest number entered is: " << large<< endl; cout << "The smallest number entered is: " <<small << endl; return 0;
}
________________________________________________
Nothing is wrong with the code, but can someone explain to mehow the following works: if (num > large) large=num; if (num < small) small=num;
How exactly does that work? How does it actually end uptelling me what the largest and smallest numbers enteredwere?
#include <iostream> using namespace std;
int main () { int pos = 1; //to specify ordering int num; int large, small; cout << "Type in a series of numbers with an 'enter'following each number. "; cout << "When you are finished, simply type in -99."<< endl; cout << "Enter integer number 1: "; cin >> num; large=small=num; while (num!= -99) { pos++; cout << "Enter integer number " << pos << ":"; cin >> num; if (num==-99) continue; if (num > large) large=num; if (num < small) small=num; }
cout << "The largest number entered is: " << large<< endl; cout << "The smallest number entered is: " <<small << endl; return 0;
}
________________________________________________
Nothing is wrong with the code, but can someone explain to mehow the following works: if (num > large) large=num; if (num < small) small=num;
How exactly does that work? How does it actually end uptelling me what the largest and smallest numbers enteredwere? #include <iostream> using namespace std;
int main () { int pos = 1; //to specify ordering int num; int large, small; cout << "Type in a series of numbers with an 'enter'following each number. "; cout << "When you are finished, simply type in -99."<< endl; cout << "Enter integer number 1: "; cin >> num; large=small=num; while (num!= -99) { pos++; cout << "Enter integer number " << pos << ":"; cin >> num; if (num==-99) continue; if (num > large) large=num; if (num < small) small=num; }
cout << "The largest number entered is: " << large<< endl; cout << "The smallest number entered is: " <<small << endl; return 0;
}
________________________________________________
Nothing is wrong with the code, but can someone explain to mehow the following works: if (num > large) large=num; if (num < small) small=num;
How exactly does that work? How does it actually end uptelling me what the largest and smallest numbers enteredwere? if (num > large) large=num; if (num < small) small=num;
How exactly does that work? How does it actually end uptelling me what the largest and smallest numbers enteredwere?
Explanation / Answer
imagine you have a number lets call it large. You have asecond number, lets call it num;
if you compare num and large if num is greater than large you cannow make num the large number and compare the next number tothat.
so for example. your list of numbers is 1,5,2,6,4,-99
get the number 1, assume it is the largest number
get 5
compare 5 to 1, it is larger so now make 5 the largest number
get 2
compare 2 to 5 it's smaller, do nothing
get 6
compare 6 to 5, it's larger so now make 6 the largest number
get 4
compare 4 to 6 it's smaller, do nothing
get -99 exit
same for small but now make the substitution when the numberis smaller
imagine you have a number lets call it large. You have asecond number, lets call it num;
if you compare num and large if num is greater than large you cannow make num the large number and compare the next number tothat.
so for example. your list of numbers is 1,5,2,6,4,-99
get the number 1, assume it is the largest number
get 5
compare 5 to 1, it is larger so now make 5 the largest number
get 2
compare 2 to 5 it's smaller, do nothing
get 6
compare 6 to 5, it's larger so now make 6 the largest number
get 4
compare 4 to 6 it's smaller, do nothing
get -99 exit
same for small but now make the substitution when the numberis smaller