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);
}
|