# Computer Programming C++ Variance?

• (2 Pages)
• 1
• 2

## 17 Replies - 3965 Views - Last Post: 02 November 2010 - 01:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=197904&amp;s=61653a25118e60e0262bae7c78c84e57&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tKc

Reputation: -2
• Posts: 38
• Joined: 25-October 10

# Computer Programming C++ Variance?

Posted 01 November 2010 - 10:41 PM

I need help in calculating the variance for randomly generated values (using srand(seed)). When i input 5 i should get 97359589.15265 but i am getting 1.29888e+010. I am almost positive i went wrong in the calculation of the variance but have no idea what to do in making it correct. Any help or suggestions would greatly be appreciated. (My code thus far):
```#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main (){
double getMean(int a[], int n);
double getVariance(int a[], int n);
const int N = 50;
unsigned int seed;
int arr[N];

cout << " Enter a positive integer seed value: \n";
cin >> seed;

srand(seed);

for (int k=0; k<N; k++)
{
arr[k] = rand();
}

cout << endl << " The Mean of the 50 random numbers is: " << getMean(arr, N)<< endl;
cout << endl << " The Variance of the 50 random numbers is: " << getVariance(arr, N)<< endl;
system ("PAUSE");
return 0;
}

double getMean(int a[], int n)
{
double sum(0);
for (int i=0; i<n; i++)
sum +=a[i];

return sum/n;
}

double getVariance(int a[], int n)
{
double sum1(0);
for (int i=0; i<n; i++)
sum1 +=a[i];

return ((pow((sum1-(sum1/n)),2))/n);
}

```

Is This A Good Question/Topic? 0

## Replies To: Computer Programming C++ Variance?

### #2 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Computer Programming C++ Variance?

Posted 01 November 2010 - 10:47 PM

How do you know what the result is for a random calculation?
You are trying to hit a moving target.

If you want to test this then
1 - Reduce the size of the array to 10.
2 - Create a test array with 10 fixed numbers.

Work out the correct result of the outputs by hand.
Run your code and see if you get the result you calculated.

If you don't then share that test version of your code and the calculated outputs so we can all test against a stationary target.

### #3 tKc

Reputation: -2
• Posts: 38
• Joined: 25-October 10

## Re: Computer Programming C++ Variance?

Posted 01 November 2010 - 11:15 PM

janotte, on 01 November 2010 - 09:47 PM, said:

How do you know what the result is for a random calculation?
You are trying to hit a moving target.

If you want to test this then
1 - Reduce the size of the array to 10.
2 - Create a test array with 10 fixed numbers.

Work out the correct result of the outputs by hand.
Run your code and see if you get the result you calculated.

If you don't then share that test version of your code and the calculated outputs so we can all test against a stationary target.

Yes, i have been using 5 as my input usually and i have been getting these as my random integers
54,28693,12255,24449,27660,31430,23927,17649,27472,32640,5114,28321,13533,17476,426,4307,11963,20107,12150,1231,12517,10992,31193,28211,25936,23849,4352,19083,26922,3131,17451,7969,4959,16110,7253,14615,1122,15487,2583,13234,25386,26006,10954,14585,27515,6519,26860,12415,12646,31612

As i stated in my original post, i have done the calculations and compared them to what my output is, i should get 97359589.15265 but i am getting 1.29888e+010. I know the results for a random calculation because everytime you input 5 the random output is the same, i know its not REALLY random but its close enough, and this way we can calculate what are results should be

### #4 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Computer Programming C++ Variance?

Posted 01 November 2010 - 11:27 PM

You've got more patience than me to add 50 numbers.

Okay change your function below to give you a running sum. Like this
```double getVariance(int a[], int n)
{
double sum(0);
for (int i=0; i<n; i++){
sum +=a[i];
cout << "a[" << i << "] = " << a[i] << "  // sum = " << sum << endl;
}

```

Does the running sum match you manual calculations?

### #5 tKc

Reputation: -2
• Posts: 38
• Joined: 25-October 10

## Re: Computer Programming C++ Variance?

Posted 01 November 2010 - 11:43 PM

janotte, on 01 November 2010 - 10:27 PM, said:

You've got more patience than me to add 50 numbers.

Okay change your function below to give you a running sum. Like this
```double getVariance(int a[], int n)
{
double sum(0);
for (int i=0; i<n; i++){
sum +=a[i];
cout << "a[" << i << "] = " << a[i] << "  // sum = " << sum << endl;
}

```

Does the running sum match you manual calculations?

yes, and my mean calculation is correct, but im sorry but i dont see how this helps me

### #6 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Computer Programming C++ Variance?

Posted 01 November 2010 - 11:53 PM

