
February 2nd, 2009, 04:21 PM
#1
help need proper equation!
For this program I have to obtain the variance. The variance is obtained by subtracting the calcAverage from each value in testvals, squaring the values obtained, adding them, and dividing by the number of elements in testvals. Basically the last line of code I need help because I can't figure out how to do all the math with one formula. This is the formula I need help with:
Code:
variance = ((testvals[i]  (testvals, MAXELS)));
I know thats not right obviously. So any help would be greatly appreciated
Heres what I have so far:
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int MAXELS=14;
float calcAverage(int [MAXELS]); //function prototype
float variance(int [MAXELS]); //function prototype
int main()
{
int testvals[MAXELS] = {89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73};
cout << "The average of these numbers is: " <<calcAverage(testvals) <<endl;
cout << "The variance of these number is: " <<variance(testvals) <<endl;
system("pause");
return 0;
}
//find the average value
float calcAverage(int testvals[MAXELS])
{
int i;
float calcAverage, total=0;
for(i=0; i < MAXELS; i++)
{
total=total+testvals[i];
}
calcAverage=total/MAXELS;
return calcAverage;
}
//find the variance
float variance(int testvals[MAXELS])
{
int i;
float calcAverage, sqd, variance=0;
for (i=0; i < MAXELS; i++)
{
variance = ((testvals[i]  (testvals, MAXELS)));
}
return variance;
}
Please help me out! I would really appreciate it
Last edited by Marc G; February 3rd, 2009 at 03:07 AM.
Reason: Added code tags

February 2nd, 2009, 04:34 PM
#2
Re: help need proper equation!
I can't figure out how to do all the math with one formula
Is doing the calculation in multiple steps not an option?

February 2nd, 2009, 07:14 PM
#3
Re: help need proper equation!
Actually it is an option but I just don't know how to do it at all.
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.
Featured
