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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace 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<min)
min=num;
}
cout<<"largest number is: "<<max << endl;
cout<<"smallest number is: "<<min << endl;
}
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().
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?
1 2
if(condition_1 && condition_2)
//Insert stuff that will only happen if conditions 1 and 2 are true.
#include <iostream>
usingnamespace 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
{
// ...
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.
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.
1 2 3 4 5 6 7 8 9 10
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<min) /*the if condition will always compare 0 (as min is initialized)
to the user input and the user can never input lower than 0 */
min=num;
}
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..
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:
1 2 3 4 5 6 7 8 9 10 11
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*/);
#include <iostream>
#include <limits>
usingnamespace std;
int main()
{
int max = numeric_limits<int>::min() ;
int min = numeric_limits<int>::max() ;
int num ;
while( cout<<"Enter the number <any non-numeric character to end input> " &&
cin >> num )
{
if( num>max ) max = num;
if( num<min ) min = num;
}
cout << "largest number is: " << max << '\n'
<< "smallest number is: " << min << '\n' ;
}
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..
#include <iostream>
usingnamespace 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<min)
min=num;
counter++;
}while(counter<=10);
cout<<"Smallest Number is: " << min<<endl;
cout<<"Largest Number is: " <<max << endl;
return 0;
}
#include <iostream>
usingnamespace 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<min)
min=num;
}
cout<<"largest number is: "<<max << endl;
cout<<"smallest number is: "<<min << endl;
}