hi guys,
I am meant to write a program to print the largest number out of a series of 10 numbers. Now a second question ask that i modify the code to also print the second largest number... so that if for example i have 2,10,9,5,... before the modification this program will print just 10 but after modifying the code the program is suppose to print 10 and 9. The following is my code so far...
#include <iostream>
using namespace std;
int main()
{
int count = 1;
int nLNum;
int nhold = 0;
for (; count <= 10; count++){
cout << "Enter any series of numbers : ";
cin >> nLNum;
if (nLNum >= nhold){
nhold = nLNum;
}
}
cout << "The largest number is : " << nhold << endl;
return 0;
}
The problem now is i can't figure out a way to print both. Mind you i am restricted to just using simple selection and repetition structures.
You need another variable to hold the next smallest number, we'll call next_num, for now. In the if statement, before nhold = nLNum, have next_num to equal nhold. Also have another if statement, that checks if nLNum > next_num AND smaller than nhold. if(nLNum > next_num && nLNum < nhold)If it is, have next_num to equal nLNum