Question on mean and standard deviation

I got the correct results of deviation but something wrong with the value sum.
Really confused about it.

#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void meanAndStDev(int size, char *arr,float *Mean,float *StDev){
float sumSqr=0.0;
float sum=0.0;
for(int i=0;i<size;i++){
sum = sum +arr[i];
}
for(int i=0;i<size;i++){
sumSqr = sumSqr+arr[i]*arr[i];
}
*Mean = sum/size;
*StDev = sqrt(sumSqr/size - pow(*Mean,2));
}
int main(){
float Mean,StDev;
int size;
char arr[size];
cout<< "Please type in how many numbers you want"<<endl;
cin>>size;
cout<<"Please type in the numbers"<<endl;
for(int i=0;i<size;i++){
cin>>arr[i];
}
meanAndStDev(size,arr,&Mean,&StDev);
cout<<"The mean is: "<<Mean<<endl;
cout<<"The Standard Deviation is: "<<StDev<<endl;
return EXIT_SUCCESS;
}

why are you using a char array?
works fine with a double array.
Last edited on
Thank you so much!!!
New problem raised up.
Please type in how many numbers you want
7
Please type in the numbers
1 2 3 4 5 6 7
The mean is: -nan
The Standard Deviation is: -nan

What's going on here?
closed account (2UD8vCM9)
You didn't post your new code.

It worked for me using
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
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void meanAndStDev(int size, double *arr, float *Mean, float *StDev){
	float sumSqr = 0.0;
	float sum = 0.0;
	for (int i = 0; i<size; i++){
		sum = sum + arr[i];
	}
	for (int i = 0; i<size; i++){
		sumSqr = sumSqr + arr[i] * arr[i];
	}
	*Mean = sum / size;
	*StDev = sqrt(sumSqr / size - pow(*Mean, 2));
}
int main(){
	float Mean, StDev;
	int size;
	double *arr;
	cout << "Please type in how many numbers you want" << endl;
	cin >> size;
	arr = new double[size];
	cout << "Please type in the numbers" << endl;
	for (int i = 0; i<size; i++){
		cin >> arr[i];
	}
	meanAndStDev(size, arr, &Mean, &StDev);
	delete arr;
	cout << "The mean is: " << Mean << endl;
	cout << "The Standard Deviation is: " << StDev << endl;
	system("pause");
	return EXIT_SUCCESS;
}
Last edited on
May I ask why you delete the array? Here is the new code which run quite well now.
#include <cmath>
#include <cstdlib>
#include <iostream>

using namespace std;
void meanAndStDev(int size, float *arr,float *Mean,float *StDev){
float sumSqr=0.0;
float sum=0.0;
for(int i=0;i<size;i++){
sum = sum +arr[i];
}
for(int i=0;i<size;i++){
sumSqr = sumSqr+arr[i]*arr[i];
}
*Mean = sum/size;
*StDev = sqrt(sumSqr/size - pow(*Mean,2));
}
int main(){
float Mean,StDev;
int size;
float arr[size];
cout<< "Please type in how many numbers you want"<<endl;
cin>>size;
cout<<"Please type in the numbers"<<endl;
for(int i=0;i<size;i++){
cin>>arr[i];
}
meanAndStDev(size,arr,&Mean,&StDev);
cout<<"The mean is: "<<Mean<<endl;
cout<<"The Standard Deviation is: "<<StDev<<endl;
return EXIT_SUCCESS;
}
Thank you so much!!!

Then why was i reported...?
Topic archived. No new replies allowed.