Questions

Hi guys, I have two codes, both which perform the same function.

The first code is
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

/*This program was created to calculate the mean and standard deviation of four user entered values*/

//Beginning of Code
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
	//Declare the variables
	float number1, number2, number3, number4;

	//Enter a value for the first number
	cout << "Please enter a value for the first number:";
	cin >> number1;

	//Enter a value for the second number
	cout << "Please enter a value for the second number:";
	cin >> number2;

	//Enter a value for the third number
	cout << "Please enter a value for the third number:";
	cin >> number3;

	//Enter a value for the fourth number
	cout << "Please enter a value for the value number four:";
	cin >> number4;

	//The formula to calculate the mean of the four user entered integers
	double average = (number1 + number2 + number3 + number4)/4.0;

	//Output of the mean to the screen
	cout << "The average value is \n" << (number1 + number2 + number3 + number4)/4.0 << "\n";

	//The formula to calculate the numerator that will be used in the standard deviation equation
	double numerator = pow(number1 - average, 2.0) + pow(number2 - average, 2.0) + pow(number3 - average, 2.0) + pow(number4 - average, 2.0);

	//Output of the standard deviation to the screen using the numerator calculated above
	cout << "The standard deviation is \n" <<sqrt((numerator)/3.0) << "\n";

	
return 0;

}



The second code is
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
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	double a, b, c, d, sum, stdev, n;

	cout << "Please enter your four values" << endl;
	cin >> a>> b>> c>> d;

	cout << "Please enter n (the number of values)" << endl;
	cin >> n;

	sum = a + b + c + d;
	double avg = sum/n;
	double numerator = pow(a - avg, 2.0) + pow(b - avg, 2.0) + pow(c - avg, 2.0) + pow(d - avg, 2.0);

	cout << "The mean value is \n" << avg<< endl;
	cout << "The standard deviation is \n" <<sqrt(numerator/3) << endl;

	return 0;

}


Now my question arises in, when I tried to calculate the standard deviation straight out, WITHOUT the numerator, I kept getting an error. Why do I need to seperate it out into a numerator to calculate the std deviation?

My second question was why can I not include average in the first double used, where a , b, c, d etc are stored?

It's not for homework purposes, it is something that Ihave already done and submitted. I merely want to learn. Thanks!
You shouldn't get an error. Maybe you didn't put parenthesis at the right positions.

sqrt((pow(a - avg, 2.0) + pow(b - avg, 2.0) + pow(c - avg, 2.0) + pow(d - avg, 2.0)) / 3)


You say this doesn't work? It should work
double a, b, c, d, sum, stdev, n, avg;

Last edited on
You might have tried to initialize avg to sum/n... that won't work... Otherwise your programs are fine.... But what I don't get is, if you're calculating average of four numbers, why do you take input for n? Shouldn't it be 4, user independent?
I'm not sure I understand your question Caprico. I just put the variable n in the program to remind the user that it was present.
I mean, consider this:

Average of two numbers: (a+b)/2
Average of three numbers: (a+b+c)/3
Average of four numbers: (a+b+c+d)/4

The denominator is automatically decided by the number of values you're calculating average for... So why take an input for it? Just makes your program vulnerable to incorrect results...
Topic archived. No new replies allowed.