Mean and Standard Deviation

Recently i'm working on Mean and standard Deviation program, my question is how can i move the for loop of the mean into the mean function, and for loop of the variance into the standard deviation function? Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  //Hung Nguyen
//01/14/2016
//HW 1 CSC 2430
//This program is to calculate mean and standard deviation

#include <iostream>
#include <cmath>
using namespace std;

//prototypes
double computeMean(double, int,const double);
double stdev(double,int);

int main() {
	//declare variables
	double variance = 0;
	int const size = 10;
	double array[size];
	double sum = 0;
	for (int i = 0; i < size; i++)
	{
		cout << "Please enter element number " << i + 1 << endl;
		cin >> array[i];
	}
	//Using for loop to calculate both mean and standard deviation
	
	/*for (int i = 0; i < size; i++)
	{
		variance += pow(array[i] - computeMean(sum,size),2);
		
	}*/

	cout << "Mean of the array is " << computeMean(sum, size, array[size]) << endl;
	cout << "std dev of the array is " << stdev(variance,size) << endl;


	return 0;
}
//mean function
double computeMean(double sum, int size, const double array[]) {
	for (int i = 0; i < size; i++)	{
		sum = sum + array[i];
	}
	double mean = sum / size;
	return mean;
}
//standard deviation calculattion
double stdev(double variance,int size) {
	return sqrt(variance/size); 
}
1
2
3
4
5
6
7
8
//mean function
double computeMean(double sum, int size, const double array[]) {
	for (int i = 0; i < size; i++)	{
		sum = sum + array[i];
	}
	double mean = sum / size;
	return mean;
}

sum doesn't have to be a parameter to this function. If you declare it as a local variable, it will work the same. You can also shorten the compounding of sum, using the compound assignment operator(+=). Also, no need to declare another variable just for the return value.
1
2
3
4
5
6
7
8
//mean function
double computeMean(const double array[], int size) {
    double sum{};
    for (int i = 0; i < size; i++)  {
        sum += array[i];
    }
    return sum / size;
}


how can i move the for loop of the mean into the mean function, and for loop of the variance into the standard deviation function?

Simply pass in the array as an argument. Also, call computeMean once and store it in a variable, so that you don't call it unnecessarily in the for-loop. You forgot to square root the result, to get the standard deviation.
1
2
3
4
5
6
7
8
9
10
//standard deviation calculattion
double stdev(const double array[], int size) {
    double mean{ computeMean(array, size) },
        variance{};
    for (int i = 0; i < size; i++)
    {
        variance += pow(array[i] - mean, 2); 
    }
    return sqrt(variance / size);
}
Ok so now is my code so far, but i get overload for the prototypes of computeMean and stdev.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//Hung Nguyen
//01/14/2016
//HW 1 CSC 2430
//This program is to calculate mean and standard deviation

#include <iostream>
#include <cmath>
using namespace std;

//prototypes
double computeMean(int,const double);
double stdev(const double,int);

int main() {
	//declare variables
	double variance = 0;
	int const size = 10;
	double array[size];
	double sum = 0;
	for (int i = 0; i < size; i++)
	{
		cout << "Please enter element number " << i + 1 << endl;
		cin >> array[i];
	}
	//Using for loop to calculate both mean and standard deviation
	
	/*for (int i = 0; i < size; i++)
	{
		variance += pow(array[i] - computeMean(sum,size),2);
		
	}*/

	cout << "Mean of the array is " << computeMean(size,array[size]) << endl;
	cout << "std dev of the array is " << stdev(variance,size) << endl;


	return 0;
}
//mean function
double computeMean(int size, const double array[]) {
	double sum{};
	for (int i = 0; i < size; i++)	{	
		sum += array[i];
	}
	return sum/size;
}
//standard deviation calculattion
double stdev(const double array[], int size) {
	double mean{ computeMean(array[size], size) },
		variance{};
	for (int i = 0; i < size; i++)
	{
		variance += pow(array[i] - mean, 2);
	}
	return sqrt(variance / size);
}
1
2
3
4
// declaration / prototype
double computeMean(int,const double);
// definition
double computeMean(int size, const double array[]) {


Same with your stdev function.
What do you mean by that?
Your function declaration/prototype parameters don't match your function definition parameters. In your declaration, you have an int and a const double, while in your definition you have an int and an array of const doubles.

Also, you're using the functions incorrectly. For both functions, we pass the array and its size as arguments and we return the mean and the standard deviation for computeMean and stdev, respectively.
This
1
2
	cout << "Mean of the array is " << computeMean(size,array[size]) << endl;
	cout << "std dev of the array is " << stdev(variance,size) << endl;

is meant to be this
1
2
	cout << "Mean of the array is " << computeMean(array, size) << endl;
	cout << "std dev of the array is " << stdev(array, size) << endl;

assuming your function declarations are correct.
Last edited on
Topic archived. No new replies allowed.