exercise

I am a beginner and I try to solve some exercises but I've got blocked. I know that is easy but anyone can help me?
The program should read in values until a negative value is read in. It should determine the largest value that was read in and display that value once the loop stops.

This program involves indefinite iteration using a sentential controlled loop. The sentential value is any negative integer. The fact that negative integers are sententials should help you determine how to initialize the variable that contains the largest integer.
thanks
Indefinite iteration or recursion, but use iteration; all the innocent programmers here will love you for it.

Here's a snippet to get you started. It iterates until the if statement (the sentinel) is fulfilled. boolean here is an expression that evaluates to a boolean value. I think you can figure out what goes there. Remember: any number smaller than zero trips the sentinel.

1
2
3
4
5
while (true)
{
    if (boolean)
       break;
}


-Albatross
Ok, but how do I determine the biggest positive number?
include a tracker variable:
 
int biggest=INT_MIN;


then in the loop (where x is the inputed integer):
 
if (x>biggest){biggest=x;}



Sorry but I do not understand the last part. I get an input from the keypad that I store in a variable. And i do not understand how I associate this with the "biggest" variable
Biggest starts out as 0 and x is inputted. The following process will show you:
Let's say X is inputted to be: 5
x = 5
biggest = 0

We compare x to biggest, if x is bigger, we change biggest to x
x = 5
biggest = 5
LOOP CONTINUES
X gets inputted.. for example: 17
x = 17
biggest = 5

Since 17 > 5, biggest becomes 17 (x)
x = 17
biggest = 17
LOOP CONTINUES
X gets inputted, a smaller number, this time: 13
x = 13
biggest = 17

Since x is not bigger than 17 (biggest), 17 is still the biggest, and the value of biggest does not change.
x = 13
biggest = 17
et cetera, et cetera
I wrote this code and is everthing ok until I writ in the numbers, when I write in negative numbers it stops but for the "biggest value it gives me 0. What could be wrong?
#include<iostream>
using namespace std;

int main ()
{
int numar,biggest = 0;
cout<< " Insert an integer ";
while(numar > 0)
cin>>numar;
if (numar>biggest)
biggest = numar;
cout<<biggest;
system ("pause");
return 0;
}
You forgot a few brackets around lines 9-11, at first glance.

EDIT: Also, you were specifically told to use indefinite iteration with the use of a sentinel. You might lose points on your assignment with your current method.

-Albatross
Last edited on
Probably you are right but I have no idea what " indefinite iteration using a sentential controlled loop" means. have to learn by myself because I have no other possibility and when you learn from a book is nobody there to explain you if you don't know something.
I gave you a snippet that if you used would be an indefinitely iterating loop using a sentinel. :P

-Albatross

EDIT: I am so 1337.
Last edited on
I changed the code into this
#include<iostream>
using namespace std;

int main ()
{
int numar, biggest = 0;
cout<<"insert an integer ";
while( numar >= 0)
cin>>numar;
if(numar<biggest)
{
biggest = numar;
}
cout<<biggest;
system("pause");
return 0;
}
and now if I enter the integers and the value -1 it gives me the result -1
I am closer but should give me the biggest value not the negative
No.... read my posts (all of them) again. You did something completely different and you still don't have a sentinel controlled loop.

Oh, and [code] tags plz.

-Albatross
Topic archived. No new replies allowed.