# a simple vector as an argument of a function

• 01-14-2005
strickey
a simple vector as an argument of a function
Dear All,
I wrote two simple functions one of which works (sum) and the other (deduct) gives same values for all vector elements. Where did I go wrong???
Thank you

Code:

``` vector <double> deduct (const vector<double>& v, double k)       {           vector<double> d(v.size());                         for(int i=0; i < v.size();++i)                             d[i]=v[i]-k;                                         return d;       } double sum (const vector<double>& v)   {       double c=0.000000000;               for(int i=0; i < v.size();++i)                       c +=v[i];                       return c;     } #include <vector> main() {     vector <double> vect(8);     double fidy []={112.999,112.994,112.994,-0.005,-0.007,-13.004, -113.003,-113.005};     vector<double> yfid(fidy,fidy+imarknum);     double k=sum (vect);     v=deduct (vect, k); }```
• 01-14-2005
strickey
Mistake
Sorry, I made a mistake. Last 2 lines should be:
Code:

```double k=sum (yfid);  v=deduct (yfid, k);```
• 01-14-2005
quzah
Don't you think it would be a good idea to #include<vector> before you start using pieces of said vector? Also, what exactly is v, since you haven't actually declared it any place.

Quzah.
• 01-14-2005
strickey
It's OK
I am sorry, I wrote this thread in haste, functions are coming after
#include <vector>
• 01-14-2005
strickey
v
v is an argument of function
• 01-14-2005
hk_mp5kpdw
Quote:

Originally Posted by quzah
Also, what exactly is v, since you haven't actually declared it any place.

Quote:

Originally Posted by strickey
v is an argument of function

He means this one in main:

Code:

`v=deduct (vect, k);`
It isn't declared anywhere before its use.

You also need to consider that vector containers are a part of the std namespace. So either:

1. Put std:: before any vector declaration, i.e.:
Code:

`std::vector<double> vect(8);`
2. Put this after your includes:
Code:

`using std::vector;`
3. Lastly you could do this after your includes:
Code:

`using namespace std;`
You should prefer the first option over the others if possible.

And while I'm at it, you should put an explicit int in front of main and you also don't seem to have defined the imarknum variable anywhere.[/edit]
• 01-14-2005
strickey
Code:

```using namespace std; . . . main() . . imarknum=8; vector <double> v(8);```
but result is still the same.
• 01-14-2005
pianorain
I'm not sure what your problem is. After outputting both the sum and the deduct return values, it looks right to me:
Code:

```Output: value of double k: 99.963 value of vector<double> v: 13.036 13.031 13.031 -99.968 -99.97 -112.967 -212.966 -212.968```
• 01-14-2005
hk_mp5kpdw
Let's get this out at the start, you allude to output in your first post but your example does not show any attempts at output, you haven't even included the <iostream> header. If you post code, lets see the whole thing since this appears to be a relatively small program and we shouldn't have to guess at what important/relevant things you might be leaving out..

Code:

```imarknum=8;  // int ?? double fidy []={112.999,112.994,112.994,-0.005,-0.007,-13.004, -113.003,-113.005}; vector<double> yfid(fidy,fidy+imarknum);```
I would prefer to use sizeof to get the number of elements in the array (get rid of imarknum):

Code:

```double fidy []={112.999,112.994,112.994,-0.005,-0.007,-13.004, -113.003,-113.005}; vector<double> vfid(fidy,fidy+(sizeof(fidy)/sizeof(fidy[0])));```
This way if you were to ever change the number of elements in the fidy array, you wouldn't need to change imarknum.

Also, just so you are aware/exposed to these things, there are template functions available that can help you do much of what you are trying to accomplish with the sum and deduct functions, i.e. the following:
Code:

```double sum (const vector<double>& v) {     double c=0.000000000;           for(int i=0; i < v.size();++i)         c +=v[ i ];             return c; } ... double k=sum (vect);```
Can be replaced with a simple call to the accumulate function:
Code:

```#include <numeric> ... double k = std::accumulate(vect.begin(),vect.end(),0.0);```
The deduct function can likewise be replaced with some code to copy the passed in vector into a new vector (using the vector's copy constructor) followed by a call to the transform function on the newly copied vector and using the minus function object along with the bind2nd function adapter. Although admittedly in this case, you aren't going to be saving much in the way of typing.