# Variance and Standard Deviation

• (3 Pages)
• 1
• 2
• 3

## 31 Replies - 90138 Views - Last Post: 03 April 2010 - 11:13 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=20885&amp;s=7b22cc656bde4802e241874cbcbe96c1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Rolf99

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 18-October 06

# Variance and Standard Deviation

Posted 15 November 2006 - 12:54 AM

Hi,
I'm working with GCC (g++) on a gentoo linux. I wrote a c++ code to calculate mean, variance and standard deviation from 8 numbers, I feel the code is too long, I also had troube with funct square. sqrt is functioning, but how to get the square of a number. Second thing, how to let the program request, how many numbers to calculate( in this code 8 numbers predefined). Any ideas??

>>>>>>>>>>>>>>>code>>>>>>>>>>>>>>>>>>>>
```#include <iostream>

using namespace std;
int main()
{
int n , j;
double max[8] , _sum=0 , variance;
for(n=0;n<=7;n++)
{
cout << "Type Numbers: " << endl;
cin >> max[n];
}
n=0;
cout << endl;
for(j=0;j<=7;j++)
{
cout << max[n]  << endl;
_sum=_sum+max[n];
n++;
}
double mean = _sum/(n);
cout << endl << "The Sum is: " << _sum;
cout << endl << "The mean is: " << mean;
n=0;
j=0;
_sum=0;
for(j=0;j<=7;j++)
{
max[n]=(max[n]-mean) * (max[n]-mean);
_sum=_sum+max[n];
n++;
}
variance=_sum/(n-1);
cout << endl << "Variance is: " << variance;
cout << endl << "Standard Deviation is: " << sqrt(variance) << endl;
cout << endl;
}

```

>>>>>>>>>>>>code>>>>>>>>>>>>>>>>>

many thanks, friends

Rolf

Is This A Good Question/Topic? 1

## Replies To: Variance and Standard Deviation

### #2 BitByte

• D.I.C Head

Reputation: 11
• Posts: 194
• Joined: 09-August 06

## Re: Variance and Standard Deviation

Posted 15 November 2006 - 03:30 AM

To use sqrt, use need to include

```#include <cmath>
```

Was This Post Helpful? 0

### #3 Rolf99

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 18-October 06

## Re: Variance and Standard Deviation

Posted 15 November 2006 - 08:05 AM

BitByte, on 15 Nov, 2006 - 03:30 AM, said:

To use sqrt, use need to include

```#include <cmath>
```

Thanks for the reply BitByte,
I tried #include <cmath> with no success

is there anything that looks better than:

max[n]=(max[n]-mean) * (max[n]-mean);

thanks again

Rolf
Was This Post Helpful? 0

### #4 Amadeus

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: Variance and Standard Deviation

Posted 15 November 2006 - 08:55 AM

You can use the pow() function, and provide it the term to be squared and an exponent of 2.
http://www.cplusplus.../cmath/pow.html
Was This Post Helpful? 0

### #5 Rolf99

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 18-October 06

## Re: Variance and Standard Deviation

Posted 15 November 2006 - 04:53 PM

Thanks guys,
I think it's now OK; Code after some changes

```#include <iostream>
#include <cmath>

using namespace std;
int main()
{
int n , j ,y;
double _sum=0 , variance;
cout << "\nHow many numbers to type? " << endl;
cin >> y;
double max[y];
for(n=0;n<=y-1;n++)
{
cout << "Type next number: ";
cin >> max[n];
}
n=0;
cout << endl;
cout << "Numbers are:\t";
for(j=0;j<=y-1;j++)
{
cout << max[n] << "\t";
_sum+=max[n];
n++;
}
double mean = _sum/n;
cout << endl << "The Sum is: "<< _sum << endl;
cout << "The mean is: " << mean << endl;
n=0;
j=0;
_sum=0;
for(j=0;j<=y-1;j++)
{
max[n]=pow((max[n]-mean),2);
_sum+=max[n];
n++;
}
variance=_sum/(n-1);
cout << "Variance is: " << variance << endl;
cout << "Standard Deviation is: " << sqrt(variance) << "\n\n";
}

```

cheers

Rolf

This post has been edited by Rolf99: 15 November 2006 - 05:16 PM

Was This Post Helpful? 0

### #6 Xing

• D.I.C Addict

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: Variance and Standard Deviation

Posted 16 November 2006 - 12:05 AM

```cout << "\nHow many numbers to type? " << endl;
cin >> y;
double max[y];
```

You cannot create arrays like this in C++.
Was This Post Helpful? 0

### #7 Rolf99

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 18-October 06

## Re: Variance and Standard Deviation

Posted 16 November 2006 - 02:43 AM

Hi Xing
I compiled it successfuly on linux and it functions

here is the output:

