Returning function value error using array/vector

Looking for someone to point me in the right direction for correcting the errors I am getting with my code. I already did some extensive searching and couldn't figure it out and I really just need to know how to properly write the call to function using an array/vector.

The program is supposed to use a vector to obtain a variable amount of test scores, and then the program is supposed to calculate standard deviation, mean, median, min/max. This is where I'm at now and I'm stuck because of the error I keep getting when I try to return the value from the standard deviation function.

These are the errors I get:
Error C2664 'double calculateSD(double [],int)': cannot convert argument 1 from 'std::vector<double,std::allocator<_Ty>>' to 'double []'

Error (active) no suitable conversion function from "std::vector<double, std::allocator<double>>" to "double *" exists

and for some reason in the min/max output lines it is saying that " << " are unknown operands

I turned the lines that were giving me errors into comment lines and the rest of the code will run fine.

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


#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
using namespace std;


// Standard deviation function prototype
void calculateSD(double&, int);

int main()
{
	int num_students, i;
	vector<double>grades;

	cout << "How many test scores will you be entering? (Must be greater than 10): ";
	cin >> num_students;
	cout << endl;

	//set array size
	grades.resize(num_students);

	cout << "Enter students' test scores: " << endl;
	for (i = 0; i < num_students; i++)
	{
		cout << "Student " << i + 1 << ": ";
		cin >> grades[i];
	}

	cout << endl;
	cout << "Mean: " << accumulate(grades.begin(), grades.end(), 0.0) / grades.size() << endl;

	cout << "Minimum: " << min_element(grades.begin(), grades.end(), 0.0) << endl;
	cout << "Maximum:" << max_element(grades.begin(), grades.end(), 0.0) << endl;

	cout << calculateSD(grades, num_students);

	system("pause");
	return 0;
}

//	Calculate and return standard deviation
void calculateSD(vector<double>& grades, int num_students)
{
	int i;
	double sum = 0.0, mean, standardDeviation = 0.0;

	for (i = 0; i < num_students; i++)
		sum += grades[i];

	mean = sum / num_students;

	for (i = 0; i < num_students; i++)
		standardDeviation = pow(grades[i] - mean, 2);

	sqrt(standardDeviation / num_students);
}

Last edited on
update*

Okay so I realized I had to pass the vector by reference and I changed my code accordingly, but I am still getting this error:
Error C2664 'double calculateSD(double &,int)': cannot convert argument 1 from 'std::vector<double,std::allocator<_Ty>>' to 'double &'

and it's still from line 37

update again*
added void to the function
Still getting errors line 36, 37, 38
Last edited on
calculateSD() function signature in the declaration (line 11, OP) don't match that in the definition (line 45)
also you could make your program terser by asking how many scores first and then declaring a std::vector of that size rather than declaring an empty vector first, then asking the qs and finally resizing the vector – ties in with the principle of declaring variables as close as possible to where they're needed
and some other, handy ways to calculate standard deviation can be found here:
http://stackoverflow.com/questions/7616511/calculate-mean-and-standard-deviation-from-a-vector-of-samples-in-c-using-boos
I got rid of the compile errors and explained them in comments in the code below.

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
using namespace std;


// Standard deviation function prototype
double calculateSD(vector<double>&, int); // changed declaration to match definition
                                          // double& --> vector<double>&
                                          // return type: void --> double

int main()
{
	int num_students, i;
	vector<double>grades;

	cout << "How many test scores will you be entering? (Must be greater than 10): ";
	cin >> num_students;
	cout << endl;

	//set array size
	grades.resize(num_students);

	cout << "Enter students' test scores: " << endl;
	for (i = 0; i < num_students; i++)
	{
		cout << "Student " << i + 1 << ": ";
		cin >> grades[i];
	}

	cout << endl;
	cout << "Mean: " << accumulate(grades.begin(), grades.end(), 0.0) / grades.size() << endl;

	cout << "Minimum: " << *min_element(grades.begin(), grades.end()) << endl; // deferenced iterator
	cout << "Maximum:" << *max_element(grades.begin(), grades.end()) << endl;  // deferenced iterator
	                                                                           // 0.0 is not a functor; removed

	cout << calculateSD(grades, num_students);

	system("pause");
	return 0;
}

//	Calculate and return standard deviation
double calculateSD(vector<double>& grades, int num_students) // made function return double
{
	int i;
	double sum = 0.0, mean, standardDeviation = 0.0;

	for (i = 0; i < num_students; i++)
		sum += grades[i];

	mean = sum / num_students;

	for (i = 0; i < num_students; i++)
		standardDeviation = pow(grades[i] - mean, 2);

	return sqrt(standardDeviation / num_students); // added return
}
Last edited on
Thank you very much ! I actually figured out how to use the iterator vector variables and converting the outputs before you posted !

I figured out what was wrong with my function. I wasn't storing the final calculation into a variable, so I just created a variable v and stored the call to function value into another variable SD and then I printed SD.

It feels so good to figure things out on your own even though it takes forever lol...

My code runs great now though !!! Even added the median function by myself
Topic archived. No new replies allowed.