Newer at c++ (actually algorithm)

build the program in c ++ that finds the maximum and minimum element for some values given by the keypad. The actions are interrupted when the user places the number 999.
pls I need help
You could use a list, sort the list, then print the front and back values
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.
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
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

using TYPE = int;       // or unsigned, or double, or whatever

int main()
{
   bool someData = false;
   TYPE value, minValue, maxValue;
   TYPE endValue = 999;

   cout << "Insert values (" << endValue << " to end):\n";
   
   while ( true )
   {
      cin >> value;
      if ( value == endValue ) break;   // Finish

      if ( !someData )                  // No previous data
      {
         minValue = value;
         maxValue = value;
         someData = true;
      }                                 // Else compare with previous
      else if ( value < minValue ) minValue = value;
      else if ( value > maxValue ) maxValue = value;
   }
   
   if ( someData )
   {
      cout << "Minimum = " << minValue << '\n'
           << "Maximum = " << maxValue << '\n';
   }
   else
   {
      cout << "No data\n";
   }
}


Insert values (999 to end):
999
No data


Insert values (999 to end):
-10  1024  12345678 999
Minimum = -10
Maximum = 12345678
on paper, doing it directly (as above) is faster ( O(N)) than sorting.

any reason not to do-while not 999 rather than the spaghetti break logic stuff? Curious, not criticizing.

An input operation can fail. One can test whether it did. Therefore, lastchance's lines 14-17 could read:
1
2
while ( cin >> value && value != endValue )
{

The logical AND (&&) is lazy; if left side is false, then right side won't be evaluated at all.


if ( foo == bar ) is not trivial, if the foo and bar are floating point values (like double). Floats have limited precision.
any reason not to do-while not 999 rather than the spaghetti break logic stuff?


I have to test value for the sentinel after cin >> value to prevent assigning 999 to maxVal. If I wrote
while (input != 999)
then I would have to check it twice on each loop, which I regard as unnecessary.

Separating the cin >> value from the loop control would also, in principle, (although not done here) allow the code to react to accidental non-numeric input, rather than concluding the input immediately the user accidentally hits a letter rather than a digit.

However, I could (and often do) do what @Keskiverto suggested:
while ( cin >> value && value != endValue )
I imagine this produces not significantly different machine code.

It wasn't spaghetti to me, anyway!
Last edited on
I see. I tend to avoid breaking out of loops that way unless there is no good alternative, so I asked, thanks!
Topic archived. No new replies allowed.