dimanche 22 février 2015

C++ elseif is not printing the right answer


I have a little problem when trying to print the letter-grades. Can anybody lead me int he right direction? I think this is because of all those conditions but i haven't found the glitch yet.



Letter grades are sometimes assigned to numeric scores by using the grading scheme commonly known as grading on the curve. In this scheme, a letter grade is assigned to a numeric score as follows (where m is the mean grade and sd is the standard deviation):


Numeric Score Letter Grade


<m-(sd*1.5) F

between 0.5 and 1.5 * sd below mean D

between 0.5*sd

below and 0.5*sd above mean C

between 0.5 and 1.5*sd above mean B

>1.5*sd above mean A

The mean is the sum of all the scores divided by the number of scores. A simplified formula for standard deviation is:

sqrt((summation(x-m)*2)/n)


Write a program to read in a list of real numbers, representing numeric scores - storing them in a static array, or a dynamic array (created using new), call functions to calculate the mean and standard deviation, and then call a function to calculate and display the letter grade and corresponding numeric score.




#include <iostream>
#include <cmath>

using namespace std;

void calculate_mean_sd (int arrcal[], int &num);
void calculate_and_print_grades (int arrcal[], int num);

int main (void)
{
int arrcal[10], num=0;
cout<<"Please enter the number of scores that you'd like to determine the grades for: ";
cin>>num;
for (int i=0; i<num; i++)
{
cout<<"Please enter a score: ";
cin>>arrcal[i];
}
calculate_mean_sd (arrcal, num);
calculate_and_print_grades (arrcal, num);

return 0;
}

void calculate_mean_sd (int arrcal[], int &num)
{
double mean=0, dSum=0, sum_deviation=0, standard_dev=0;

for (int i=0; i<num; i++)
{
dSum+=arrcal[i];
}
mean=dSum/num;
cout<<"\n\nMean= "<<mean<<endl;

for (int i=0; i<num; i++)
{
sum_deviation+=(arrcal[i]-mean)*(arrcal[i]-mean);//here is a glitch, I think
}

standard_dev=sqrt(sum_deviation/num);
cout<<"Standard deviation= "<<standard_dev<<endl;
return;
}
void calculate_and_print_grades (int arrcal[], int num)
{
double mean=0, standard_dev=0;

char * gradeArray = new char[num];
for (int i=0; i<num; i++)
{
if (arrcal[i]<(mean-1.5*standard_dev))
cout<<"The letter grade is F.\n"<<gradeArray[i];

else if ((mean-(1.5*standard_dev)) <=arrcal[i] && arrcal[i]<(mean-(0.5*standard_dev)) )
cout<<"The letter grade is D.\n"<<gradeArray[i];
else if ((mean-(0.5*standard_dev)) <=arrcal[i] && arrcal[i]<(mean+(0.5*standard_dev)) )
cout<<"The letter grade is C.\n"<<gradeArray[i];
else if ((mean+(0.5*standard_dev)) <=arrcal[i] && arrcal[i]<(mean+(1.5*standard_dev)) )
cout<<"The letter grade is B.\n"<<gradeArray[i];
else
cout<<"The letter grade is A.\n"<<gradeArray[i];
}
return;
}


If I enter 5 scores, the output will be A even if I enter 70,85,90 and so on.




Aucun commentaire:

Enregistrer un commentaire