Max, Min of a set of numbers

Hey guys,
I have went through a bunch of the posts relating to this topic but I just don't seem to understand half of what is going on. So I decided to make my own topic and hope someone can help me out.

Basically I have to write a program that asks the user how many integers they want to input (say 5). Afterwards I will have them input 5 random integers of their choosing. This I have done and understand however what I do not understand is how to have the program look at those 5 numbers and choose the maximum number and minimum number. I cannot use an array by the way I believe I need to use a loop for this and have been trying but I just don't understand the idea behind max and min I suppose. It seems simple but writing a program for that has stumped me.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main()
{
  int min, max, number, integer;  
  
  cout << "How many integers would you like to enter?" << endl;
  cin >> number;
  
  cout << "Please enter " << number << " integers" << endl;
  cin >> integer;
 
Last edited on
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

#include <iostream>

int main(int argc, char* argv[])
{
	signed int min = INT_MAX;
	signed int max = INT_MIN;

	unsigned int nTime = 0;

	std::cout << "How many integers would you like to enter?\n";
	std::cin >> nTime;

	while (nTime--)
	{
		std::cout << "Please enter " << nTime + 1 << " integers\n";

		signed int temp;
		std::cin >> temp;

		if (temp <= min) min = temp;
		if (temp >= max) max = temp;
	}

	std::cout << "\nMin value is " << min;
	std::cout << "\nMax value is " << max;

	return 0;
}
I appreciate the reply but I have to be honest. I don't understand half of what you wrote. I'm extremely new to c++ and the things you wrote within your main function I have never seen.
Lets go through Ericool's code.

Line 6 and 7 are self explanatory, they store the minimum number and the maximum number.

Line 9 declares the variable nTime which stores how many different numbers you want.

Line 11 and 12 gets user input and puts it into nTime.

I'm assuming the while loop at Line 14 loops until nTime is equal to 0, but it would be clearer to do :

1
2
3
4
while (nTime != 0) {
    nTime--;
    //Rest of loop
}


Line 18 creates a temporary variable temp which then on Line 19 gets user input for the current integer.

Line 21 and 22 then check if the temp variable is bigger or smaller than the current minimum and maximum numbers. If the temp is smaller than the minimum number, the new minimum is set to 'temp'. The same goes for the maximum number except it checks if 'temp' is bigger.

Finally when nTime equals 0, it exits the loop, printing out the numbers.

P.S. I'm a new programmer too, so anything i've said may or may not have been completely correct.

everything is fine :)
Topic archived. No new replies allowed.