# correct largest number but not smallest number

Page 1 of 1

## 7 Replies - 872 Views - Last Post: 11 July 2011 - 02:15 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=238814&amp;s=ab21b4433224135164cf852272dc89f9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 clickspiker23

Reputation: 5
• Posts: 200
• Joined: 29-October 10

# correct largest number but not smallest number

Posted 09 July 2011 - 09:58 PM

Im trying to get the smallest number out of a series of user generated numbers.
i can correctly get the largest number but i always get the negative 99 for the smallest.
im working out of a c++ book that does not provide solutions, i tried to catch the -99 but it didn't work
as you can see i commented it out.

i know someone is going to suggest it (dont use a neg number or try an array)
but the challenge says to do it WITHOUT using arrays and i HAVE to use -99 to quit.

any suggestions

```int main()
{
int counter, input, largest = 0, smallest = 0;

cout << "Please enter a series of numbers so i can find the largest and smallest." << endl;
cout << "Enter -99 to indicate you are done entering numbers." << endl;

do
{

cout << "number: ";
cin >> input;

//        if (input == -99)
//        {
//            input = 0;
//        }

if (input < smallest)

{
smallest = input;
}

if (input > largest )
{
largest = input;
}

} while (input != -99);

//always gives -99
cout << "After searching the smallest is: " << smallest << endl;
// correct largest
cout << "After searching largest is: " << largest << endl;

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: correct largest number but not smallest number

### #2 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: correct largest number but not smallest number

Posted 09 July 2011 - 10:24 PM

Generally you want to initialize the largest and smallest values with the first numbers you are testing.

Since you seem to have a counter:
```#include <iostream>
#include <limits>
using namespace std;

int main() {
int counter = 0, input, largest, smallest;
cout << "Please enter a series of numbers so i can find the largest and smallest." << endl;
cout << "Enter -99 to indicate you are done entering numbers." << endl;

do {
cout << "number: ";
cin >> input;
if (input != -99)
{
if (input < smallest || counter==0) {
smallest = input;
}

if (input > largest || counter==0) {
largest = input;
}
counter++;
}
} while (input != -99);

//always gives -99
cout << "After searching the smallest is: " << smallest << endl;
// correct largest
cout << "After searching largest is: " << largest << endl;
return 0;
}

```

Or... you can initialize smallest with the maximum value an integer can hold and largest with the minimum value:
```#include <iostream>
#include <limits>
using namespace std;

int main() {
int counter = 0, input, largest = numeric_limits<int>::min(), smallest = numeric_limits<int>::max();
cout << "Please enter a series of numbers so i can find the largest and smallest." << endl;
cout << "Enter -99 to indicate you are done entering numbers." << endl;

do {
cout << "number: ";
cin >> input;
if (input != -99)
{
if (input < smallest) {
smallest = input;
}

if (input > largest) {
largest = input;
}
counter++;
}
} while (input != -99);

//always gives -99
cout << "After searching the smallest is: " << smallest << endl;
// correct largest
cout << "After searching largest is: " << largest << endl;
return 0;
}
```

BTW... notice that I put the logic inside of the "if (input!=-99) { }" block. This ensures that when input IS -99 there is no attempt to assign it to smallest or largest

### #3 clickspiker23

Reputation: 5
• Posts: 200
• Joined: 29-October 10

## Re: correct largest number but not smallest number

Posted 10 July 2011 - 07:52 PM

I went with the first block of code as i am not familiar with lines that were used to initialize smallest and largest. one question i have
though is the counter. originally i tried to use it to keep rack of the amount of numbers entered by the user and subtract the last number input which would be -99 (kind of like ignore it) but could not get it to work. so i kind of understand what it is for but i dont understand how incrementing it in the code block affects it. i wanted to see and left it out of the code and it gives the same number for smallest and largest, why is that?

This post has been edited by macosxnerd101: 10 July 2011 - 08:33 PM
Reason for edit:: Please do not quote massive posts

### #4 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: correct largest number but not smallest number

Posted 10 July 2011 - 09:36 PM

So. read though the logic of the code.

Counter starts off at 0.
keeping that in mind we enter the do loop and get an input (say 10)
we check the if statement (input != -99) well 10 != 99 so that is true and we enter the body

next we check the first inner-if the first part is hard to evaluate because "smallest" is uninitialized. so we don't know if input < smallest.

next part "OR counter == 0" -- well counter DOES == 0 SO that is true.

so we have if(??? || true) { ... }

handy dandy truth table:
true OR true == true
false OR true == true

So we don't know if input < smallest but we DO know that the overall condition there is true so we enter the body of the if statement:

smallest = input

The same logic works for the next if-statment. We don't know what the value of largest is (because we left it uninitialized) BUT we do know that counter==0 and therefore the overall statement is true:

largest = input

counter++ --> counter = 1 (and is no longer 0)

### #5 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,997
• Joined: 30-May 10

## Re: correct largest number but not smallest number

Posted 10 July 2011 - 10:09 PM

### #6 sunilchintu2468

Reputation: 3
• Posts: 37
• Joined: 29-May 11

## Re: correct largest number but not smallest number

Posted 10 July 2011 - 10:11 PM

Remove your do while loop and paste
the following code there, it wil work

```        cout << "number: ";
cin >> input;

while (input != -99)
{

//        if (input == -99)
//        {
//            input = 0;
//        }

if (input < smallest)

{
smallest = input;
}

if (input > largest )
{
largest = input;
}
cout << "number: ";
cin >> input;

}

```

### #7 clickspiker23

Reputation: 5
• Posts: 200
• Joined: 29-October 10

## Re: correct largest number but not smallest number

Posted 11 July 2011 - 12:09 AM

Salem_c, on 10 July 2011 - 10:09 PM, said:

as i am new to c++ i decided i would try a few forums and see which seemed to be the most helpful. when i first started no one said "hey by the way use this forum because its the best" i had to look around for myself. so i decided i would try a few out. dream in code has so far proven to have the most helpful answers and people willing to help. daniweb and dream in code were not the only two that i posted some of the same questions.

### #8 sunilchintu2468

Reputation: 3
• Posts: 37
• Joined: 29-May 11

## Re: correct largest number but not smallest number

Posted 11 July 2011 - 02:15 AM

clickspiker23, on 11 July 2011 - 12:09 AM, said:

Salem_c, on 10 July 2011 - 10:09 PM, said:

as i am new to c++ i decided i would try a few forums and see which seemed to be the most helpful. when i first started no one said "hey by the way use this forum because its the best" i had to look around for myself. so i decided i would try a few out. dream in code has so far proven to have the most helpful answers and people willing to help. daniweb and dream in code were not the only two that i posted some of the same questions.

Dude you can't just do that okay. If you really struck and not getting answers in any forums, then post
simply after getting such a helpful suggestions also please don't post. All making time to help others who really got struck.