Noob question

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.

This is what I've done so far..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace 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;
}
You'll need to put the numbers into an array, so that you can use an algoritm to sort it from there on.

I'd reccomend the bubble sort:

http://www.algolist.net/Algorithms/Sorting/Bubble_sort
Last edited on
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.
Last edited on
After revising my program I came up with this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace 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
Don't print to the screen when you get a larger one. Wait until the user has entered every number, then print it out.
I tried to create what you wanted, I'm not sure if this is accomplishing it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace 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..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace 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;
}


Not Sure if the min_num part is correct
Last edited on
try


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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
Last edited on
After much contemplating I was able to solve it :D
Thanks to Max Edgar idea, this is what I came up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace 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
Last edited on
Topic archived. No new replies allowed.