Hi guys, I need help with a program I have been scratching my head over in which I can't seem to realize the problem. I have a program where a user enters 10 scores, and C++ outputs mean, min, and max. I got the min and max part right but the average just will not output the right value. It is always 0 or the max value...any help is appreciated.
//Declare variables
float score[10],avg,min,max,sum=0;
int count=1,i;
//Run loop
for(i=count;i<=10;i+=1){
cout<<"\nEnter score "<<i<<": ";
cin>>score[i];
}
//Compute data
for(i=count;i<=10;i+=1){
sum = sum + score[i];
}
//Calculate average
sum = (sum/10.0);
//Calculate max and display
max=score[1];
for(i=count;i<=10;i+=1){
if(max<=score[i]){
max = score[i];
}
}
//Calculate min and display
min=score[1];
for(i=count;i<=10;i+=1){
if(min>=score[i]){
min = score[i];
}
}
//Display inputs and calculations
cout<<"Using values: ";
for(i=count;i<=10;i+=1){
cout<<score[i]<<", ";
}
cout<<"\nThe average is: "<<avg<<"\nThe \
minimum is: "<<min<<"\nThe maximum is: "<<max;
return(0);
}
#include <iostream>
int main() {
// program where a user enters 10 scores, and outputs mean, min, and max.
constint NSCORES = 10 ; // number of scores
// accept the first score
double score ;
std::cout << "enter score: " ;
std::cin >> score ;
// initially, the min and max is he first score
double min = score ;
double max = score ;
double sum = score ; // the sum so far is just the first score
// for the remaining NSCORES-1 scores
for( int i = 0 ; i < (NSCORES-1) ; ++i )
{
// read n the score
std::cout << "enter score: " ;
std::cin >> score ;
sum += score ; // add this score to the sum
// if this score is smaller than the minimum so far,
if( score < min ) min = score ; // then this becomes the minimum
// if this score is larger than the maximum so far,
if( score > max ) max = score ; // then this becomes the maximum
}
// compute the average score
constdouble average = sum / NSCORES ;
// print out the results
std::cout << "# scores: " << NSCORES << '\n'
<< " sum: " << sum << '\n'
<< " average: " << average << '\n'
<< " minimum: " << min << '\n'
<< " maximum: " << max << '\n' ;
}
Thank you for the reply @JLBorges. But boy did my professor teach C++ in a different way! She taught us in the most basic form and I never really understood the std:: part of it. This is mostly due to me being in an engineering course, not comp sci. (They require engineers to learn a little C++...)
Appreciate the effort though,