#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); }

## 17 Replies - 4107 Views - Last Post: 02 November 2010 - 01:43 PM

### #1

# Computer Programming C++ Variance?

Posted 01 November 2010 - 10:41 PM

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

### #2

## Re: Computer Programming C++ Variance?

Posted 01 November 2010 - 10:47 PM

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

## Re: Computer Programming C++ Variance?

Posted 01 November 2010 - 11:15 PM

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

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

## Re: Computer Programming C++ Variance?

Posted 01 November 2010 - 11:27 PM

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

## Re: Computer Programming C++ Variance?

Posted 01 November 2010 - 11:43 PM

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

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

## Re: Computer Programming C++ Variance?

Posted 01 November 2010 - 11:53 PM

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

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:08 AM

This post has been edited by **JackOfAllTrades**: 02 November 2010 - 03:40 AM

Reason for edit:: Removed unnecessary quote

### #8

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:12 AM

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

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

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:16 AM

This post has been edited by **JackOfAllTrades**: 02 November 2010 - 03:40 AM

Reason for edit:: Removed unnecessary quote

### #10

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:25 AM

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

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:37 AM

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

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 12:48 AM

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

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 01:15 AM

**JackOfAllTrades**: 02 November 2010 - 03:40 AM

Reason for edit:: Removed unnecessary quote

### #14

## Re: Computer Programming C++ Variance?

Posted 02 November 2010 - 03:49 AM

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