Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

a little help for a confused one in C++

blitzer16blitzer16 Member Posts: 2
this is my code for translating numeric grades to letter grades but somehow my code for the standard deviation is wrong. worst "my whole program is wrong". it outputs letter B for a grade 100 XD

/*
ASSUMPTION: There will be a fixed number of grades that is 5.
*/
#include
#include

using namespace std;

double mean(double a[]);
double mean(double a[])
{
double sum = 0;
for(int i = 0; i < 5; i++)
{
sum = sum + a[i];
}
return (sum/5);
}
double stdev(double b[], double mean);
double stdev(double a[], double mean)
{
double sum = 0;
for(int i = 0; i < 5; i++)
{
sum = sqrt(pow((a[i] - mean), 2));
}
return sum;
}
void detdisp(double c[], double mean, double stdev);
void detdisp(double c[], double mean, double stdev)
{
double w, x, y, z;

w = mean - ((3 * stdev)/2);
x = mean - (stdev/2);
y = mean + (stdev/2);
z = mean + ((3 * stdev)/2);

cout << "w = " << w << ", x = " << x << ", y = " << y << ", z = " << z << "




";

for(int i = 0; i < 5; i++)
{
if(c[i] < w)
{
cout << "w = " << w << ", " << c[i] << " -> F
";
}
if(c[i] >= w && c[i] < x)
{
cout << "w= " << w << ", x = " << x << ", " << c[i] << " -> D
";
}
if(c[i] >= x && c[i] < y)
{
cout << "x= " << x << ", y = " << y << ", " << c[i] << " -> C
";
}
if(c[i] >= y && c[i] < z)
{
cout << "y= " << y << ", z = " << z << ", " << c[i] << " -> B
";
}
if(c[i] >= z)
{
cout << "z = " << z << ", " << c[i] << " -> A
";
}
}
}

int main()
{
double arr[5];

cout << "ENTER 5 VALUES SEPARATED BY SPACES:
";
for(int j = 0; j < 5; j++) cin >> arr[j];

cout << "mean = " << mean(arr);
cout << "
stdev = " << stdev(arr, mean(arr)) << "



";

detdisp(arr, mean(arr), stdev(arr, mean(arr)));

system("pause");
return 0;
}


thanks.. help me XD

