max/min/average program

This program is suppose to find the max, min, and average for an unspecified quantity of numbers. Everything works fine in the output except the min value always comes out to 0. I appreciate any help, thanks.

//Robert
// Engr 21
// 17 October 2011
// Purpose: Assignment 3: Max/min, and Average of set of numbers

/* Pseudocode:
1. User inputs unspecified quantity of real numbers ending with the sentenial value of -1 by using a while loop.
2. Program will count number of real numbers using while loop.
3. Program will use the count to caclulate the average of the numbers entered.
4. Program will find the smallest number entered by comparing values with if statemant.
5. Program will find the largest number entered by comparing values with if statement.
6. Output will show how many numbers entered, the min, the max, and the average.
7. Program will ask if user wants to run the program again using different set of numbers using while statement.

*/

#include <iostream>


using namespace std;

int main()
{

// Declare and initialize variables
char choice = 'y'; // do at least once
float num = 0; // numbers entered
float sum = 0; // sum of the numbers to find average
int count; // to count how many numbers
float min= 0; // lowest number
float max = 0; // highest number
float ave; // the average
count=0; // initialize count

cout << "Enter a set of real numbers and end by entering -1" << endl;
while (choice == 'y') //to run program again


{

cin >> num; // first set of numbers

while ( num != -1)
{

sum += num; // sum of the numbers
count++; // count how many numbers
if (num > max)
max = num;

else if (num < min)
min = num;

cin >> num; // get more numbers
}
ave = sum/count;

cout << "There were " << count << " numbers" << endl;
cout << "The highest number is " << max << endl;
cout << "The lowest number is " << min << endl;
cout << "The Average is " << ave << endl;






cout << " Do you want to run again (y/n)?" << endl; // want to run program again?
cin >> choice;
}
return 0;
}
try to input negative numbers to observe the change of min:-)
You initialize your "min" as 0, then compare new val to min val. Unless you input a number below 0 (negative number), you'll never see a change.

To initialize a minimum, use the biggest number required ("bigM"). In case of a maximum, use the smallest number required ("bigM"). The "required" part depends on the situation (i.e. expected range of input). To be quite certain, use the pre-defined value "INT_MAX" (or "-INT_MAX" for the max variable) as starting values.
closed account (D80DSL3A)
Another method is to assign the first number entered to min and max.
1
2
cin >> num; // first set of numbers
min = max = num;
Topic archived. No new replies allowed.