|
 |
|
 |
09-24-2005, 04:54 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 3
|
possible logic errors in my code?
ok, so im writing a program to have the user enter an amount of students, have the user type the grade for the # of students, then output and calcualate the mean, standard deviation with functions... after that you must print out the grade, also in a function, with the letter grades equaling a formula... here's my code, i got the mean and standard deviation correctly, but the grades are wrong. any help would be appreciated. thanks.
These are the formulas used to get the grades...
Code:
//x=Numeric score(grd[s])
//m=displaymn
//sigma=displaystddev
//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
this is my code....
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
//function prototypes
double mean(int[],int,int);
double stddev(int[],int,int);
void dispgrds(int[],int,int);
int main()
{//Top of main
int grd[100],x,y;
cout<<"Enter number of students\n";
cin>>x;
cout<<"Enter"<<' '<<x<<' '<<"grades\n";
for(y=0;y<x;y++)
cin>>grd[y];
cout.setf(ios::fixed,ios::floatfield);
cout.setf(ios::showpoint);
cout.precision(1);
//print-out of mean
cout<<"MEAN:";
cout<<mean(grd,x,y)<<endl;
//print-out of standard deviation
cout<<"STANDARD DEVIATION:";
cout<<stddev(grd,x,y)<<endl;
//print-out of grades
cout<<"GRADES:";
dispgrds(grd,x,y);
return 0;
}//bottom of main
//function header for function mean
double mean(int grd[],int i,int j)
{
double sum=0,avg=0;
for(i=0;i<j;i++)
sum+=grd[i];
avg=sum/j;
return avg;
}
//function header for function stddev
double stddev(int grd[],int a, int b)
{
double sum2=0,avg2=mean(grd,a,b);
for(a=0;a<b;a++)
sum2+=(grd[a]-avg2)*(grd[a]-avg2);
avg2=sum2/b;
return sqrt(avg2);
}
//function header to print grades
void dispgrds(int grd[], int s, int t)
{
double displaymn=mean(grd,s,t),displaystddev=stddev(grd,s ,t);
for (s=0; s<t; s++)
if (grd[s] < displaymn - (3/2 * displaystddev))
cout << 'F'<<' ';
else if (grd[s] >= displaymn - (3/2 * displaystddev) && grd[s] < displaymn - (1/2 * displaystddev))
cout << 'D'<<' ';
else if (grd[s] >= displaymn - (1/2 * displaystddev) && grd[s] < displaymn + (1/2 * displaystddev))
cout << 'C'<<' ';
else if (grd[s] >= displaymn + (1/2 * displaystddev) && grd[s] < displaymn + (3/2 * displaystddev))
cout << 'B'<<' ';
else if (grd[s] >= displaymn + (3/2 * displaystddev))
cout << 'A' <<' ';
}
Last edited by Valmont; 09-24-2005 at 07:53 PM.
|
|
|
09-25-2005, 06:33 AM
|
#2 (permalink)
|
|
Registered User
Join Date: Mar 2005
Location: UK
Posts: 11
|
Looks like you're short a couple of brackets:
Code:
void dispgrds(int grd[], int s, int t)
{
double displaymn=mean(grd,s,t),displaystddev=stddev(grd,s ,t);
for (s=0; s<t; s++)
{ // Bracket!!
if (grd[s] < displaymn - (3/2 * displaystddev))
cout << 'F'<<' ';
else if (grd[s] >= displaymn - (3/2 * displaystddev) && grd[s] < displaymn - (1/2 * displaystddev))
cout << 'D'<<' ';
else if (grd[s] >= displaymn - (1/2 * displaystddev) && grd[s] < displaymn + (1/2 * displaystddev))
cout << 'C'<<' ';
else if (grd[s] >= displaymn + (1/2 * displaystddev) && grd[s] < displaymn + (3/2 * displaystddev))
cout << 'B'<<' ';
else if (grd[s] >= displaymn + (3/2 * displaystddev))
cout << 'A' <<' ';
} //Bracket!!
}
|
|
|
09-26-2005, 07:45 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 3
|
still not printing out right grades... when i type in the grades 90 82 78, it gives me grades: A B F. the brackets were not the answer
|
|
|
09-26-2005, 05:04 PM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Well the definitions for the grades are axioms from where I stand. By definition I can't prove the correctness of them. I don't even know what you mean. So you need to explain what makes you think your axioms/definitions are correct. For example is a single grade of 55 an A? I can't know from where I am standing...
__________________
|
|
|
09-26-2005, 05:38 PM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
|
Originally Posted by ajstee123
still not printing out right grades... when i type in the grades 90 82 78, it gives me grades: A B F. the brackets were not the answer
|
Then what should the output be?
__________________
|
|
|
09-26-2005, 09:21 PM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
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.
|
|
|
09-27-2005, 03:57 PM
|
#7 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 3
|
i understand the problem and thank you for your help.... i talked to my professor about the formulas the example give you, and they are totally skewed. She said if you got the standard deviation and mean correct it was ok. I also see now why you should use the 3/2.0 since 3/2 will give you one. One question, what is the std:: for? another way to use the c standard lib? also you reinitialized the "i" variable in both loops. plus there were two other errors, saying the pow and sqrt functions not part of the std library... I think because they are in the math library.
|
|
|
09-27-2005, 04:13 PM
|
#8 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Quote:
|
One question, what is the std:: for? another way to use the c standard lib?
|
It is to specify that it is the std namespace equivalent definitions beeing used.
You could also achieve it by having a line saying
Code:
using namespace std;
then you wouldn't need the std::, but for portable code and ANSI C++ restrictions it is better to use std:: infront of the standard declared definitions.
The i isn't reinitialized, it is only defined within the loop. thus it needs to be redefined in the next loop. It is standard ISO/ANSI C++ usage, only to define variables when they are needed.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 02:54 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|