Comparing Negative Numbers

I am trying to create a program that compares a set of integers and outputs the min and max value. My code executes correctly for all scenarios except when I have a list of only negative answers to compare. Then the code returns the correct min value and 0 for the max value. Any thoughts?


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
# include <iostream>
using namespace std;

int main ()
{
int numInt,             // The total number of integers to be entered
    currentInt,         // The number of the current integer beig processed
    val,                // The value of the current integer being processed
    minVal,             // The integer with the smallest value
    maxVal;             // The integer with the largest value

// Gets input from the user
cout << "How many integers would you like to enter?\n";
cin >> numInt;

if (numInt == 1)
        cout << "Please enter " << numInt << " integer.\n";
else
        cout << "Please enter " << numInt << " integers.\n";

// Initializes the loop control variable
currentInt = 1;

// Sets initial min and max values

minVal == val;
maxVal == val;

// Evaluates for minValue and maxValue for each integer
while (currentInt <= numInt)
{
   // Compares the value of each entered integer to determine min and max values
   cin >> val;
   if (val < minVal)
        minVal = (val);
   if (val > maxVal)
        maxVal = (val);

   // Sets the loop to the next integer
   currentInt = currentInt + 1;
}

// Prints the largest and smallest integers
cout << "min:  " << minVal << "\n";
cout << "max:  " << maxVal << "\n";

return 0;

}
Last edited on
Please insert your code between the tags [ code ] and [ / code ] (without spaces)

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
    int howManyNumbers, minVal = 0, maxVal = 0;

    do
    {
        cout << "How many numbers? ";
        cin >> howManyNumbers;
    } while (howManyNumbers < 0);

    for(int i=0; i<howManyNumbers; i++)
    {
        int actual;
        cout << "Insert a number: ";
        cin >> actual;

        if(i==1)
        {
            minVal = actual;
            maxVal = actual;
        }

        if(actual > maxVal)
            maxVal = actual;
        if(actual < minVal)
            minVal = actual;
    }

    cout << "Max: " << maxVal << endl;
    cout << "Min: " << minVal;
Last edited on
Sorry! I am new to this forum. I'll repost.
No, you just need to "edit" the original post.
closed account (E0p9LyTq)
gr8flmommy wrote:
Sorry! I am new to this forum. I'll repost.

How to use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

You can edit your post, follow the instructions given. :)
1
2
3
4
// Sets initial min and max values

minVal == val;
maxVal == val;
that does nothing.
== is comparison
= is assignment
closed account (48T7M4Gy)
1
2
3
4
// Sets initial min and max values

minVal == val;
maxVal == val;


So you need to fix those two lines in two ways:
1) Use '=' not '==' as above

2) There are many ways of getting the ball rolling with starting values for minVal and maxVal and you need to think about them carefully.

minVal should be set at a high enough value so that from knowledge of possible responses from the user all values are guaranteed to be less than that. So pick a big number.

Use the same logic for the initial value for maxVal.
Topic archived. No new replies allowed.