Max until Sentinel

Ok so I've been assigned a code to write that seems fairly easy but I'm having trouble figuring out one thing. I understand iteration and while loops but I don't know how to store the max value. The assignment is to find the largest numbers entered until -1 is entered. The entered values can be anything but -1 which means other negatives can be entered. I have this accounted for with the break so we're good there. The only thing I can't figure out is how to get it to store the highest entered value. I was thinking something along the lines of using an if statement where if the previously entered value was larger then the newly entered value then store that value into a variable like "max" and then at the end of the loop when -1 is entered, I'll call back that variable and output it but I just can't figure out how to do it. Here's the basis of my code. Everything is ready to output something but I just can't figure out how to get it to store the max value for later output.

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
#include <iostream>
using namespace std;

int main()
{
	cout << "This program finds the maximum number." << endl << endl;

	float num;

// Get the intitial number. 
	cout << "Number: ";
	cin >> num;


	while( true )
	{
		cout << "Number: ";
		cin >> num;

		

		if(num == -1)
			break;
	}

	
	cout << "The largest number is " <<  << ".";
}
A good way to start since you want to store a lot of values (in such as way) would be using an array and a for-loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
int main(int argc, char* argv[])
{
    double input [50];
    
    for (int n=0; n<49; n++) // will keep running until 50 values are entered or -1 is entered
    {
        cin >> input[n];
        if (input[n]==-1)
        {
            break;
        }
    }
    
    // Do something to find the highest number
    
    return 0;
}

Although that example isn't too great as it will allocate enough memory for 50 doubles (50*8=400MB) to get around that it would be a good idea to learn about dynamic memory although functions might be nice to learn before it.
Last edited on
I'm not allowed to use for loops yet because of the program I submit to but I've got it thanks. I just had to look over more information on while loops and control variables.
Actually I still have one issue. Here is my 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
30
31
#include <iostream>
using namespace std;

int main()
{
	cout << "This program finds the maximum number." << endl << endl;

	float num1, num2, greater, i;

// Get the intitial number. 
	cout << "Number: ";
	cin >> num1;

	while( i != -1 )
	{

		cout << "Number: ";
		cin >> num2;

		i = num2; //control variable

		if(num1 > num2)
			num1 = num1;
		else
			num1 = num2;
			num2 = num1; 
	}

	
	cout << "The largest number is " << num1 << ".";
}


Now the problem that I have is it still considers the signal number "-1" as a number in the problem. So if I were to enter a bunch of numbers that were all negative, < -1, then it will output -1 which would be the highest number but -1 is only supposed to be a signal not a number. What do I do with it?
The problem is that you check before you ask for input, and then you process the value without checking. Make the while loop infinite (the condition is true) and instead, have an if statement after you get the input and if it is the sentinel value, break from the loop.
Last edited on
Haha, I just solved it right after entering the question again...this is what I did:

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
#include <iostream>
using namespace std;

int main()
{
	cout << "This program finds the maximum number." << endl << endl;

	float num1, num2, greater, i;

// Get the intitial number. 
	cout << "Number: ";
	cin >> num1;

	while( true )
	{

		cout << "Number: ";
		cin >> num2;

		i = num2; //control variable

		if(i == -1)
			break;

		if(num1 > num2)
			num1 = num1;
		else
			num1 = num2;
			num2 = num1; 
	}	

	cout << "The largest number is " << num1 << ".";
}


Instead of having "while( i != -1), I put a break in right before anything actually calculates so if -1 is entered for num2 then it will just break before num1 gets the value of -1.
Topic archived. No new replies allowed.