0

Please help me get the minimum in the following problem. i get everything else but the min. take a look at it.

//**************************************************
// Description : This program asks a user for five real numbers, then outputs
// the number of data entries entered, the total of the numbers
// entered, the mean average of the numbers entered, the minimum
// and maximum value entered. it will also format the output to
// one decimal place.
//**************************************************
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void)
{
double num, sum = 0, avg, max, min;
int count = 0; // loop counter variable
while (count < 5)
{
cout <<"Enter number " << count ++ << ":";
cin >> num;
sum = sum + num;
if (count == 0)
{
max = num;
min = num;
}
if (num > max)
{
max = num;
}
if (num < min)
{
min = num;
}
}
cout <<"The number of values entered is " << count << endl;
cout <<"The sum of values entered is " << sum << endl;
avg = sum / count;
cout <<"The average of values entered is " << avg << endl;
cout <<"The maximum value entered is " << max << endl;
cout <<"The minimum of value entered is " << min << endl;

system("PAUSE");
return 0;
}

2
Contributors
1
Reply
2
Views
13 Years
Discussion Span
Last Post by samaru
0

In your while loop, you're telling it to loop five times and keeping track with variable count. Once you first enter the while loop, you're immediately incrementing it with the stament count++. Even if you're using this with a cout as output, it still increments it. What does this mean? It means that once it enter, it equals 1, and your statement of

if (count == 0) {
 max = num;
 min = num;
}

will never run. All you have to do is take the "++" from the "count" variable. You then increment this variable before another loop begins. Like this:

#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void) {
 double num, sum = 0, avg=0, max=0, min=0;
 int count = 0; // loop counter variable 
 while (count < 5) {
  cout <<"Enter number " << count << ":";
  cin >> num;
  sum = sum + num;	
  if (count == 0) {
   max = num;
   min = num;
  }
  if (num > max) {
   max = num;
  }
  if (num < min) {
   min = num;
  }
  count++;
 }
 
 cout <<"The number of values entered is " << count << endl;
 cout <<"The sum of values entered is " << sum << endl;
 avg = sum / count;
 cout <<"The average of values entered is " << avg << endl;
 cout <<"The maximum value entered is " << max << endl;
 cout <<"The minimum of value entered is " << min << endl; 
 return 0;
}

Also, don't forget to initialize your variables always. In this case, you'd want to set them to zero, as I did.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.