Average/Standard Deviation Problem

Hello everyone, first post here. I am trying to write a program to calculate the average and standard deviation and I am having a little bit of trouble. I had the code working, but the standard deviation output was incorrect so I tried re working it, but now it is telling me that line 52(argument of type "int" is incompatible with parameter type "int", and too many arguments in the function call). Red lines under vals and sum. Any insight would be much appreciated, I am new to this and is the first semester I have been working with Visual Studio and C++.
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cmath>

using namespace std;

const int VALSIZE = 100;

void checkVals(int &numVals);
void displayVals(int vals[], int numVals, int sum);
void getVals(int vals[], int numVals);
double average(int sum, int numVals);
double stanDev(int vals[], double mean, int numVals);

int main()
{
	int numVals;
	int sum = 0;
	int vals[VALSIZE];

	cout << "Please enter the number of values you would like(1-100): ";
	cin >> numVals;
	checkVals(numVals);

	getVals(vals, numVals);
	cout << endl;
	displayVals(vals, numVals, sum);
	cout << endl;



}

void getVals(int vals[], int numVals)
{
	int index;
	for (index = 0; index<numVals; index++) {
		cout << "Please enter value #" << index + 1 << ": ";
		cin >> vals[index];
	}
}

void displayVals(int vals[], int numVals, int sum)
{
	int index;
	for (index = 0; index<numVals; index++) {
		cout << "Here is value #" << index + 1 << ": ";
		cout << vals[index] << ".\n";
	}

	cout << endl;
	cout << "The average of your values is: ";
	cout << average(vals, numVals, sum) << ".\n";

	cout << endl;
	cout << "The standard deviation of your values is: ";
	cout << stanDev(vals, numVals, sum) << ".\n";

}

void checkVals(int &numVals)
{
	while ((numVals < 0) || (numVals > 100)) {
		cout << "That is an invalid value entered!!!\n";
		cout << "A valid value is between 0 and 100 inclusive!!!\n";
		cout << "Please reenter the number of values that you would like!\n";
		cin >> numVals;
	}
}


double average(int sum, int numVals)
{
	double dsum = (double)sum;
	double dnumVals = (double)numVals;
	return dsum / dnumVals;
}


double stanDev(int vals[], double mean, int numVals)
{
	double sum = 0, dVals = 0, value = 0, variance = 0;
	for (int i = 0; i<numVals; i++)
	{
		dVals = (double)vals[i];
		value = (dVals - mean)*(dVals - mean);
		sum += value;
		variance = sum / (numVals);
	}
	return sqrt(variance);
}
Last edited on
In order to calculate an average, you need a list of numbers, and also need to know how many numbers are in that list.

Thus function average() requires two parameters, the array, and the number of element in that array. In your code that would be vals and numVals.

The variable sum is part of the internal workings of the function, it should not be a parameter, and does not need to exist at all in main().
I removed sum in main, I still get the first error for vals.
Well, you need to make sure three things match:
• the function prototype (at the top of the code)
• the function definition towards the bottom of the code
• the function call - from main()

My version of your code used
 
double average(int vals[], int numVals)

and passed the corresponding variables in the function call.
Last edited on
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cmath>

using namespace std;

const int VALSIZE = 100;

void checkVals(int &numVals);
void displayVals(int vals[], int numVals, int sum);
void getVals(int vals[], int numVals);
double average(int vals[], int numVals);
double stanDev(int vals[], double mean, int numVals);

int main()
{
	int numVals;
	int sum = 0;
	int vals[VALSIZE];

	cout << "Please enter the number of values you would like(1-100): ";
	cin >> numVals;
	checkVals(numVals);

	getVals(vals, numVals);
	cout << endl;
	displayVals(vals, numVals, sum);
	cout << endl;



}

void getVals(int vals[], int numVals)
{
	int index;
	for (index = 0; index<numVals; index++) {
		cout << "Please enter value #" << index + 1 << ": ";
		cin >> vals[index];
	}
}

void displayVals(int vals[], int numVals, int sum)
{
	int index;
	for (index = 0; index<numVals; index++) {
		cout << "Here is value #" << index + 1 << ": ";
		cout << vals[index] << ".\n";
	}

	cout << endl;
	cout << "The average of your values is: ";
	cout << average(vals, numVals) << ".\n";

	cout << endl;
	cout << "The standard deviation of your values is: ";
	cout << stanDev(vals, numVals, sum) << ".\n";

}

void checkVals(int &numVals)
{
	while ((numVals < 0) || (numVals > 100)) {
		cout << "That is an invalid value entered!!!\n";
		cout << "A valid value is between 0 and 100 inclusive!!!\n";
		cout << "Please reenter the number of values that you would like!\n";
		cin >> numVals;
	}
}


double average(int vals[], int numVals)
{
	double dsum = (int)vals;
	double dnumVals = (double)numVals;
	return dsum / dnumVals;
}


double stanDev(int vals[], double mean, int numVals)
{
	double sum = 0, dVals = 0, value = 0, variance = 0;
	for (int i = 0; i<numVals; i++)
	{
		dVals = (double)vals[i];
		value = (dVals - mean)*(dVals - mean);
		sum += value;
		variance = sum / (numVals);
	}
	return sqrt(variance);


Now I am getting an error: displayVals: function does not take 2 arguments(line 26)....
Now I am getting an error: displayVals: function does not take 2 arguments(line 26)....

There maybe something out of sync here. I compiled your latest code with no errors.

I used this input:
5 600  470  170  430  300

which generated this output.

Here is value #1: 600.
Here is value #2: 470.
Here is value #3: 170.
Here is value #4: 430.
Here is value #5: 300.

The average of your values is: 458610.

The standard deviation of your values is: 0.


I'd suggest you need to sort out the calculation of the average before proceeding with the standard deviation.

1
2
3
4
5
6
 double average(int vals[], int numVals)
{
	double dsum = (int)vals;
	double dnumVals = (double)numVals;
	return dsum / dnumVals;
}

This function is a bit messed up. only the last line, where the total is divided by the number of values is correct.

Before that , the function needs a loop to add up the total value of all of the elements. So long as the variable you use to accumulate the total is of type double, there will be no need for any of the other casts to double. (a double divided by an int will automatically use floating-point division, and give a result of type double).

The first line, double dsum = (int)vals; is doing some weird casting of a pointer to an integer, for no good reason. You don't need that line.

Topic archived. No new replies allowed.