7 Replies - 11116 Views - Last Post: 12 May 2009 - 01:17 PM Rate Topic: -----

#1 lifesgood929  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
}



Can somebody please help me?
thanks!

Is This A Good Question/Topic? 0
  • +

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

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

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

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

Posted 22 March 2009 - 08:51 PM

Try this instead:
#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

Was This Post Helpful? 0
  • +
  • -

#3 sahar arshad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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???
Was This Post Helpful? 0
  • +
  • -

#4 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • 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. :P

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;
}


Was This Post Helpful? 0
  • +
  • -

#6 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

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

Posted 12 May 2009 - 01:05 PM

View PostMowgef, 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. ;)
Was This Post Helpful? 0
  • +
  • -

#8 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • 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 :rolleyes:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1