### Smallest and Largest of N numbers..

Pages: 12
I've made a program to find the smallest and largest among the n entered numbers by the user..

But in the following code if the smallest value is greater than -1 (Sentinel Value ) the program also considers the considers value and always tells that -1 is the smallest value... Need help to overcome this problem

 ``123456789101112131415161718`` `````` #include using namespace std; void main () { int max=0,num,min=0; for (int i=0; num!=-1; i++) { cout<<"Enter the number <-1 to end input> "; cin>>num; if (num>max) max=num; if (num

But in the following code if the smallest value is greater than -1 (Sentinel Value ) the program also considers the considers value and always tells that -1 is the smallest value... Need help to overcome this problem
I would recommend adding a condition to your if statement on line 12 that checks if num is greater than or equal to zero. That way it won't be included.

Also, less importantly, why are you using a for loop? A do-while would probably be better.

Finally, `void main()` is illegal C++ that Microsoft has chosen to support because it doesn't care a bit about international standards. Please change it to `int main()`.

-Albatross
Sorry, But I didn't got what you've said about adding condition on 12 line..
i've also tried "if(num>=min)"
but that also didn't work..
Line 12 is what checks if num is new smallest number. The only thing it does is makes min equal to num. You want to prevent it from doing that when num is less than zero. So why not add a condition (with a && operator) to that if statement so that line 13 will only run when num is greater than or equal to zero?

 ``12`` ``````if(condition_1 && condition_2) //Insert stuff that will only happen if conditions 1 and 2 are true. ``````

-Albatross
Can you write the code for that point to avoid considering sentinel value as input..
 ``123456789`` ``````#include using namespace std; /*void*/ int main () // main must return int { int max=0,num,min=0; for (int i=0; num!=-1; i++) // undefined behaviour; num is used uninitialized { // ... ``````
@JLBroges
Num is initialized in the body of loop
Oop. JLBorges has a good point.

When JLBorges says "uninitialized", that means it's used before it's given a value. Although the chances of your program quitting randomly are miniscule, they're still there because num isn't given a value before it's used as a condition for your loop.

Also, I just noticed something. min is already initialized to the smallest value it can be set to, which is zero. Isn't that a bit counterproductive?

I won't write the code for you because we tend to avoid giving out solutions here. I gave you a code template that you can use. Hopefully that'll be enough.

-Alabatross
Last edited on
I am not going to touch on the -1 in the min thing because Albatross has made a good recommendation for that. I would like to say though that Albatross's recommendation of a do-while loop instead of the for loop would help. Also at the moment when the -1 issue is fixed you will still have the problem with the minimum number being 0 no matter what smallest number the user enters.
 ``12345678910`` ``````int min=0; //min is set to 0 for (int i=0; num!=-1; i++) { cout<<"Enter the number <-1 to end input> "; cin>>num; if (num

Last edited on
if i don't set min to zero, then in line 12 how will it compare that if num<min, without knowing the value of min..
And plz tell me how is num used before getting the value..
You can instead set min to the largest possible value for an integer.
http://www.cplusplus.com/reference/limits/numeric_limits/#example

-Albatross
0k, Em going to write the same program in Do-while loop... just tell me that which sentinel value should i choose, if user want to end the input.. if i choose -1 the same problem will occur again..
Another solution could be to get the first user number before entering the do-while loop and setting both the min and max to that first number
EX:
 ``1234567891011`` ``````int min = 0, max = 0, num = 0; cout<<"Enter the number <-1 to end input> "; cin >> num; //sets the min and max to the first user input number before entering the loop max = num; min = num; do { //if statements //ask for user input at bottom of loop }while(/*condition*/);``````
Last edited on
 ``1234567891011121314151617181920`` ``````#include #include using namespace std; int main() { int max = numeric_limits::min() ; int min = numeric_limits::max() ; int num ; while( cout<<"Enter the number " && cin >> num ) { if( num>max ) max = num; if( num
I've written the code but another problem has been raised.. in this program the number of iteration are known in advance (Condition of Do-While loop). I want this program to quit at the user's choice..
 ``1234567891011121314151617181920212223`` ``````#include using namespace std; int main () { int min=0, max=0, num=0,counter=1; cout<<"Enter the numbers: "; cin >> num; max = num; min = num; do { cout<<"Enter the numbers: "; cin >> num; if (num>max) max=num; if (num
while ( cin >> num && something )
plz tell me the code for that condition i didn't get your point.. :(
something like -1 != num
 ``123456789101112131415161718192021`` ``````#include using namespace std; void main () { int max=0,num=0,min=10000000;/* dude its min number it shud be max at start*/ for (int i=0; num!=-1; i++) { cout<<"Enter the number <-1 to end input> "; cin>>num; if(num==-1)break;// add this and all k if (num>max) max=num; if (num
@topnik1 why you've set min to maximum ??
Pages: 12