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.
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
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.
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 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