Hello, I am having a problem with this program, I am supposed to enter a series of numbers, and after entering the numbers I need to display the largest and smallest number. But I don't know how to do that, if anyone can please tell me how, it would be much appreciated.
#include <iostream>
usingnamespace std;
int main()
{
double numbers,
max_num,
min_num,
total;
cout << " Enter a series of numbers. When you are finished entering the numbers of\n";
cout << " your desire, enter -99 to stop the program. I will then pick the largest\n";
cout << " and the smallest numbers you have entered." << endl;
while(numbers != -99)
{
total += numbers;
cout << " Enter your number(s): ";
cin >> numbers;
}
return 0;
}
I don't think I am up to that sort of thing yet.. And I just need to display the largest number and smallest number, I don't think I need to sort them, but if it helps I'll look into it. Thank you.
You could try using an If statement to have it place the current max and min values while in the loop. That way you don't have to use an array if you haven't got to that point yet.
You don't need to sort them, and you don't need to store them in an array, or at all. You can store them in array if you want, or any other container, and if it's your prerogative you can choose to sort them, but the simplest way to fulfill the requirements would be to have a variable for make number, and min number, as you do, and as you get a number, compare it to the smallest, if it is smaller, set it to be the new smallest, then do the same to the largest.
Also, set largest and smallest to be the first number entered, so you are guaranteed to have a valid value at the start.
Edit: Why are you keeping a total? Also, have them enter the numbers 1 at a time.
#include <iostream>
usingnamespace std;
int main()
{
double numbers = 0, //series of numbers
max_num, //maximum number
min_num; //minimum number
cout << " Enter a series of numbers. When you are finished entering the numbers of\n";
cout << " your desire, enter -99 to stop the program. I will then pick the largest\n";
cout << " and the smallest numbers you have entered.\n\n";
cout << " Enter your number(s): ";
cin >> numbers;
max_num = numbers;
min_num = numbers;
while(numbers != -99)
{
cout << " Enter your number(s): ";
cin >> numbers;
if(max_num > numbers)
{
cout << " The largest number is " << max_num << endl;
}
}
return 0;
}
But there is still problems with this and its only for the max number at the moment
#include <iostream>
usingnamespace std;
int main()
{
double numbers = 0, //series of numbers
max_num, //maximum number
min_num; //minimum number
cout << " Enter a series of numbers. When you are finished entering the numbers of\n";
cout << " your desire, enter -99 to stop the program. I will then pick the largest\n";
cout << " and the smallest numbers you have entered.\n\n";
min_num = numbers;
max_num = numbers;
while(numbers != -99)
{
cout << " Enter your number(s): ";
cin >> numbers;
if(max_num < numbers)
{
max_num = numbers;
}
cout << " The largest number is " << max_num << endl;
}
system("pause");
return 0;
}
The problem you had before when it didn't update the max number was because max_num was assigned numbers before the while loop, but not during it. So it only changed it once. I also moved the largest number cout out of the if statement, because it would only display the highest number if you put in the highest number (That is optional). One last thing I did was get rid of the repetitive "Enter your numbers" cout, you can do exactly the same thing as before with cleaner code.
Ok, so I was able to polish up the program and get the maximum number, but when I enter -99 to stop the loop, I get -99 as the smallest number when it was supposed to act as the sentinel. This is what I've done..
#include <iostream>
usingnamespace std;
int main()
{
double numbers = 0, //series of numbers
max_num, //maximum number
min_num; //minimum number
cout << " Enter a series of numbers. When you are finished entering the numbers of\n";
cout << " your desire, enter -99 to stop the program. I will then pick the largest\n";
cout << " and the smallest numbers you have entered.\n\n";
while(numbers != -99)
{
max_num = numbers;
min_num = numbers;
cout << " Enter your number(s): ";
cin >> numbers;
if(max_num < numbers)
{
max_num = numbers;
}
if(min_num > numbers)
{
min_num = numbers;
}
}
cout << " The largest number is " << max_num << endl;
cout << " the smallest number is " << min_num << endl;
return 0;
}
do
{
your code
cout<<"Any more numbers? (Y/N)"
cin>>answer;
}while(answer=='Y' || answer=='y');
because your using a while loop, you typed in -99 but it still executed the remainder of the code untill it started at the top again then it terminated when it seen numbers = -99. Therfore storeing -99 into the min_num
#include <iostream>
usingnamespace std;
int main()
{
double numbers = 0, //values entered by user
max_num = INT_MIN, //maximum number
min_num = INT_MAX; //minimum number
cout << " Enter a series of numbers. When you are finished entering the numbers of\n";
cout << " your desire, enter -99 to stop the program. I will then pick the largest\n";
cout << " and the smallest numbers you have entered.\n\n";
do
{
cin >> numbers;
if(numbers > max_num)
{
max_num = numbers;
}
if(numbers < min_num && numbers != -99)
{
min_num = numbers;
}
}while(numbers != -99);
cout << " Largest: " << max_num << endl;
cout << " Smallest: " << min_num << endl;
return 0;
}
Edit: This works just fine now, thank you for the help :D