View Single Post
Old 09-26-2005, 09:21 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Now I have more time.
Four things:
1) What you want to do is...
3/2.0
and...
1/2.0

2) The formula for grade "C" isn't correct implemented:
Yours:
Code:
if (grd[s] >= displaymn - (1/2.0 * displaystddev) && grd[s] < displaymn + (1/2.0 * displaystddev)) 
  cout << 'C'<<' ';
But the comment says:
Code:
//m - 1/2(sigma) <= x < m - 1/2(sigma)   --> C
So for the second part of the formula you switched "-" with a "+".

3) Don't call variables x, y and whatever. These variables say nothing. You need to learn to give variables and functions proper names. Don't forget that you communicate through them.

4) Your grade calculation program does two big things: it translates the grades and views them. Let that function do only ONE thing and that is to calculate a grade.
The loop for viewing all grades doesn't belong in that function as well. Use it in the part where the display is managed. In the case below, I manage it in main() because of it's simplicity.

In general, this is slightly better code:
Code:
#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

double mean(unsigned[], unsigned size);
double stddev(unsigned[], unsigned size);
char calc_grade(unsigned, double m, double sigma);


int main()
{
  unsigned grd[100] ={0}, size(0);

  cout<<"Enter number of students (max 100) => ";
  cin>>size;

  cout<<"Enter"<<' '<<size<<' '<<"grades\n";
  for(unsigned i = 0; i < size; ++i)
  {
    std::cin >> grd[i];
  }
    
  cout.setf(ios::fixed, ios::floatfield);
  cout.setf(ios::showpoint);
  cout.precision(1);
  
  double Mean =  mean(grd, size);
  std::cout<< "Mean: " << Mean << std::endl;
  double Sigma =  stddev(grd, size);
  std::cout<< "Std Dev: " << Sigma << std::endl;

  for(unsigned i=0; i < size; ++i)
  {
    std::cout<<calc_grade(grd[i], Mean, Sigma);
  }
  
  return 0;
}

//function header for function mean
double mean(unsigned grades[], unsigned size)
{
  double sum(0);
  for(unsigned i = 0; i < size; ++i)
  {
    sum+=grades[i];
  }
  return sum/size;
}

//function header for function stddev
double stddev(unsigned grades[], unsigned size)
{
  double sum(0);
  double avg = mean(grades, size);
  for(unsigned i = 0; i < size; ++i)
  {
    sum += std::pow((grades[i]-avg), 2);
  }
  return std::sqrt(sum/size);
}

//x = grade.
//m = mean
//sigma = standard deviation. 

//x < m - 3/2(sigma)   --> F
//m - 3/2(sigma) <= x < m - 1/2(sigma)   --> D
//m - 1/2(sigma) <= x < m - 1/2(sigma)   --> C
//m + 1/2(sigma) <= x < m + 3/2(sigma)   --> B
//m + 3/2(sigma) <= x  --> A

char calc_grade(unsigned grade, double m, double sigma)
{
  //Precalculations
  double const BigSigmaFactor = 1.50 * sigma;
  double const SmallSigmaFactor = 0.50 * sigma;
  
  if(grade < m - BigSigmaFactor)
  {
    return 'F';
  }
  if(m - BigSigmaFactor <= grade && grade < m - SmallSigmaFactor)
  {
    return 'D';
  }
  if(m - SmallSigmaFactor <= grade && grade < m - SmallSigmaFactor)
  {
    return 'C';
  }
  if(m + SmallSigmaFactor <= grade && grade < m + BigSigmaFactor)
  {
    return 'B';
  }
  if(m + BigSigmaFactor <= grade)
  {
    return 'A';
  }

}
__________________

Last edited by Valmont; 09-26-2005 at 09:41 PM.
Valmont is offline   Reply With Quote