How many numbers to type?
8
Type next number: 15
Type next number: 20
Type next number: 21
Type next number: 20
Type next number: 15
Type next number: 36
Type next number: 25
Type next number: 15

Numbers are: 15 20 21 20 15 36 25 15
The Sum is: 167
The mean is: 20.875
Variance is: 50.125
Standard Deviation is: 7.0799

am I breaking C++ rules?

Rolf

This post has been edited by Rolf99: 16 November 2006 - 02:51 AM

Was This Post Helpful? 0

### #8 Xing

• D.I.C Addict

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: Variance and Standard Deviation

Posted 16 November 2006 - 03:54 AM

Rolf99, on 16 Nov, 2006 - 03:13 PM, said:

am I breaking C++ rules?

Yes.
ISO C++ doesn't support variable length arrays. That's the feature of C99 only.

Rolf99, on 16 Nov, 2006 - 03:13 PM, said:

I compiled it successfuly on linux and it functions

It's working because this feature is supported by your compiler as an extension. Try compiling with -Wall and -pedantic flags.
Was This Post Helpful? 0

### #9 Rolf99

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 18-October 06

## Re: Variance and Standard Deviation

Posted 16 November 2006 - 04:59 AM

Xing, on 16 Nov, 2006 - 03:54 AM, said:

Rolf99, on 16 Nov, 2006 - 03:13 PM, said:

am I breaking C++ rules?

Yes.
ISO C++ doesn't support variable length arrays. That's the feature of C99 only.

Rolf99, on 16 Nov, 2006 - 03:13 PM, said:

I compiled it successfuly on linux and it functions

It's working because this feature is supported by your compiler as an extension. Try compiling with -Wall and -pedantic flags.

Thanks Xing,
I 'll try to change the code

nice day

Rolf
Was This Post Helpful? 0

### #10 Rolf99

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 18-October 06

## Re: Variance and Standard Deviation

Posted 16 November 2006 - 08:19 AM

OK I think I got it now,
with Borland C++ 5.5, fixed size data (arrays), waisted memory, but at least it works.

```#include <iostream.h>
#include <math.h>

int main()
{
int n , j ,y;
double _sum=0 , variance;
cout << "\nHow many numbers to type? " << endl;
cin >> y;
double max[100];
for(n=0;n<y;n++)
{
cout << "Type next number: ";
cin >> max[n];
}
n=0;
cout << endl;
cout << "Numbers are:\t";
for(j=0;j<y;j++)
{
cout << max[n] << "\t";
_sum+=max[n];
n++;
}
double mean = _sum/n;
cout << endl << "The Sum is: "<< _sum << endl;
cout << "The mean is: " << mean << endl;
n=0;
_sum=0;
for(j=0;j<y;j++)
{
max[n]=pow((max[n]-mean),2);
_sum+=max[n];
n++;
}
variance=_sum/(n-1);
cout << "Variance is: " << variance << endl;
cout << "Standard Deviation is: " << sqrt(variance) << "\n\n";
}

```

Grateful for any suggestions

Rolf

This post has been edited by Rolf99: 16 November 2006 - 08:22 AM

Was This Post Helpful? 0

### #11 Amadeus

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: Variance and Standard Deviation

Posted 16 November 2006 - 08:27 AM

Well, in order to eliminate the need for wasted memory, you could allocate an array of the proper size based on user input with the new operator (delete when done).
Was This Post Helpful? 0

### #12 BitByte

• D.I.C Head

Reputation: 11
• Posts: 194
• Joined: 09-August 06

## Re: Variance and Standard Deviation

Posted 16 November 2006 - 08:28 AM

I've got a suggestion, why not dynamically allocate the memory needed:

```double *max = new double [y];
```

Nothing wasted then

Edit: as Amadeus said lol

This post has been edited by BitByte: 16 November 2006 - 08:29 AM

Was This Post Helpful? 0

### #13 Rolf99

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 18-October 06

## Re: Variance and Standard Deviation

Posted 16 November 2006 - 09:12 AM

BitByte, on 16 Nov, 2006 - 08:28 AM, said:

I've got a suggestion, why not dynamically allocate the memory needed:

```double *max = new double [y];
```

Nothing wasted then

Edit: as Amadeus said lol

Thanks
the code compiles nicely

grateflul

Rolf
Was This Post Helpful? 0

### #14 BitByte

• D.I.C Head

Reputation: 11
• Posts: 194
• Joined: 09-August 06

## Re: Variance and Standard Deviation

Posted 16 November 2006 - 09:23 AM

Did you remember to delete?

```delete [] max;
```

Was This Post Helpful? 0

### #15 Xing

• D.I.C Addict

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: Variance and Standard Deviation

Posted 16 November 2006 - 09:24 AM

Better use vector
Was This Post Helpful? 0

• (3 Pages)
• 1
• 2
• 3

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }