I am trying to calculate the standard deviation and average of students in a txt file. I have the average down but I am having trouble using a function to calculate the standard deviation of the 5 grades. My prototype is void stats(double,double,double,double,double,double&ave,double&sd); I am stuck at this point. I don't know output my standard deviation or if i have my void function. Thanks
#include <iostream>
using namespace std;
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
float getValue(char);
void stats(double&grade1,double&grade2,double&grade3,double&grade4,double&grade5,double&ave,double&sd);
int main() {
cout << "Name\t\tAverage\t\tDeviation\n";
string name;
int count;
char grade;
float ave,sum;
ifstream input;
input.open("data.txt");
input >>name;
while (name!="XXX") {
cout<<name<<"\t"<<"\t";
sum=count=0;
input>>grade;
while (grade!= 'X' ){
sum+=getValue(grade); //sum = sum+grade
count++;
input>>grade;
}
if (count>0)ave=sum/count;
else cout<<"no average";
cout<< setprecision (2)<<fixed <<ave<<"\n";
input>>name;
}
return 0;
}
float getValue(char x){
float ans=0;
switch(x){
case 'A': ans=4.0;break;
case 'B': ans=3.0;break;
case 'C': ans=2.0;break;
case 'D': ans=1.0;break;
}
return ans;
}
void stats(double&grade1,double&grade2,double&grade3,double&grade4,double&grade5,double&ave,double&sd)
{
double var;
var=(pow((grade1-ave),2)+pow((grade2-ave),2)+pow((grade3-ave),2)+pow((grade4-ave),2)+pow((grade5-ave),2))/5;
sd=sqrt(var);
}
std::vector? A vector would make the average and standard deviation easier to implement. - Thomas Matthewssumandcount. Both are easy given what you have. - Alan Stokesvoid. (Either return the answer, or assign it tosd- not both.) - Alan Stokes