-
Min/Max Numbers
Hi all,
I just started a C++ class and have figured out that I can't quit my day job! I have a simple problem - figure out the average, minimum, and maximum numbers for a list of numbers that I enter with -1 to end the listing.
I have been struggling for 3 days on how to get the min & max numbers. I have tried a few diferent things, but I just can't think it out in my head in a logical code way!
Now I can't even get the average to work - it WAS working before I started in again trying to do the min & max.
Any assistance would be greatly appreciated. I don't want anyone to do it for me, but an explanation of how I can accomplish this would be great! The code I have altered at least 50 times is below.
thanks mz
Code:
//"This program calculates the average score, minimum score and maximum score."
#include <iostream.h>
void main()
{ float tscore=0.0, sumscore = 0.0, count = 0.0, average;
// int minscore, maxscore;
cout <<"Enter a list of positive values (ending with -1):";
cin >> tscore;
// for (maxscore=minscore=tscore; tscore > -1;); {
// if (tscore < minscore)
// minscore = tscore;
// else if (tscore > maxscore)
// maxscore = tscore; }
while (tscore > 0.0)
sumscore = sumscore + tscore;
count = count + 1;
cin >> tscore;
average = sumscore / count;
cout << "The average is :" <<average << endl;
// cout << "The minimum score is :" <<minscore << endl;
// cout << "The maximum score is :" <<maxscore << endl;
}
PS It is always good to try new things, if for nothing else, to see how some things just aren't for you.
:confused:
-
Re: Min/Max Numbers
Quote:
Originally posted by mzt
Code:
for (maxscore=minscore=tscore; tscore > -1;); {
// if (tscore < minscore)
// minscore = tscore;
// else if (tscore > maxscore)
//
sorry but i didnt understand this code
why didnt you use while instead of for??
trythe following code
Code:
#include<iostream>
using namespace std;
int main()
{
int count=0;
double score,sum,av,max,min; sum=0.0;
cout<<"enter student's score : "<<endl;
cin>>score;
max=min=score;
while(score>0)
{
if(score>max)
max=score;
if(score<min)
min=score;
sum+=score;
count++;
cout<<"enter student score :";
cin>>score;
}
av=sum/count;
cout<<"average :"<<av <<endl;
cout<<"maximum : "<<max<<endl;
cout<<"minimum : " <<min<<endl;
}
return 0;
}
i didnt run it on my compiler but i hope it works