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

Thread: mason74

  1. #1
    Join Date
    Sep 2001
    Posts
    3

    mason74

    Hello! I need some help big time for a C++ project I am working on. The professor wants the class to find the average and variance for a set of scores using "sentinel controlled repletetion" structure. My answers also have to print out in double value form. Here is where the problems start though. With variance...

    The teacher states that variance is calculated as by summing the squares of all the scores a user might use in a set, dividing this sum by the count (or number of scores that are placed), and then subtracting the square of the average from this amount. One problem, the professor mentions or has not taught how to define powers or squares yet! Maybe I don't need to define the "power" and "square root" but it seems as if I do.

    I am having a hard time putting what I know into code to make the statement work right. I have the average already specified and working but not the variance....here is what I have thus far:


    #include <iostream>

    using std::cout;
    using std::cin;
    using std::endl;
    using std::ios;

    # include <iomanip>

    using std::setprecision;
    using std::setiosflags;

    int main()

    {
    int total,
    scoreCounter,
    score;

    double average;
    double variance;
    double pow;
    double sqrt;

    total = 0;
    scoreCounter = 0;

    cout << "Greetings! Please enter your first score, -1 to end: \n";
    cin >> score;


    while (score != -1 )
    {
    total = total + score;
    scoreCounter = scoreCounter +1;
    cout << "Please enter your next score, -1 to end: \n";
    cin >> score;
    }

    if ( scoreCounter !=0 )
    {
    average = static_cast< double >( total ) / scoreCounter;
    cout << "The average of the scores is " << setprecision( 1 )
    << setiosflags( ios::fixed | ios::showpoint )
    << average << endl;
    }

    else
    cout << "Sorry, no scores were entered, therefore an average can't be calculated." << endl;

    if ( scoreCounter !=0 )
    {
    variance = static_cast< double >( WHERE I NEED HELP) / scoreCounter - sqrt( average );
    cout << "The variance of the scores is " << setprecision( 1 )
    << setiosflags( ios::fixed | ios::showpoint )
    << variance << endl;
    }

    else
    cout << "Sorry, no scores were entered, therefore a variance can't be calculated." << endl;


    return 0;

    Any help would be greatly appreciated. I am just trying to understand the basics here of how to solve for variance the right way.


    Andrew


  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: mason74

    I think you should post this on C forum, not on Vb...
    ;-)

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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