what did I do wrong?

This is a discussion on what did I do wrong? within the C++ Programming forums, part of the General Programming Boards category; Boy, I sure would love it if someone could help me figure this out. My professor has created some code ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    what did I do wrong?

    Boy, I sure would love it if someone could help me figure this out. My professor has created some code that's a loop for enter numbers. I need to add code to make the program figure averages and standard deviation. I've added the formulas as I believe they should be, and the program runs, but when I test the program with some small numbers I can do in my head, the average and deviation is wrong. Here's the code, stripped of pretty formatting....

    Code:
    // Program Abstract:       the purpose of this program is to determine the average and standard deviation of input numbers
    //   Input Required:       list of numbers
    //   Output Desired:       program will output the list of numbers,
    //                         average and standard deviation of each number
    // =============================================================================
    
    #include <conio.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <iostream.h>
    #include <math.h>
    
    //==============================================================================
    
    // variable declarations
    
    // alphabetized variable dictionary
    // =========================================================================
    // d is data
    // i is spacing
    // m is average
    // n is count
    // reply stores user's answer to queries
    // s is standard deviation
    // x is sum
    
    int main() {
      double d, i, m, n, s, x;
      double average, stddev, sum, sumsqr;
      char reply;
    
    ofstream fout;
    
      fout.open ("prompt.txt");
    
      // introduce the program to determine average and
      // standard deviation of a list of numbers
    
    //==============================================================================
      cout << setw (61) << "Program for average and standard deviation";
    
      // press any key to continue
      cout << setw (52) << "Press RETURN to continue";
      getchar();
    
      // run program until user is done
      // loop for data entry, calculates sum and counter, ends after last while
      do
        {
          // setting beginning numbers to 0
          n = 0;
          x = 0;
          sumsqr = 0;
    
          // loop B check and correct data entry
          do
            {
              clrscr ();
    
              // prompt the user for the next data
              cout << setw (61) << "Enter next data, enter -1 to end data stream";
              cout << ": ";
              cin >> d;
    
              do
                {
                  clrscr ();
    
                    // echo print data read
                    cout << setw (37) << "Is " << d << " correct?";
                    cout << endl << endl << endl;
    
                    // query the user for correctness of input data
                    cout << setw (55) << "Enter y for yes or n for no: ";
    
                    // input response to query
                    cin >> reply;
                    tolower(reply);
          }
    
          while(reply!='n' && reply!='y');
    
    
          if(reply=='y') {
    
                ++n ;
                x += d;
                sumsqr += d * d;
    
                // record verified data in output textfile
                fout << d << endl;
              }
        }
            while (d != -1);
    
            // calculate average
            if ( n > 0 )
              m = x / n;
            else
              m = 0 ;
    
            // press any key to continue
            cout << setw (52) << "Press RETURN to continue";
            getchar();
    
            // calculate standard deviation
            if ( n > 1 )
              s = sqrt ((n * sumsqr - x * x) / (n * (n - 1))) ;
            else
              s = 0;
    
            clrscr ();
    
            cout << setw (50) <<  "The average is: " << m;
    
            cout << setw (50) <<  "The standard deviation is: " << s;
    
            // press any key to continue
            cout << setw (52) << "Press RETURN to continue";
            getchar();
    
            do
              {
                clrscr ();
    
                // query user for another input data
                cout << setw (55) << "Is there another set of data?";
    
                cout << setw (55) << "Enter y for yes or n for no: ";
                cin >> reply;
                tolower(reply);
              }
            while ((reply != 'n') && (reply != 'N') &&
                   (reply != 'y') && (reply != 'Y'));
          }
        while ((reply == 'y') || (reply == 'Y'));
    
        fout.close ();
        return 0;
      }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by cherigee
    Boy, I sure would love it if someone could help me figure this out.
    Here's how you can figure it out:

    Put some output statements close to your calculations to see what's happening.

    Like this:
    Code:
          if(reply=='y') {
    
                ++n ;
                x += d;
                sumsqr += d * d;
                cout << "x = " << x 
                     << ", sumsqr = " << sumsqr 
                     << ", n = " << n << endl;
    
                // record verified data in output textfile
                fout << d << endl;
              }
        }
    and this
    Code:
            // calculate average
            cout << "x = " << x 
                 << ", sumsqr = " << sumsqr 
                 << ", n = " << n << endl;
    
            if ( n > 0 )
              m = x / n;
            else
              m = 0 ;
    Regards,

    Dave
    Last edited by Dave Evans; 02-15-2005 at 08:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21