You could use a list, sort the list, then print the front and back value |
This is an entry-level exercise that's usually given by high school or college teachers as a first foray into programming. If you ask a newcomer "Bro, implement a sorted list and print its head and tail" you'll probably give them an irreversible trauma.
Well, not really, but they wouldn't even know where to start.
To solve this problem you only need three variables. We'll call them input, maxValue, and minValue. input and maxValue should be initialized to 0, whereas minValue should be initialized to an arbitrarily high value.
You need to write a while loop that executes until input equals 999 and checks input's value to maxValue and minValue and, if need be, assigns those values to minValue or maxValue.
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
using namespace std;
int main() {
cout << "Insert some values: " << endl;
unsigned int input = 0; //We use this for comparing its value to the other two variables
unsigned int minValue = RAND_MAX; //The minimum value the user inserts, set to an arbitrarily high value
unsigned int maxValue = 0; //The maximum value the user inserts
while (input != 999) {
//Read the number from the keypad
cin >> input;
//Is out input smaller than minValue?
if (input < minValue)
//If it is, assign input to minValue
minValue = input;
//Is our input larger than maxValue?
if (input > maxValue)
//If it is, assign input to maxValue
maxValue = input;
//The loop ends and restarts at line #10
}
cout << endl << "Minimum value: " << minValue << endl << "Maximum value: " << maxValue << endl; //Prints those values
system("pause"); //Wait for Enter to close the program.
return 0;
}
| |
I commented it to explain exactly what each line does, but next time try to solve the problem yourself and, when asking for help, outline exactly what you did to solve it and post your problem.