We are just going through looking for where the error starts.
Do you have a magic wand that can tell you about mistakes?
I don't. I need to walk boringly and carefully through each step and look for where it goes wrong.
If it is right then I can cross it off the list of suspects.
That's how you debug. And if you think I am going to add up 50 numbers to debug your code you are mistaken.

I can see the mistake coming but this is how you learn to debug.
By doing debugging.

So you now tell me that the sum is right so the error must be after that.
So we move to the next step.
```double getVariance(int a[], int n)
{
double sum1(0);
for (int i=0; i<n; i++)
sum1 +=a[i];

double sumMaths = sum1 - (sum1/n)
cout << "sumMaths = " << sumMaths << endl;

return ((pow(sumMaths,2))/n);
}

```

Run that.
Does that step of the program get the maths right?
Or is there a problem in there?

### #7 tKc

Reputation: -2
• Posts: 38
• Joined: 25-October 10

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:08 AM

same results, still getting 1.29888e+010 so it seams the maths is correct

This post has been edited by JackOfAllTrades: 02 November 2010 - 03:40 AM
Reason for edit:: Removed unnecessary quote

### #8 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:12 AM

tKc, on 02 November 2010 - 04:08 PM, said:

same results, still getting 1.29888e+010 so it seams the maths is correct

You told us in the first post that that was the wrong answer.

I am not changing any of your logic I am just showing you what your code is doing.
it is up to you to say whether or not things at each stage are right or wrong.

So time to make up your mind.
Is the value at sumMaths the correct value for that part of the calculation or not?

EDIT
By the way. No need to quote my whole posts back at me. We can all see what I wrote. It is right there above. If you want to respond to a specific point then quote that point. Not just the whole post for no good reason.

This post has been edited by janotte: 02 November 2010 - 12:16 AM

### #9 tKc

Reputation: -2
• Posts: 38
• Joined: 25-October 10

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:16 AM

sry, bit frustrated with this code atm, no the value it is outputting is still wrong, and iv tested these numbers multiple times and the variance should be 97359589.15265 (tested by hand and online calculators)

This post has been edited by JackOfAllTrades: 02 November 2010 - 03:40 AM
Reason for edit:: Removed unnecessary quote

### #10 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:25 AM

Right so what are you getting at sumMaths in your hand calculation?
What is the program producing?

They are different. Is that right?
How are they different?

I am not testing you. I haven't done the manual calculations so I don't know if the program is getting it right or not. I am relying on you to say whether or not things at each stage are good or not.

### #11 tKc

Reputation: -2
• Posts: 38
• Joined: 25-October 10

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:37 AM

oh i see your point, by hand my result ( and the correct result) is 822324, but sum math is producing 805878 a difference of 16446
oh snap, just realized, it is a difference of my mean

This post has been edited by JackOfAllTrades: 02 November 2010 - 03:40 AM
Reason for edit:: Removed unnecessary quote

### #12 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:48 AM

Right. Well we seem to have found where things are going wrong.

Let's drill down a bit further and see if we can find the exact part.
```ddouble getVariance(int a[], int n)
{
double sum1(0);
for (int i=0; i<n; i++)
sum1 +=a[i];

cout << "sum1 = " << sum1 << endl;
cout << "n = "    << n    << endl;
double divSum = sum1/n;
cout << "divSum = " << divSum << endl;
double sumMaths = sum1 - (sum1/n);
cout << "sumMaths = " << sumMaths << endl;

return ((pow(sumMaths,2))/n);
}

```

Some of those should come out right and some come out wrong.
You have already said that 'sum1' is right.
Therefore I suspect the problem will be that this bit 'sum1/n' which is 'divSum'
Is that bit coming out right?
How are we going in narrowing this down?

### #13 tKc

Reputation: -2
• Posts: 38
• Joined: 25-October 10

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 01:15 AM

good. well sum1 is correct and so is n and divsum, the only part here that is incorrect is sumMaths which is still 805878 and the variance which is still 1.29888e+010

This post has been edited by JackOfAllTrades: 02 November 2010 - 03:40 AM
Reason for edit:: Removed unnecessary quote

### #14 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 03:49 AM

So you are saying that the program

gets this bit right -> sum1
and gets this bit right -> (sum1/n)

But gets this wrong -> sum1 - (sum1/n)

So you are saying your computer is unable to accurately take one number away from another number.
If you are completely sure about that then I have no suggestion for you.

Anyone else got any ideas?

This post has been edited by janotte: 02 November 2010 - 04:07 AM

### #15 CTphpnwb

• D.I.C Lover

Reputation: 3336
• Posts: 11,814
• Joined: 08-August 08

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 06:33 AM

The program is calculating correctly, but it isn't calculating variance. According to wikipedia, you need to take the mean of the squares and subtract the square of the mean to get the variance.