0
votes

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);

}
2
Just implement the simple equation - it is widely know and can be googled - Ed Heal
Are you allowed to use std::vector? A vector would make the average and standard deviation easier to implement. - Thomas Matthews
@ThomasMatthews no we have not done that yet. - hopkinslax
You need to assign values to sum and count. Both are easy given what you have. - Alan Stokes
And you can't return a value from a function you declared to return void. (Either return the answer, or assign it to sd - not both.) - Alan Stokes

2 Answers

1
votes

Delete the line starting #define. It doesn't do what you think it does, and is causing your mysterious error message.

0
votes

First of all, you are using the wrong formula for the standard deviation. Have a look at the page on Wikipedia! The right order of the operations is:

  1. Calculate the difference between the grade and the average
  2. Square each of the differences that you have just calculated.

Now you have to tale the average of these numbers, therefore:

  1. Sum all of them
  2. Divide the sum by 5 (in this case, of course!)

This is your variance. And finally,

  1. Take the square root of the variance.

Now you have the standard deviation.

What you are doing is different, because you have reverted step 2 and 3: first you are summing all the results, then your are squaring the sum. Unfortunately, it isn't the same, as (a+b)^2 = a^2 + 2ab + b^2 which is obviously different from a^2 + b^2. This means that when you need to take the sum of some squares you can't take the square of the sum.

That said, you have some confusion in your code:

float sum, count,var;
ave=sum/count;

You don't need sum here, and count in this case is fixed (it's 5), so you don't need that either. And ave is passed as argument, so you don't have to declare it again (and neither to calculate it - if you are passing it as argument, you have already calculated it before calling the function!).