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
//Hung Nguyen
//01/14/2016
//HW 1 CSC 2430
//This program is to calculate mean and standard deviation
#include <iostream>
#include <cmath>
usingnamespace std;
//prototypes
double computeMean(double, int,constdouble);
double stdev(double,int);
int main() {
//declare variables
double variance = 0;
intconst 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, constdouble 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);
}
//mean function
double computeMean(double sum, int size, constdouble 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(constdouble 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(constdouble 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);
}
//Hung Nguyen
//01/14/2016
//HW 1 CSC 2430
//This program is to calculate mean and standard deviation
#include <iostream>
#include <cmath>
usingnamespace std;
//prototypes
double computeMean(int,constdouble);
double stdev(constdouble,int);
int main() {
//declare variables
double variance = 0;
intconst 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, constdouble array[]) {
double sum{};
for (int i = 0; i < size; i++) {
sum += array[i];
}
return sum/size;
}
//standard deviation calculattion
double stdev(constdouble 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);
}
Your function declaration/prototype parameters don't match your function definition parameters. In your declaration, you have an int and a constdouble, while in your definition you have an int and an array of constdoubles.
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;