I'm new to programming, so bear with me. Part of my code requires me to calculate the mean of all the elements of a vector but I am having problems putting my vector into the function 'mean'.
#include <iostream>
#include <vector>
#include <numeric>
usingnamespace std;
double mean(const vector<long>& v);
int main()
{
vector <long> primes;
/*
Fill vector primes with first N primes using push back
*/
//mean
cout << "The mean of all elements is " << mean( primes ) << endl;
return 0;
}
double mean(const vector<long>& v)
{
double sum_of_elems=0, mean;
sum_of_elems = accumulate(v.begin(),v.end(),0);
mean = sum_of_elems / v.size();
return mean;
}
After messing around with it, this code now runs! Just need to get it to return the value properly.
#include <iostream>
#include <vector>
#include <numeric>
usingnamespace std;
int mean(const vector<long>& v);
int main()
{
vector <long> primes;
/*
Fill vector primes with first N primes using push back
*/
primes.push_back(2);
//mean
cout << "The mean of all elements is " << mean(primes) << endl;
return 0;
}
int mean(const vector<long>& v)
{
int sum_of_elems=0, mean;
sum_of_elems = accumulate(v.begin(),v.end(),0);
mean = sum_of_elems / v.size();
return mean;
}