Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. trouble with calculations

trouble with calculations

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
17 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lindag1783
    wrote on last edited by
    #1

    Hello I have writen this in C++ (using the compiler Visual C++ 2005 Express Edition). I am now having some trouble with calculations. I want to work out each sample scores variance (which is used in statistics). I have a bunch of scores that hte user inputs and I can work out the average, that is the program will do that part, but when it comes to working variance the answers are wrong. The variance is worked out like this - each score minus the mean, to the power of two (or times by itself), all these scores then have to be divided by the Number of scores that are in the sample. The program cannot calculate this. For example, there are 2 scores, 1st score = 5, 2nd score = 4; it works out the mean (average) okay which is the total of all the scores divided by the number of scores in the sample. It gets very close to figuring the right amount for the variance, but not quiet. Please help me figure out where I've gone wrong. Thank You in advance. Here is my code: #include using namespace std; int main(void) { float M, N, total, score1, score2, score3, score4, score5, score6, score7, score8, V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance; cout << "Input the number of people in sample "; cin >> N; cout << "input the 1st score "; cin >> score1; cout << "input the 2nd score "; cin >> score2; cout << "input the 3rd score "; cin >> score3; cout << "input the 4th score "; cin >> score4; cout << "input the 5th score "; cin >> score5; cout << "input the 6th score "; cin >> score6; cout << "input the 7th score "; cin >> score7; cout << "input the 8th score "; cin >> score8; total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8; M = total / N; cout << "the mean for these scores is: " << M << "\n"; s1 = (float) score1 - M; V1 = (float) s1 * s1; s2 = (float) score2 - M; V2 = (float) s2 * s2; s3 = (float) score3 - M; V3 = (float) s3 * s3; s4 = (float) score4 - M; V4 = (float) s4 * s4; s5 = (float) score5 - M; V5 = (float) s5 * s5; s6 = (float) score6 - M; V6 = (float) s6 * s6; s7 = (float) score7 - M; V7 = (float) s7 * s7; s8 = (float) score8 - M; V8 = (float) s8 * s8; cout << "the variance for these scores is: " << (float) (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N << "\n"; return 0; } linda

    J G S S 4 Replies Last reply
    0
    • L lindag1783

      Hello I have writen this in C++ (using the compiler Visual C++ 2005 Express Edition). I am now having some trouble with calculations. I want to work out each sample scores variance (which is used in statistics). I have a bunch of scores that hte user inputs and I can work out the average, that is the program will do that part, but when it comes to working variance the answers are wrong. The variance is worked out like this - each score minus the mean, to the power of two (or times by itself), all these scores then have to be divided by the Number of scores that are in the sample. The program cannot calculate this. For example, there are 2 scores, 1st score = 5, 2nd score = 4; it works out the mean (average) okay which is the total of all the scores divided by the number of scores in the sample. It gets very close to figuring the right amount for the variance, but not quiet. Please help me figure out where I've gone wrong. Thank You in advance. Here is my code: #include using namespace std; int main(void) { float M, N, total, score1, score2, score3, score4, score5, score6, score7, score8, V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance; cout << "Input the number of people in sample "; cin >> N; cout << "input the 1st score "; cin >> score1; cout << "input the 2nd score "; cin >> score2; cout << "input the 3rd score "; cin >> score3; cout << "input the 4th score "; cin >> score4; cout << "input the 5th score "; cin >> score5; cout << "input the 6th score "; cin >> score6; cout << "input the 7th score "; cin >> score7; cout << "input the 8th score "; cin >> score8; total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8; M = total / N; cout << "the mean for these scores is: " << M << "\n"; s1 = (float) score1 - M; V1 = (float) s1 * s1; s2 = (float) score2 - M; V2 = (float) s2 * s2; s3 = (float) score3 - M; V3 = (float) s3 * s3; s4 = (float) score4 - M; V4 = (float) s4 * s4; s5 = (float) score5 - M; V5 = (float) s5 * s5; s6 = (float) score6 - M; V6 = (float) s6 * s6; s7 = (float) score7 - M; V7 = (float) s7 * s7; s8 = (float) score8 - M; V8 = (float) s8 * s8; cout << "the variance for these scores is: " << (float) (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N << "\n"; return 0; } linda

      J Offline
      J Offline
      Justin Tay
      wrote on last edited by
      #2

      What do you mean by not quite? If what you have is a sample, are you supposed to divide by N and not N - 1?

      L 1 Reply Last reply
      0
      • L lindag1783

        Hello I have writen this in C++ (using the compiler Visual C++ 2005 Express Edition). I am now having some trouble with calculations. I want to work out each sample scores variance (which is used in statistics). I have a bunch of scores that hte user inputs and I can work out the average, that is the program will do that part, but when it comes to working variance the answers are wrong. The variance is worked out like this - each score minus the mean, to the power of two (or times by itself), all these scores then have to be divided by the Number of scores that are in the sample. The program cannot calculate this. For example, there are 2 scores, 1st score = 5, 2nd score = 4; it works out the mean (average) okay which is the total of all the scores divided by the number of scores in the sample. It gets very close to figuring the right amount for the variance, but not quiet. Please help me figure out where I've gone wrong. Thank You in advance. Here is my code: #include using namespace std; int main(void) { float M, N, total, score1, score2, score3, score4, score5, score6, score7, score8, V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance; cout << "Input the number of people in sample "; cin >> N; cout << "input the 1st score "; cin >> score1; cout << "input the 2nd score "; cin >> score2; cout << "input the 3rd score "; cin >> score3; cout << "input the 4th score "; cin >> score4; cout << "input the 5th score "; cin >> score5; cout << "input the 6th score "; cin >> score6; cout << "input the 7th score "; cin >> score7; cout << "input the 8th score "; cin >> score8; total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8; M = total / N; cout << "the mean for these scores is: " << M << "\n"; s1 = (float) score1 - M; V1 = (float) s1 * s1; s2 = (float) score2 - M; V2 = (float) s2 * s2; s3 = (float) score3 - M; V3 = (float) s3 * s3; s4 = (float) score4 - M; V4 = (float) s4 * s4; s5 = (float) score5 - M; V5 = (float) s5 * s5; s6 = (float) score6 - M; V6 = (float) s6 * s6; s7 = (float) score7 - M; V7 = (float) s7 * s7; s8 = (float) score8 - M; V8 = (float) s8 * s8; cout << "the variance for these scores is: " << (float) (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N << "\n"; return 0; } linda

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #3

        How much is the difference between what you are expecting and the value you are getting? Two things could be happening here. First, floating point numbers are only represented approximately. A float value in this case is represented in 32 bits: 24 bits for a signed mantissa, and 8 bits for a signed exponent if I remember right. This means that when the user enters "5.0", for example, the closest internal representation might actually be the value 4.999998. This introduces one type of difference. Secondly, you are using float variables for your calculations. The maximum precision you will see in calculations using float is the value of the FLT_EPSILON constant found in math.h. For VC++, this constant has the value 1.192092896e-07F, which basically means that using float values the best precision you can get is around 7 decimal places. Practically speaking, you can probably expect 5 or 6 decimal places with your program. One thing to try is to change all of your float values to double values. The double type is represented in 64 bits, with correspondingly higher precision. A side note: toward the end of your code, you've got statements like this:

        s8 = (float)score8 - M;
        V8 = (float)s8 * s8;

        The (float) cast's aren't necessary. The compiler already knows that all of the variables in the statements are of type float.


        Software Zen: delete this;

        Fold With Us![^]

        L 1 Reply Last reply
        0
        • L lindag1783

          Hello I have writen this in C++ (using the compiler Visual C++ 2005 Express Edition). I am now having some trouble with calculations. I want to work out each sample scores variance (which is used in statistics). I have a bunch of scores that hte user inputs and I can work out the average, that is the program will do that part, but when it comes to working variance the answers are wrong. The variance is worked out like this - each score minus the mean, to the power of two (or times by itself), all these scores then have to be divided by the Number of scores that are in the sample. The program cannot calculate this. For example, there are 2 scores, 1st score = 5, 2nd score = 4; it works out the mean (average) okay which is the total of all the scores divided by the number of scores in the sample. It gets very close to figuring the right amount for the variance, but not quiet. Please help me figure out where I've gone wrong. Thank You in advance. Here is my code: #include using namespace std; int main(void) { float M, N, total, score1, score2, score3, score4, score5, score6, score7, score8, V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance; cout << "Input the number of people in sample "; cin >> N; cout << "input the 1st score "; cin >> score1; cout << "input the 2nd score "; cin >> score2; cout << "input the 3rd score "; cin >> score3; cout << "input the 4th score "; cin >> score4; cout << "input the 5th score "; cin >> score5; cout << "input the 6th score "; cin >> score6; cout << "input the 7th score "; cin >> score7; cout << "input the 8th score "; cin >> score8; total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8; M = total / N; cout << "the mean for these scores is: " << M << "\n"; s1 = (float) score1 - M; V1 = (float) s1 * s1; s2 = (float) score2 - M; V2 = (float) s2 * s2; s3 = (float) score3 - M; V3 = (float) s3 * s3; s4 = (float) score4 - M; V4 = (float) s4 * s4; s5 = (float) score5 - M; V5 = (float) s5 * s5; s6 = (float) score6 - M; V6 = (float) s6 * s6; s7 = (float) score7 - M; V7 = (float) s7 * s7; s8 = (float) score8 - M; V8 = (float) s8 * s8; cout << "the variance for these scores is: " << (float) (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N << "\n"; return 0; } linda

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          First use double not float. Second lose the (float) cast: the fact that a C-style cast is used at all in a C++ program is bad enough (not everyone agrees with this; the ones that don’t are wrong!) but score1 is already a float anyway. Steve

          1 Reply Last reply
          0
          • J Justin Tay

            What do you mean by not quite? If what you have is a sample, are you supposed to divide by N and not N - 1?

            L Offline
            L Offline
            lindag1783
            wrote on last edited by
            #5

            Hi thanks, what i mean by quet is that sometimes it will give the answerr that is close to the answer and tother times the answer is way off. I do not know what to do. I want to calculate the variance of a single sample thus it is /N, /N-1 is only used in test that have two sets of samples. Thanks linda

            1 Reply Last reply
            0
            • G Gary R Wheeler

              How much is the difference between what you are expecting and the value you are getting? Two things could be happening here. First, floating point numbers are only represented approximately. A float value in this case is represented in 32 bits: 24 bits for a signed mantissa, and 8 bits for a signed exponent if I remember right. This means that when the user enters "5.0", for example, the closest internal representation might actually be the value 4.999998. This introduces one type of difference. Secondly, you are using float variables for your calculations. The maximum precision you will see in calculations using float is the value of the FLT_EPSILON constant found in math.h. For VC++, this constant has the value 1.192092896e-07F, which basically means that using float values the best precision you can get is around 7 decimal places. Practically speaking, you can probably expect 5 or 6 decimal places with your program. One thing to try is to change all of your float values to double values. The double type is represented in 64 bits, with correspondingly higher precision. A side note: toward the end of your code, you've got statements like this:

              s8 = (float)score8 - M;
              V8 = (float)s8 * s8;

              The (float) cast's aren't necessary. The compiler already knows that all of the variables in the statements are of type float.


              Software Zen: delete this;

              Fold With Us![^]

              L Offline
              L Offline
              lindag1783
              wrote on last edited by
              #6

              Hi Thanks for your reply. I have insert double instead of float but I still get the same mistakes. he mistakes sometimes are way off from the true answer, so onlsy sometimes are they close to the answer. In this case the answer came back way off again, by about 40! Anyway, I am sure my logic is correct so it mst be the code somehow. Please help. Here is my code: #include using namespace std; int main(void) { double M, N, total, score1, score2, score3, score4, score5, score6, score7, score8, V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance; cout << "Input the number of people in sample "; cin >> N; cout << "input the 1st score "; cin >> score1; cout << "input the 2nd score "; cin >> score2; cout << "input the 3rd score "; cin >> score3; cout << "input the 4th score "; cin >> score4; cout << "input the 5th score "; cin >> score5; cout << "input the 6th score "; cin >> score6; cout << "input the 7th score "; cin >> score7; cout << "input the 8th score "; cin >> score8; total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8; M = total / N; cout << "the mean for these scores is: " << M << "\n"; s1 = score1 - M; V1 = s1 * s1; s2 = score2 - M; V2 = s2 * s2; s3 = score3 - M; V3 = s3 * s3; s4 = score4 - M; V4 = s4 * s4; s5 = score5 - M; V5 = s5 * s5; s6 = score6 - M; V6 = s6 * s6; s7 = score7 - M; V7 = s7 * s7; s8 = score8 - M; V8 = s8 * s8; cout << "the variance for these scores is: " << (double) (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N << "\n"; return 0; } :confused: linda

              G 1 Reply Last reply
              0
              • L lindag1783

                Hello I have writen this in C++ (using the compiler Visual C++ 2005 Express Edition). I am now having some trouble with calculations. I want to work out each sample scores variance (which is used in statistics). I have a bunch of scores that hte user inputs and I can work out the average, that is the program will do that part, but when it comes to working variance the answers are wrong. The variance is worked out like this - each score minus the mean, to the power of two (or times by itself), all these scores then have to be divided by the Number of scores that are in the sample. The program cannot calculate this. For example, there are 2 scores, 1st score = 5, 2nd score = 4; it works out the mean (average) okay which is the total of all the scores divided by the number of scores in the sample. It gets very close to figuring the right amount for the variance, but not quiet. Please help me figure out where I've gone wrong. Thank You in advance. Here is my code: #include using namespace std; int main(void) { float M, N, total, score1, score2, score3, score4, score5, score6, score7, score8, V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance; cout << "Input the number of people in sample "; cin >> N; cout << "input the 1st score "; cin >> score1; cout << "input the 2nd score "; cin >> score2; cout << "input the 3rd score "; cin >> score3; cout << "input the 4th score "; cin >> score4; cout << "input the 5th score "; cin >> score5; cout << "input the 6th score "; cin >> score6; cout << "input the 7th score "; cin >> score7; cout << "input the 8th score "; cin >> score8; total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8; M = total / N; cout << "the mean for these scores is: " << M << "\n"; s1 = (float) score1 - M; V1 = (float) s1 * s1; s2 = (float) score2 - M; V2 = (float) s2 * s2; s3 = (float) score3 - M; V3 = (float) s3 * s3; s4 = (float) score4 - M; V4 = (float) s4 * s4; s5 = (float) score5 - M; V5 = (float) s5 * s5; s6 = (float) score6 - M; V6 = (float) s6 * s6; s7 = (float) score7 - M; V7 = (float) s7 * s7; s8 = (float) score8 - M; V8 = (float) s8 * s8; cout << "the variance for these scores is: " << (float) (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N << "\n"; return 0; } linda

                S Offline
                S Offline
                Saurabh Garg
                wrote on last edited by
                #7

                Can you provide with a sample input data and expected mean and variance. I can't see anything wrong in the code. Only thing I can think of is you are computing biased variance in the code but true answer with which you are comparing with might be un-biased variance. See here[^] for more information about variance. -Saurabh

                L 1 Reply Last reply
                0
                • S Saurabh Garg

                  Can you provide with a sample input data and expected mean and variance. I can't see anything wrong in the code. Only thing I can think of is you are computing biased variance in the code but true answer with which you are comparing with might be un-biased variance. See here[^] for more information about variance. -Saurabh

                  L Offline
                  L Offline
                  lindag1783
                  wrote on last edited by
                  #8

                  Hi, I really appreciate your help here. The variance is worked out like the first example that you redirected my browser to regarding variance (the website about it). Example: Numberofscores(N)=2, score1=2, score2=5, all the other scores are input as 0 (seeing there is no scores). The mean is worked out as 2 + 5 / 2 = 3.5 (the program will do this calculation). Variance is worked out like this: (score1 - Mean) to the power of 2 + (score2 - Mean) to the power of 2 / 2 (N). I did not know what power was in C++ so I times it by itself. I think maybe there is something wrong with the compiler I am using as it will not recognise some commands. It does not return any errors but the calculations are wrong for the variance. Here is my code: #include using namespace std; int main(void) { double M, N, total, score1, score2, score3, score4, score5, score6, score7, score8, V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8; cout << "Input the number of people in sample "; cin >> N; cout << "input the 1st score "; cin >> score1; cout << "input the 2nd score "; cin >> score2; cout << "input the 3rd score "; cin >> score3; cout << "input the 4th score "; cin >> score4; cout << "input the 5th score "; cin >> score5; cout << "input the 6th score "; cin >> score6; cout << "input the 7th score "; cin >> score7; cout << "input the 8th score "; cin >> score8; total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8; M = total / N; cout << "the mean for these scores is: " << M << "\n"; s1 = score1 - M; V1 = s1 * s1; s2 = score2 - M; V2 = s2 * s2; s3 = score3 - M; V3 = s3 * s3; s4 = score4 - M; V4 = s4 * s4; s5 = score5 - M; V5 = s5 * s5; s6 = score6 - M; V6 = s6 * s6; s7 = score7 - M; V7 = s7 * s7; s8 = score8 - M; V8 = s8 * s8; cout << "the variance for these scores is: " << (double) (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N << "\n"; return 0; } linda

                  J S 2 Replies Last reply
                  0
                  • L lindag1783

                    Hi, I really appreciate your help here. The variance is worked out like the first example that you redirected my browser to regarding variance (the website about it). Example: Numberofscores(N)=2, score1=2, score2=5, all the other scores are input as 0 (seeing there is no scores). The mean is worked out as 2 + 5 / 2 = 3.5 (the program will do this calculation). Variance is worked out like this: (score1 - Mean) to the power of 2 + (score2 - Mean) to the power of 2 / 2 (N). I did not know what power was in C++ so I times it by itself. I think maybe there is something wrong with the compiler I am using as it will not recognise some commands. It does not return any errors but the calculations are wrong for the variance. Here is my code: #include using namespace std; int main(void) { double M, N, total, score1, score2, score3, score4, score5, score6, score7, score8, V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8; cout << "Input the number of people in sample "; cin >> N; cout << "input the 1st score "; cin >> score1; cout << "input the 2nd score "; cin >> score2; cout << "input the 3rd score "; cin >> score3; cout << "input the 4th score "; cin >> score4; cout << "input the 5th score "; cin >> score5; cout << "input the 6th score "; cin >> score6; cout << "input the 7th score "; cin >> score7; cout << "input the 8th score "; cin >> score8; total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8; M = total / N; cout << "the mean for these scores is: " << M << "\n"; s1 = score1 - M; V1 = s1 * s1; s2 = score2 - M; V2 = s2 * s2; s3 = score3 - M; V3 = s3 * s3; s4 = score4 - M; V4 = s4 * s4; s5 = score5 - M; V5 = s5 * s5; s6 = score6 - M; V6 = s6 * s6; s7 = score7 - M; V7 = s7 * s7; s8 = score8 - M; V8 = s8 * s8; cout << "the variance for these scores is: " << (double) (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N << "\n"; return 0; } linda

                    J Offline
                    J Offline
                    Justin Tay
                    wrote on last edited by
                    #9

                    Notice that if score3 to score8 is 0, the values for V3 to V8 is (0 - 3.5)^2 and you use these in your calculation for variance.

                    #include <iostream>
                    #include <vector>

                    using namespace std;

                    int main(void)
                    {
                    vector<double> scores;
                    double score;
                    double mean;
                    double total = 0;
                    double variance = 0;
                    int numberOfPeople;

                    cout << "Input the number of people in sample: ";
                    cin >> numberOfPeople;
                    
                    for(int n = 0; n < numberOfPeople; n++)
                    {
                    	cout << "Input the score for person " << n+1 << ": ";
                    	cin >> score;
                    	scores.push\_back(score);
                    	total += score;
                    }
                    
                    mean = total / numberOfPeople;
                    cout << "The mean for these scores is: " << mean << "\\n";
                    
                    for(int n = 0; n < numberOfPeople; n++)
                    {
                    	double temp = scores\[n\] - mean;
                    	variance += temp \* temp;
                    }
                    
                    variance /= numberOfPeople;
                    cout << "The variance for these scores is: " << variance << "\\n";
                    return 0;
                    

                    }

                    L 1 Reply Last reply
                    0
                    • J Justin Tay

                      Notice that if score3 to score8 is 0, the values for V3 to V8 is (0 - 3.5)^2 and you use these in your calculation for variance.

                      #include <iostream>
                      #include <vector>

                      using namespace std;

                      int main(void)
                      {
                      vector<double> scores;
                      double score;
                      double mean;
                      double total = 0;
                      double variance = 0;
                      int numberOfPeople;

                      cout << "Input the number of people in sample: ";
                      cin >> numberOfPeople;
                      
                      for(int n = 0; n < numberOfPeople; n++)
                      {
                      	cout << "Input the score for person " << n+1 << ": ";
                      	cin >> score;
                      	scores.push\_back(score);
                      	total += score;
                      }
                      
                      mean = total / numberOfPeople;
                      cout << "The mean for these scores is: " << mean << "\\n";
                      
                      for(int n = 0; n < numberOfPeople; n++)
                      {
                      	double temp = scores\[n\] - mean;
                      	variance += temp \* temp;
                      }
                      
                      variance /= numberOfPeople;
                      cout << "The variance for these scores is: " << variance << "\\n";
                      return 0;
                      

                      }

                      L Offline
                      L Offline
                      lindag1783
                      wrote on last edited by
                      #10

                      Hi there Thanks for that I appreciate your code, it is good. I think I need to study some more before attempting to write calculations like this:rolleyes: :-) linda

                      1 Reply Last reply
                      0
                      • L lindag1783

                        Hi Thanks for your reply. I have insert double instead of float but I still get the same mistakes. he mistakes sometimes are way off from the true answer, so onlsy sometimes are they close to the answer. In this case the answer came back way off again, by about 40! Anyway, I am sure my logic is correct so it mst be the code somehow. Please help. Here is my code: #include using namespace std; int main(void) { double M, N, total, score1, score2, score3, score4, score5, score6, score7, score8, V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance; cout << "Input the number of people in sample "; cin >> N; cout << "input the 1st score "; cin >> score1; cout << "input the 2nd score "; cin >> score2; cout << "input the 3rd score "; cin >> score3; cout << "input the 4th score "; cin >> score4; cout << "input the 5th score "; cin >> score5; cout << "input the 6th score "; cin >> score6; cout << "input the 7th score "; cin >> score7; cout << "input the 8th score "; cin >> score8; total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8; M = total / N; cout << "the mean for these scores is: " << M << "\n"; s1 = score1 - M; V1 = s1 * s1; s2 = score2 - M; V2 = s2 * s2; s3 = score3 - M; V3 = s3 * s3; s4 = score4 - M; V4 = s4 * s4; s5 = score5 - M; V5 = s5 * s5; s6 = score6 - M; V6 = s6 * s6; s7 = score7 - M; V7 = s7 * s7; s8 = score8 - M; V8 = s8 * s8; cout << "the variance for these scores is: " << (double) (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N << "\n"; return 0; } :confused: linda

                        G Offline
                        G Offline
                        Gary R Wheeler
                        wrote on last edited by
                        #11

                        1. You should initialize your variables score1 through score8 to 0.0. 2. Even though you let the user enter N, the number of samples, your calculation for the mean M and the variance assumes that all 8 scores are used. This means that the user must enter 0.0 for any scores greater than or equal to N for the calculations to work correctly.


                        Software Zen: delete this;

                        Fold With Us![^]

                        L 1 Reply Last reply
                        0
                        • G Gary R Wheeler

                          1. You should initialize your variables score1 through score8 to 0.0. 2. Even though you let the user enter N, the number of samples, your calculation for the mean M and the variance assumes that all 8 scores are used. This means that the user must enter 0.0 for any scores greater than or equal to N for the calculations to work correctly.


                          Software Zen: delete this;

                          Fold With Us![^]

                          L Offline
                          L Offline
                          lindag1783
                          wrote on last edited by
                          #12

                          Hi Thanks for your help. How do I initilize these variables, I am very new to this. So, this will only work for certain scores? Like, if I input a score that is higher then the mean then the variance will not work? (is that what you meant at the end?). How would I get it to work for all scores then, is there any special way or code I can use to fix this? Thanks once again. Much appreciated.:-D linda

                          G 1 Reply Last reply
                          0
                          • L lindag1783

                            Hi Thanks for your help. How do I initilize these variables, I am very new to this. So, this will only work for certain scores? Like, if I input a score that is higher then the mean then the variance will not work? (is that what you meant at the end?). How would I get it to work for all scores then, is there any special way or code I can use to fix this? Thanks once again. Much appreciated.:-D linda

                            G Offline
                            G Offline
                            Gary R Wheeler
                            wrote on last edited by
                            #13

                            using namespace std;
                            int main(void)

                            {

                            double M, N, total, score1, score2, score3, score4, score5, score6, score7, score8,
                            V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance;

                            score1 = 0.0;
                            score2 = 0.0;
                            score3 = 0.0;
                            score4 = 0.0;
                            score5 = 0.0;
                            score6 = 0.0;
                            score7 = 0.0;
                            score8 = 0.0;

                            cout << "Input the number of people in sample ";
                            cin >> N;
                            cout << "input the 1st score ";
                            cin >> score1;
                            cout << "input the 2nd score ";
                            cin >> score2;
                            cout << "input the 3rd score ";
                            cin >> score3;
                            cout << "input the 4th score ";
                            cin >> score4;
                            cout << "input the 5th score ";
                            cin >> score5;
                            cout << "input the 6th score ";
                            cin >> score6;
                            cout << "input the 7th score ";
                            cin >> score7;
                            cout << "input the 8th score ";
                            cin >> score8;
                            total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8;
                            M = total / N;
                            cout << "the mean for these scores is: " << M << "\n";
                            s1 = score1 - M;
                            V1 = s1 * s1;
                            s2 = score2 - M;
                            V2 = s2 * s2;
                            s3 = score3 - M;
                            V3 = s3 * s3;
                            s4 = score4 - M;
                            V4 = s4 * s4;
                            s5 = score5 - M;
                            V5 = s5 * s5;
                            s6 = score6 - M;
                            V6 = s6 * s6;
                            s7 = score7 - M;
                            V7 = s7 * s7;
                            s8 = score8 - M;
                            V8 = s8 * s8;
                            Variance = (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N;
                            cout << "the variance for these scores is: " << Variance << "\n";

                            return 0;

                            }

                            My modifications are highlighted in **bold**. To give you another view, here is how I would do this:

                            int main(void)
                            {
                            int N = 0;
                            double score[8];
                            for (int i = 0; i < 8; i++) score[i] = 0.0;
                            cout << "Enter number of people in sample [1-8] ";
                            cin >> N;
                            if ((N > 0) && (N <= 8)) {
                            double total = 0;
                            for (int i = 0; i < N; i++) {
                            cout << "Enter score " << (i + 1) << " ";
                            cin >> score[i];
                            total = total + score[i];
                            }
                            double mean = total / (double)N;
                            double variance = 0.0;
                            for (int i = 0; i < N; i++) {
                            variance = variance + ((score[i] - mean) * (score[i] - mean));
                            }
                            variance = variance / (double)N;
                            cout << "Variance is " << variance;
                            }
                            else {
                            cout << "Incorrect number of samples.";
                            }
                            }

                            First, instead of having 8 discrete variables score1 through score8, I use an array. This makes it easier to step through each value using a for loop. Next, I initialize the score array, even though the logic doesn't strictly require it; it's

                            L 2 Replies Last reply
                            0
                            • L lindag1783

                              Hi, I really appreciate your help here. The variance is worked out like the first example that you redirected my browser to regarding variance (the website about it). Example: Numberofscores(N)=2, score1=2, score2=5, all the other scores are input as 0 (seeing there is no scores). The mean is worked out as 2 + 5 / 2 = 3.5 (the program will do this calculation). Variance is worked out like this: (score1 - Mean) to the power of 2 + (score2 - Mean) to the power of 2 / 2 (N). I did not know what power was in C++ so I times it by itself. I think maybe there is something wrong with the compiler I am using as it will not recognise some commands. It does not return any errors but the calculations are wrong for the variance. Here is my code: #include using namespace std; int main(void) { double M, N, total, score1, score2, score3, score4, score5, score6, score7, score8, V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8; cout << "Input the number of people in sample "; cin >> N; cout << "input the 1st score "; cin >> score1; cout << "input the 2nd score "; cin >> score2; cout << "input the 3rd score "; cin >> score3; cout << "input the 4th score "; cin >> score4; cout << "input the 5th score "; cin >> score5; cout << "input the 6th score "; cin >> score6; cout << "input the 7th score "; cin >> score7; cout << "input the 8th score "; cin >> score8; total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8; M = total / N; cout << "the mean for these scores is: " << M << "\n"; s1 = score1 - M; V1 = s1 * s1; s2 = score2 - M; V2 = s2 * s2; s3 = score3 - M; V3 = s3 * s3; s4 = score4 - M; V4 = s4 * s4; s5 = score5 - M; V5 = s5 * s5; s6 = score6 - M; V6 = s6 * s6; s7 = score7 - M; V7 = s7 * s7; s8 = score8 - M; V8 = s8 * s8; cout << "the variance for these scores is: " << (double) (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N << "\n"; return 0; } linda

                              S Offline
                              S Offline
                              Saurabh Garg
                              wrote on last edited by
                              #14

                              Oh I got it now, your code is correct but you are using it incorrectly. You have written code for computing variance of 8 numbers but you are trying to use it for only two numbers. Let me explain. Code for mean is M = (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8) / N; when you give N =2, s1 = 2, s2 = 5 and rest equal to zero. mean becomes M = (s1 + s2 + 0 + 0 + 0 + 0 + 0 + 0) / N = (s1 + s2) = N which is correct so mean is computed correctly. Code for variance is [(s1-M)*(s2-M) + (s1-M)*(s2-M) + ... + (s8-M)*(s8-M)] / N. Now when you give N =2, s1 = 2, s2 = 5 and rest equal to zero. Variance becomes [(s1-M)*(s2-M) + (s1-M)*(s2-M) + (0-M)*(0-M) + ... + (0-M)*(0-M)] / N which is not what you want. Correct formula for variance of two numbers is [(s1-M)(s1-M) + (s2-M)(s2-M)]/2. I hope this helps. -Saurabh

                              1 Reply Last reply
                              0
                              • G Gary R Wheeler

                                using namespace std;
                                int main(void)

                                {

                                double M, N, total, score1, score2, score3, score4, score5, score6, score7, score8,
                                V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance;

                                score1 = 0.0;
                                score2 = 0.0;
                                score3 = 0.0;
                                score4 = 0.0;
                                score5 = 0.0;
                                score6 = 0.0;
                                score7 = 0.0;
                                score8 = 0.0;

                                cout << "Input the number of people in sample ";
                                cin >> N;
                                cout << "input the 1st score ";
                                cin >> score1;
                                cout << "input the 2nd score ";
                                cin >> score2;
                                cout << "input the 3rd score ";
                                cin >> score3;
                                cout << "input the 4th score ";
                                cin >> score4;
                                cout << "input the 5th score ";
                                cin >> score5;
                                cout << "input the 6th score ";
                                cin >> score6;
                                cout << "input the 7th score ";
                                cin >> score7;
                                cout << "input the 8th score ";
                                cin >> score8;
                                total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8;
                                M = total / N;
                                cout << "the mean for these scores is: " << M << "\n";
                                s1 = score1 - M;
                                V1 = s1 * s1;
                                s2 = score2 - M;
                                V2 = s2 * s2;
                                s3 = score3 - M;
                                V3 = s3 * s3;
                                s4 = score4 - M;
                                V4 = s4 * s4;
                                s5 = score5 - M;
                                V5 = s5 * s5;
                                s6 = score6 - M;
                                V6 = s6 * s6;
                                s7 = score7 - M;
                                V7 = s7 * s7;
                                s8 = score8 - M;
                                V8 = s8 * s8;
                                Variance = (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N;
                                cout << "the variance for these scores is: " << Variance << "\n";

                                return 0;

                                }

                                My modifications are highlighted in **bold**. To give you another view, here is how I would do this:

                                int main(void)
                                {
                                int N = 0;
                                double score[8];
                                for (int i = 0; i < 8; i++) score[i] = 0.0;
                                cout << "Enter number of people in sample [1-8] ";
                                cin >> N;
                                if ((N > 0) && (N <= 8)) {
                                double total = 0;
                                for (int i = 0; i < N; i++) {
                                cout << "Enter score " << (i + 1) << " ";
                                cin >> score[i];
                                total = total + score[i];
                                }
                                double mean = total / (double)N;
                                double variance = 0.0;
                                for (int i = 0; i < N; i++) {
                                variance = variance + ((score[i] - mean) * (score[i] - mean));
                                }
                                variance = variance / (double)N;
                                cout << "Variance is " << variance;
                                }
                                else {
                                cout << "Incorrect number of samples.";
                                }
                                }

                                First, instead of having 8 discrete variables score1 through score8, I use an array. This makes it easier to step through each value using a for loop. Next, I initialize the score array, even though the logic doesn't strictly require it; it's

                                L Offline
                                L Offline
                                lindag1783
                                wrote on last edited by
                                #15

                                Hi Thanks so much for all this help, it is much appreciated. This is my first program, good guess! I would like to eventually work out some more things but I think I must hit the books about it before that will happen, very much lacking in knowledge. Thank you all for all your help regarding this problem. :-> linda

                                1 Reply Last reply
                                0
                                • G Gary R Wheeler

                                  using namespace std;
                                  int main(void)

                                  {

                                  double M, N, total, score1, score2, score3, score4, score5, score6, score7, score8,
                                  V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance;

                                  score1 = 0.0;
                                  score2 = 0.0;
                                  score3 = 0.0;
                                  score4 = 0.0;
                                  score5 = 0.0;
                                  score6 = 0.0;
                                  score7 = 0.0;
                                  score8 = 0.0;

                                  cout << "Input the number of people in sample ";
                                  cin >> N;
                                  cout << "input the 1st score ";
                                  cin >> score1;
                                  cout << "input the 2nd score ";
                                  cin >> score2;
                                  cout << "input the 3rd score ";
                                  cin >> score3;
                                  cout << "input the 4th score ";
                                  cin >> score4;
                                  cout << "input the 5th score ";
                                  cin >> score5;
                                  cout << "input the 6th score ";
                                  cin >> score6;
                                  cout << "input the 7th score ";
                                  cin >> score7;
                                  cout << "input the 8th score ";
                                  cin >> score8;
                                  total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8;
                                  M = total / N;
                                  cout << "the mean for these scores is: " << M << "\n";
                                  s1 = score1 - M;
                                  V1 = s1 * s1;
                                  s2 = score2 - M;
                                  V2 = s2 * s2;
                                  s3 = score3 - M;
                                  V3 = s3 * s3;
                                  s4 = score4 - M;
                                  V4 = s4 * s4;
                                  s5 = score5 - M;
                                  V5 = s5 * s5;
                                  s6 = score6 - M;
                                  V6 = s6 * s6;
                                  s7 = score7 - M;
                                  V7 = s7 * s7;
                                  s8 = score8 - M;
                                  V8 = s8 * s8;
                                  Variance = (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N;
                                  cout << "the variance for these scores is: " << Variance << "\n";

                                  return 0;

                                  }

                                  My modifications are highlighted in **bold**. To give you another view, here is how I would do this:

                                  int main(void)
                                  {
                                  int N = 0;
                                  double score[8];
                                  for (int i = 0; i < 8; i++) score[i] = 0.0;
                                  cout << "Enter number of people in sample [1-8] ";
                                  cin >> N;
                                  if ((N > 0) && (N <= 8)) {
                                  double total = 0;
                                  for (int i = 0; i < N; i++) {
                                  cout << "Enter score " << (i + 1) << " ";
                                  cin >> score[i];
                                  total = total + score[i];
                                  }
                                  double mean = total / (double)N;
                                  double variance = 0.0;
                                  for (int i = 0; i < N; i++) {
                                  variance = variance + ((score[i] - mean) * (score[i] - mean));
                                  }
                                  variance = variance / (double)N;
                                  cout << "Variance is " << variance;
                                  }
                                  else {
                                  cout << "Incorrect number of samples.";
                                  }
                                  }

                                  First, instead of having 8 discrete variables score1 through score8, I use an array. This makes it easier to step through each value using a for loop. Next, I initialize the score array, even though the logic doesn't strictly require it; it's

                                  L Offline
                                  L Offline
                                  lindag1783
                                  wrote on last edited by
                                  #16

                                  Just one more question. This 0.0 declaration works well now it will work out the variance but only if all scores are input, what if you only have 2 scores, how do you leave the other fields blank. Thanks linda

                                  G 1 Reply Last reply
                                  0
                                  • L lindag1783

                                    Just one more question. This 0.0 declaration works well now it will work out the variance but only if all scores are input, what if you only have 2 scores, how do you leave the other fields blank. Thanks linda

                                    G Offline
                                    G Offline
                                    Gary R Wheeler
                                    wrote on last edited by
                                    #17

                                    My alternative handles this case. You could place an 'if' around each prompt, something like this:

                                    if (N >= 3) {
                                    cout << "Enter score 3 ";
                                    cin >> score3;
                                    }
                                    if (N >= 4) {
                                    cout << "Enter score 4 ";
                                    cin >> score4;
                                    }

                                    Doing that, you only prompt for and read in the number of scores that you need.


                                    Software Zen: delete this;

                                    Fold With Us![^]

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups