Need help identifying smallest and largest numbers

So I have just started working with C++ this semester, so I'm fairly new to working with all of the concepts. Our most recent assignment asks us to output the sum of 5 entered numbers, the smallest and largest numbers input, and how many times the number 10 was input. All input values are to be input in a for loop. I've figured out how how to add the numbers, and how to show how many times the number 10 was input, but I'm really having difficulty identifying and outputting the smallest and largest numbers. Can someone please give me some insight? I would really appreciate it. Here's the code that I have already written if it helps:

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
//Assignment 6

#include <iostream>
#include <iomanip>
using namespace std;

int main(){
	float num, i, sum, ten, small, large;

	sum = 0;
	ten = 0;
	small = 0;
	large = 0;

	for (i = 0; i < 5; i++){
		cout << "Input a number: ";
		cin >> num;
		sum += num;
		if (num == 10){
			ten++;
		}
	}
	
	cout << "The sum of all entered numbers is " << sum << endl;
	cout << "The largest number entered is " << large << endl;
	cout << "The smallest number entered is " << small << endl;
	cout << "The number 10 was entered " << ten << " times" << endl;

	return 0;
}
Last edited on
Start with the highest number being the lowest possible number and the lowest number being the highest possible number (lines 12 and 13).

As you process each number, compare it to each of the highest and lowest values you have saved - if it is higher than the highest or lower than the lowest, update them accordingly.

At the end, you will know for a fact that you have discovered the highest and lowest numbers.
study this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 int main()
{
    int smallest, largest, i, number;

    for ( i = 0; i < 5; i++ )
   {
        cin >> number;
        if (i == 0)
             smallest = largest = number; //you need this only once, to get you 
                                                            //going.
        if ( largest >= number )
            largest = number;
        if ( smallest > number )
            smallest = number;
    }
     cout  << "smallest =" << smallest << " largest= " << largest << endl;
    return 0;
I'm not sure if I understand. I tried this code, but both outputs are the smallest number that I entered. For example: if the smallest number I enter is 2, then both outputs are 2. Why is this?

[EDIT]
I just ran your code and nothing happened after I entered the two numbers.
Last edited on
it seems that you do not want to study ,yu just want the complete solution. ;) Oh well, play a little bit with the inequality symbol line 11 and you will find it. ok? :)
Ahh ok. Thank you. Sorry about that, I'm pretty impulsive. I really appreciate the help!
Topic archived. No new replies allowed.