CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Help!

  1. #1
    Join Date
    Apr 2010
    Posts
    13

    Post Help!

    I need to write a program to calculate the mean and standard deviation of a set of data. Need to use functions ReadData, PrintData,CalcMean,CalcStanDev.....I have no idea what I'm doing. I'm barely beginning to grasp loops. CodeGuru's been good to me in the past, please help me out?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help!

    Write ReadData and PrintData first. After that, it's just equations.

  3. #3
    Join Date
    Apr 2010
    Posts
    13

    Re: Help!

    The equations are whats killing me, I never learned standard deviation

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help!

    Well, standard deviation is just the square root of the variance, and the variance isn't any more difficult to compute than the mean:
    http://en.wikipedia.org/wiki/Computa...r_the_variance

  5. #5
    Join Date
    Apr 2010
    Posts
    13

    Re: Help!

    aye, im looking at the equation now. I have no idea what it means, maybe my brother can get through to me

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help!

    This might be a more useful link:
    http://en.wikipedia.org/wiki/Algorit...ating_variance
    While all the caveats and alternatives are interesting, for your purposes the naive algorithm should be good enough. Note the relationship to the equation at the top of the previous link.

  7. #7
    Join Date
    Apr 2010
    Posts
    13

    Re: Help!

    Thanks I'm gonna run with this for a little and see where I can, I'll probably be back to check my coding

  8. #8
    Join Date
    Apr 2010
    Posts
    13

    Re: Help!

    #include <cstdlib>
    #include <iostream>
    #include <math.h>

    using namespace std;

    int main(int argc, char *argv[])
    {


    ReadData

    PrintData

    int calc (float a, float b, float c, float d, float e, float sum, float mean,float sd);




    float a, b, c, d, e, mean=0.0, avg=0.0;

    float sd=0.0;



    calc (a, b, c, d, e, &sum,&avg, &sd);



    print("Mean=", mean);

    print("Standard Deviation=", sd);


    getchar();

    return 0;
    }

    calc (float a, float b, float c, float d, float e, float sum float mean, float sd)

    {
    float Calc=0.0;

    *sum = a+b+c+d+e;

    *avg = *sum / 5.0;

    Calc += ( a - avg) * ( a - avg);

    Calc += ( b - avg) * ( b - avg);

    Calc += ( c - avg) * ( c - avg);
    Calc += ( d - avg) * ( d - avg);

    Calc += ( e - avg) * ( e - avg);


    sd = sqrt((double)Calc/5.0);

    }


    system("PAUSE");
    return EXIT_SUCCESS;
    }
    Last edited by Kogo; April 9th, 2010 at 01:21 AM.

  9. #9
    Join Date
    Apr 2010
    Posts
    13

    Re: Help!

    this is what I got but it isn't compiling

  10. #10
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Help!

    Code:
    int calc (float a, float b, float c, float d, float e, float sum, float mean,float sd);
    This is inside your main function, it should be outside.

    Code:
    ReadData
    
    PrintData
    What is this doing there ?

    Code:
    system("PAUSE");
    return EXIT_SUCCESS;
    this code belongs in your main , not outside it..

    Code:
    *sum = a+b+c+d+e;
    sum is not a pointer.


    Also, for posting code use code tags
    Last edited by Skizmo; April 9th, 2010 at 02:17 AM.

  11. #11
    Join Date
    Apr 2010
    Posts
    13

    Re: Help!

    Thank you, as dumb as I feel thats the kind of help I need. I never should have taken Honors Computer Programming as a freshman but my teacher has been horribly nice to me considering and I'm just trying to make it through at least one of these 2 assignments that are due. Thanks. This is what I've got, still working though but any more advice in the mean time is greatly appreciated.

    Code:
     #include <cstdlib>
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int calc (float a, float b, float c, float d, float e, float sum, float mean,float sd);
    
    int main(int argc, char *argv[])
    {
    float a, b, c, d, e, mean=0.0, avg=0.0;
    
    float sd=0.0;
    
    
    
    CalcMean ((a+b+c+d+e)/5) ;
    
    
    
    print("Mean=", mean);
    
    print("Standard Deviation=", sd);
    
    
    getchar();
    
    return 0;
    }
    
    calc (float a, float b, float c, float d, float e, float sum float mean, float sd)
    
    {
    float Calc=0.0;
    
    *sum = a+b+c+d+e;
    
    *avg = *sum / 5.0;
    
    Calc += ( a - avg) * ( a - avg);
    
    Calc += ( b - avg) * ( b - avg);
    
    Calc += ( c - avg) * ( c - avg);
    Calc += ( d - avg) * ( d - avg);
    
    Calc += ( e - avg) * ( e - avg);
    
    
    sd = sqrt((double)Calc/5.0);
    
    system("PAUSE");
    return EXIT_SUCCESS;
    }

  12. #12
    Join Date
    Apr 2010
    Posts
    13

    Re: Help!

    Idk if this code is salvageable. I have put together working programs before,its just been a while and I missed some chapters that obviously would have been helpful. I tried taking a shortcut on this one since I procrastinated for so long, hopefully I'll get it worked out, thanks anyway.

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help!

    Well, you were told to use functions called CalcMean and CalcStanDev rather than a single calc() function, so that's one change you can make immediately.

    The first thing you need to figure out is how you're going to handle the data set. Right now, you have 5 float variables, which means you can't handle more than 5 values. Are you guaranteed you'll only get 5 values, or are you supposed to handle an arbitrary number? Will input be terminated after a specific count, or after a delimiting number is observed? Are you going to store all the inputs in memory, or process them "on the fly" with no storage? These are important details to worry about before you ever concern yourself with the equations.
    Last edited by Lindley; April 9th, 2010 at 09:07 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured