# C++ how to find min/max of entered numbers

Page 1 of 1

## 7 Replies - 12781 Views - Last Post: 12 May 2009 - 01:17 PMRate 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=94698&amp;s=163e59103c317b52d2612d4b13641f84&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lifesgood929

Reputation: 0
• Posts: 1
• Joined: 22-March 09

# C++ how to find min/max of entered numbers

Posted 22 March 2009 - 07:15 PM

I need to find the min/max and average of a series of entered numbers from the user.

This is what I have so far, but I keep getting weird values for my min/max and avg.
```// Max, Min, Average of Number String
// Shawn Cho
// March 20, 2009
// ENGR 021 Monday 6:30-9:40 PM
// PURPOSE: Find the max, min and average of a number string.
/*
PSEUDOCODE For This Program:
1. Include preprocessors needed
2. Ask users to input date as dd mm yyyy.
3. Echo out input back to user.
4. Write out if/else statements for converting mm to certain month.
5. If month is greater than 12 or less than 1, error out.
6. Write out if/else statements for converting dd to days.
7. Write more if/else statements for "st, nd, rd, th".
8. Echo out the output to the user in correct format.
*/

#include <iostream>
#include <conio.h>

using namespace std;

float n;
float x;
float q;
float min;
float max;
float avg;
int count = 1;

int main ()
{
float n;
float x;
float q;
float min;
float max;
float avg;
int count = 1;

cout << "Please enter numbers one at a time, pressing return after each one." << endl;
cout << "When you want to stop, just enter 0 (a zero). ";

cin >>x;

n = x;

while (x != 0 && count < 100)
{
cin >>x;
if (x == 0)
break;
if (x > q)
x = max;
if (x < q)
x = min;
q = x;
n = n + x;
count++;
}

avg = n/count;

cout << "Thank you." << endl;
cout << "You entered " << count << " numbers.";
cout << "The largest number is " << max << endl;
cout << "The smallest is " << min << "."<< endl;
cout << "The average is " << avg << "." << endl;

getch (); return 0;
}

```

thanks!

Is This A Good Question/Topic? 0

## Replies To: C++ how to find min/max of entered numbers

### #2 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: C++ how to find min/max of entered numbers

Posted 22 March 2009 - 08:51 PM

```#include <iostream>
#include <conio.h>

using namespace std;

int main ()
{
// initialize variables
float n = 0;
float x;
//	float q = 0;
float min = 0;
float max = 0;
float avg = 0;
int count = 1;

cout << "Please enter numbers one at a time, pressing return after each one." << endl;
cout << "When you want to stop, just enter 0 (a zero). ";

//	cin >>x;
//	n = x;
//	while (x != 0 && count < 100)
// no need to check "x" since you break out of the while() loop
// with the if() test

while (count < 100)
{
cin >> x;

n += x;  // update the running sum first since it's used by avg

if (x == 0)
break;

if (x > max)
max = x;

if (x < min)
min = x;

count++;
}

avg = n/count;

cout << "Thank you." << endl;
cout << "You entered " << count << " numbers.";
cout << "The largest number is " << max << endl;
cout << "The smallest is " << min << "."<< endl;
cout << "The average is " << avg << "." << endl;

getch (); return 0;
}

```

This post has been edited by n8wxs: 22 March 2009 - 08:56 PM

Reputation: 0
• Posts: 1
• Joined: 12-May 09

## Re: C++ how to find min/max of entered numbers

Posted 12 May 2009 - 09:41 AM

the working of program is not correct!
as the zero is ultimately intrusion into the users input and thus becoming part of number of elements entered by user
whether user wants or not.
this could be resolved if count should be equal to zero
the actual input is correct then without including zero in it
but the minimum is always zero for the trick involve in the coding that is input taken by user to stop further inputs and thus providing zero as minimum

can this issue be resolve logically???

### #4 Mowgef

Reputation: 11
• Posts: 245
• Joined: 01-May 09

## Re: C++ how to find min/max of entered numbers

Posted 12 May 2009 - 11:18 AM

``` #include <iostream>
#include <conio.h>

using namespace std;

int main ()
{
// initialize variables
float n = 0.0;
float x;
//	float q = 0;
float min = 0.0;
float max = 0.0;
float avg = 0.0;
int count = 0;

cout << "Please enter numbers one at a time, pressing return after each one." << endl;
cout << "When you want to stop, just enter 0 (a zero). ";

//	cin >>x;
//	n = x;
//	while (x != 0 && count < 100)
// no need to check "x" since you break out of the while() loop
// with the if() test

while (count < 100)
{
cin >> x;

n += x;  // update the running sum first since it's used by avg

if (x == 0.0)
break;

if (x > max)
max = x;
if (min == 0.0)
min = x;
if (x < min)
min = x;

count++;
}
if(count > 0.0)
avg = n/count;

cout << "Thank you." << endl;
cout << "You entered " << count << " numbers.";
cout << "The largest number is " << max << endl;
cout << "The smallest is " << min << "."<< endl;
cout << "The average is " << avg << "." << endl;

getch (); return 0;
}

```

How about something like this? Might have missed something cause I'm in a rush but it should work this way.

### #5 baavgai

• Dreaming Coder

Reputation: 6129
• Posts: 13,237
• Joined: 16-October 07

## Re: C++ how to find min/max of entered numbers

Posted 12 May 2009 - 11:36 AM

The "min" isn't initialized correctly in any offering.

This should do it.

```#include <iostream>

using namespace std;

int main () {
cout << "Please enter numbers one at a time, pressing return after each one." << endl;
cout << "When you want to stop, just enter 0 (a zero). ";

int count = 0;
float min, max, total = 0;

for (;;)/>{
float x; cin >> x; if (x==0) { break; }
total += x;
if (count++ == 0) {
min = max = x;
} else {
if (x<min) { min=x; }
if (x>max) { max=x; }
}

}
cout << endl << "Thank you." << endl;
cout << "You entered " << count << " numbers." << endl;
cout << "The largest number is " << max << endl;
cout << "The smallest is " << min << "."<< endl;
cout << "The average is " << (total/count) << "." << endl;

return 0;
}

```

### #6 Mowgef

Reputation: 11
• Posts: 245
• Joined: 01-May 09

## Re: C++ how to find min/max of entered numbers

Posted 12 May 2009 - 12:05 PM

Yeah baav I like yours more. Will mine not work though?

### #7 baavgai

• Dreaming Coder

Reputation: 6129
• Posts: 13,237
• Joined: 16-October 07

## Re: C++ how to find min/max of entered numbers

Posted 12 May 2009 - 01:05 PM

Mowgef, on 12 May, 2009 - 01:05 PM, said:

Yeah baav I like yours more. Will mine not work though?

Apologies, I didn't spot the "if (min == 0.0) min = x;" in there. Yep, yours should work fine.

Well, almost. If I enter all negative numbers, your max will be off.

### #8 Mowgef

Reputation: 11
• Posts: 245
• Joined: 01-May 09

## Re: C++ how to find min/max of entered numbers

Posted 12 May 2009 - 01:17 PM

yeah for some reason I didn't think of neg numbers