Comments

  • LundinLundin Member Posts: 3,711
    Well... I'm sorry but it is a mess, I won't clean it up for you nor read it before it is readable.

    Please use code tags when posting code to preserve the indention:

    [leftbr]code[rightbr]
    /* code goes here */
    [leftbr]/code[rightbr]

    Also, put some new lines between every function. As you have written them (above the caller in the source file) you won't need the function prototypes, they are just confusing.
  • cactus1cactus1 Member Posts: 41
    : Well... I'm sorry but it is a mess, I won't clean it up for you nor
    : read it before it is readable.
    :
    : Please use code tags when posting code to preserve the indention:
    :
    : [leftbr]code[rightbr]
    : /* code goes here */
    : [leftbr]/code[rightbr]
    :
    : Also, put some new lines between every function. As you have written
    : them (above the caller in the source file) you won't need the
    : function prototypes, they are just confusing.

    I'll clean it up.
    [code]
    /*
    ASSUMPTION: There will be a fixed number of grades that is 5.
    */
    #include
    #include

    using namespace std;

    double mean(double a[]);
    double mean(double a[])
    {
    double sum = 0;
    for(int i = 0; i < 5; i++)
    {
    sum = sum + a[i];
    }
    return (sum/5);
    }
    double stdev(double b[], double mean);
    double stdev(double a[], double mean)
    {
    double sum = 0;
    for(int i = 0; i < 5; i++)
    {
    sum = sqrt(pow((a[i] - mean), 2));
    }
    return sum;
    }
    void detdisp(double c[], double mean, double stdev);
    void detdisp(double c[], double mean, double stdev)
    {
    double w, x, y, z;

    w = mean - ((3 * stdev)/2);
    x = mean - (stdev/2);
    y = mean + (stdev/2);
    z = mean + ((3 * stdev)/2);

    cout << "w = " << w << ", x = " << x << ", y = " << y << ", z = " << z << "




    ";

    for(int i = 0; i < 5; i++)
    {
    if(c[i] < w)
    {
    cout << "w = " << w << ", " << c[i] << " -> F
    ";
    }
    if(c[i] >= w && c[i] < x)
    {
    cout << "w= " << w << ", x = " << x << ", " << c[i] << " -> D
    ";
    }
    if(c[i] >= x && c[i] < y)
    {
    cout << "x= " << x << ", y = " << y << ", " << c[i] << " -> C
    ";
    }
    if(c[i] >= y && c[i] < z)
    {
    cout << "y= " << y << ", z = " << z << ", " << c[i] << " -> B
    ";
    }
    if(c[i] >= z)
    {
    cout << "z = " << z << ", " << c[i] << " -> A
    ";
    }
    }
    }

    int main()
    {
    double arr[5];

    cout << "ENTER 5 VALUES SEPARATED BY SPACES:
    ";
    for(int j = 0; j < 5; j++) cin >> arr[j];

    cout << "mean = " << mean(arr);
    cout << "
    stdev = " << stdev(arr, mean(arr)) << "



    ";

    detdisp(arr, mean(arr), stdev(arr, mean(arr)));

    system("pause");
    return 0;
    }
    [/code]
  • blitzer16blitzer16 Member Posts: 2
    : : Well... I'm sorry but it is a mess, I won't clean it up for you nor
    : : read it before it is readable.
    : :
    : : Please use code tags when posting code to preserve the indention:
    : :
    : : [leftbr]code[rightbr]
    : : /* code goes here */
    : : [leftbr]/code[rightbr]
    : :
    : : Also, put some new lines between every function. As you have written
    : : them (above the caller in the source file) you won't need the
    : : function prototypes, they are just confusing.
    :
    : I'll clean it up.
    : [code]:
    : /*
    : ASSUMPTION: There will be a fixed number of grades that is 5.
    : */
    : #include
    : #include
    :
    : using namespace std;
    :
    : double mean(double a[]);
    : double mean(double a[])
    : {
    : double sum = 0;
    : for(int i = 0; i < 5; i++)
    : {
    : sum = sum + a[i];
    : }
    : return (sum/5);
    : }
    : double stdev(double b[], double mean);
    : double stdev(double a[], double mean)
    : {
    : double sum = 0;
    : for(int i = 0; i < 5; i++)
    : {
    : sum = sqrt(pow((a[i] - mean), 2));
    : }
    : return sum;
    : }
    : void detdisp(double c[], double mean, double stdev);
    : void detdisp(double c[], double mean, double stdev)
    : {
    : double w, x, y, z;
    :
    : w = mean - ((3 * stdev)/2);
    : x = mean - (stdev/2);
    : y = mean + (stdev/2);
    : z = mean + ((3 * stdev)/2);
    :
    : cout << "w = " << w << ", x = " << x << ", y = " << y << ", z = " << z << "




    ";
    :
    : for(int i = 0; i < 5; i++)
    : {
    : if(c[i] < w)
    : {
    : cout << "w = " << w << ", " << c[i] << " -> F
    ";
    : }
    : if(c[i] >= w && c[i] < x)
    : {
    : cout << "w= " << w << ", x = " << x << ", " << c[i] << " -> D
    ";
    : }
    : if(c[i] >= x && c[i] < y)
    : {
    : cout << "x= " << x << ", y = " << y << ", " << c[i] << " -> C
    ";
    : }
    : if(c[i] >= y && c[i] < z)
    : {
    : cout << "y= " << y << ", z = " << z << ", " << c[i] << " -> B
    ";
    : }
    : if(c[i] >= z)
    : {
    : cout << "z = " << z << ", " << c[i] << " -> A
    ";
    : }
    : }
    : }
    :
    : int main()
    : {
    : double arr[5];
    :
    : cout << "ENTER 5 VALUES SEPARATED BY SPACES:
    ";
    : for(int j = 0; j < 5; j++) cin >> arr[j];
    :
    : cout << "mean = " << mean(arr);
    : cout << "
    stdev = " << stdev(arr, mean(arr)) << "



    ";
    :
    : detdisp(arr, mean(arr), stdev(arr, mean(arr)));
    :
    : system("pause");
    : return 0;
    : }
    : [/code]:
    :

    thx for cleaning out my prog dude.. yeah its a bit messy because im a bit
    tired that time.. and still im confused XD..
Sign In or Register to comment.