Reputation: 1
Its probably an easy solution, but I am still new to programming, it says that my ivec is undeclared. Here were the instructions and my code:
This is my code
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
//prototype
double mean(vector<int> & data);
int main()
{
const int SIZE = 10;
vector<int> ivec(SIZE);
int srand(time(0));
double m;
for(int i = 0; i < ivec.size(); i++)
{
ivec[i] = rand() % 99 + 1;
}
m = mean(ivec);
for(int i = 0; i < ivec.size(); i ++)
{
ivec[i] = ivec[i] - mean;
}
return 0;
}
double mean(vector<int> & data)
{
double dSum = 0;
for(int i = 0; i < ivec.size; i++)
{
dSum += data[i];
}
return dSum/ivec.size;
}
It says that my ivec is undeclared so how would I declare. I would appreciate the help
Upvotes: 0
Views: 326
Reputation: 227390
The parameter of your mean
function is called data
, so that is what you need to use:
double mean(const vector<int>& data)
{
double dSum = 0;
for(int i = 0; i < data.size(); i++)
{
dSum += data[i];
}
return dSum/data().size;
}
Also note that size
is a member function, so you need to call is as size()
. Also, the vector should be passed by const
reference, because it is not being modified.
Note: An idiomatic way to calculate the mean would be to use std::accumulate
:
if (!ivec.empty())
{
double mean = std::accumulate(ivec.begin(), ivec.end(), 0.0)/ivec.size();
}
Upvotes: 6
Reputation: 310950
You have several errors in function mean. First of all std::vector has no data member with name size . It has a member function with name size. So instead of for example expression
ivec.size
there must be
ivec.size()
And the second error is that instead of nane ivec you have to use name data because name ivec is not declared in this function.
So the function should look as
double mean( const vector<int> &data )
{
double dSum = 0.0;
for ( std::vector<int>::size_type i = 0; i < data.size(); i++ )
{
dSum += data[i];
}
return ( data.size() == 0 ? 0.0 : dSum / data.size() );
}
I changed the type of the parameter to const vector<int> &
because the vector itself is not changed in the function.
It would be even better to substitute the ordinary for statement for the range-based for statement. in this case the function will look as
double mean( const vector<int> &data )
{
double dSum = 0.0;
for ( int x : data ) dSum += x;
return ( data.size() == 0 ? 0.0 : dSum / data.size() );
}
These two statements
double dSum = 0.0;
for ( int x : data ) dSum += x;
in fact correspond to the body of standard algorithm std::accumulate
So you could write (provided that you included header <numeric>
)
#include <numeric>
//...
double mean( const vector<int> &data )
{
double dSum = std::accumulate( data.begin(), data.end(), 0.0 );
return ( data.size() == 0 ? 0.0 : dSum / data.size() );
}
Also take into account that your program does not fully satisfies the assignment. In the assignment there is written
Query the user for a size.
Create a vector of the size specified by the user.
Also after this loop
for(int i = 0; i < ivec.size(); i ++)
{
ivec[i] = ivec[i] - mean;
}
I think you should output the vector that to show new values of its elements.
Upvotes: 0