finding the smallest value

Hey Guys! I am very new to programming. I am trying to write a code that outputs sum, count, average, and largest and smallest number. I have ran into a problem with outputting the smallest number entered. Everything else is working fine except the smallest number. Whatever number that was entered last will be the output for the smallest number and I tried other ways, but it either comes out as the largest number entered, 0 or -999 (which is the way I have the user to end the program). I am trying to accompish this without using arrays. Any help will be appreciated. Thanks!
Here is my code:

#include <iostream>
using namespace std;

int small;

int main ( )
{
int num; //use num 10 4 6 11 15 23 18 2
int smallestNum = 0;
int largestNum = 0;
int count = 0;
int sum = 0;
float average;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(4);

do
{
cout << "Enter A Number (-999 to quit): ";
cin >> num;

if (num != -999)
{
count ++;
sum += num;
}

if (num > largestNum)
{
largestNum = num;
}
if (num != 0 && num != -999)
{
smallestNum = num;
}


}
while (num != -999);

average = (sum /(float) count);

cout << "The sum of your numbers is: " << sum << endl;
cout << "The count of numbers you entered is: " << count << endl;
cout << "The average of your numbers is: " << average << endl;
cout << "The largest number you entered is: " << largestNum << endl;
cout << "The smallest number you entered is: " << smallestNum << endl;


return 0;

}

I figured it out for the time being, I just made smallestNum a number other than 0, I made it 999 and it did out put the smallest value than, but I know there is a better way to do this
Well you could use std::numeric_limits<int>::max() (which is defined in the <limits> header) as an initial value for smallestNum.
also std::numeric_limits<int>::min() for largestNum.

I fiddled with it and came up with this. You have the same problem in the original example with large numbers as with small numbers. You just hadn't discovered that yet. The bottom line is that you need to initialize largest and smallest with the first valid value entered. 0 is not an acceptable starting point for either. If you only entered negative numbers 0 would be the largest. It is more obvious with the smallest number calculation because you are probably going to enter mostly positive numbers.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
int main ()
{
    int num; //use num 10 4 6 11 15 23 18 2
    int count = 0;
    int sum = 0;
    float average;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(4);
    
    int smallestNum(0);
    int largestNum(0);
    bool first(true);

    do
    {
	cout << "Enter A Number (-999 to quit): ";
	cin >> num;

	// If it is a valid number, perform the metrics calculations.
	if (num != -999)
	{
	    // initialize if it is the first valid number entered.
	    if(0 == count)
	    {
		smallestNum = num;
		largestNum = num;
	    }

	    if (num > largestNum)
	    {
		largestNum = num;
	    }
	    if (num < smallestNum)
	    {
		smallestNum = num;
	    }
	    count ++;
	    sum += num;
	}
    } while (num != -999); // continue until -999 entered

    if(0 != count)
    {
	average = (sum /(float) count);
	cout << "The sum of your numbers is: " << sum << endl;
	cout << "The count of numbers you entered is: " << count << endl;
	cout << "The average of your numbers is: " << average << endl;
	cout << "The largest number you entered is: " << largestNum << endl;
	cout << "The smallest number you entered is: " << smallestNum << endl;
    }
    else
    {
	cout << "You didn't enter any numbers! " << endl;
    }


    return 0;

}


R0mai's solution is a nice C++ solution too! That avoids the extra if(..) block, although there were some other changes in my example that I recommend such as checking the count after the loop ends and moving the if blocks that are checking for the smallest/largest nums within the if(num != -999) block.
Last edited on
Topic archived. No new replies allowed.