finance function

hi,
I am trying to compile this program to compute the Present Value using the following code, and I think my main() function is flawed. Any help would be really appreciated.

#include <cmath>
#include <vector>
#include <iostream>
using std::cout;
using std::endl;

double cfd(const vector<double>& cft, const vector<double>& cfa, const double& r)
{
double pv=0;
for (int i=0;i<cft.size();i++)
{
pv+=cfa[i]/pow((1+i),cft[i]);
}
return pv;
};

int main()
{
double a1[]={1,2,3};
double a2[]={5,5,5};
double r=0.10;
cout<<cfd(&a1,&a2,&r)<<endl;
return 0;
}

sorry Disch
here goes..
when I compile it (I run the following $:g++ bacpp.cpp -o test), I get the following errors:
bacpp.cpp:7: error: expected ',' or '...' before '(' token
bacpp.cpp:7: error: ISO C++ forbids declaration of 'vector' with no type
bacpp.cpp: In function 'int cfd(int)':
bacpp.cpp:10: error: 'cft' was not declared in this scope
bacpp.cpp:12: error: 'cfa' was not declared in this scope
bacpp.cpp:10: In function 'int main()':
bacpp.cpp:10: error: invalid conversion from 'int(*)[3]' to 'int'
bacpp.cpp:10: error: too many arguments to function 'int cfd(int)'
bacpp.cpp:10: error: at this point in file
problem #1:

Vectors are in the std namespace. You either need to do using std::vector; with your other using lines, or you need to change line 7 to have std:: before all the vectors.

problem #2:

main is passing arrays to the function, not vectors. If you want to pass vectors to the function, then main needs to make vectors instead of arrays.

problem #3:

When passing things to a function by reference (which is what you're doing), you do not use the & operator. That operator gives you a pointer.

This line:

cout<<cfd(&a1,&a2,&r)<<endl; <- get rid of all those '&' symbols
EDIT: Functions don't need to end with a semicolon. I had more here, but Disch beat me.

Happy coding!

-Albatross
Last edited on
thanks Disch,

I fixed problem #1, and problem #3.

for problem #2, I created a typedef ivec, so, here's the code again:

#include <cmath>
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
using std::endl;

typedef std::vector<int> ivec;

int cfd(const vector<int>& cft, const vector<int>& cfa, const double& r)
{
double pv=0;
for (int i=0;i<cft.size();i++)
{
pv+=cfa[i]/pow((1+i),cft[i]);
}
return pv;
};

int main()
{
ivec cft={1,2,3};
ivec cfa={5,5,5};
double r=0.10;
cout<<cfd(cft,cfa,r)<<endl;
return 0;
}

but now when I compile it, it says:

bacpp.cpp: In function 'int main()':
bacpp.cpp:21: error: braces around initializer for non-aggregate type 'ivec'
bacpp.cpp:21: error: braces around initializer for non-aggregate type 'ivec'
If I read the docs carefully enough, that'll be fixed in C++0x.

You can't assign lists like those to vectors; the operator = isn't defined for that operation. Sorry. :/

You can use the assign() function, though!
http://cplusplus.com/reference/stl/vector/assign/

-Albatross
thanks Albatross,
I removed the ; at the end of the function.

I guess I am still making an error somewhere with the vectors
oh ok....
thanks for the link Albatross
I'll review it and then try to rewrite this again....
PLEASE encase your code with "[ code ][ /code ]" (without the spaces) it makes it so much easier to read it.
Last edited on
thanks guys,
it works now...

here's the code I used to calculate the PV of future cashflows

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

#include <cmath>
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
using std::endl;

double cfd(const vector<int>& coupon, const vector<int>& itime, const double& r)
{
double pv=0;
for (int i=0;i<itime.size();i++)
{
pv+=coupon[i]/pow((1+r),itime[i]);
}
return pv;
}

int main()
{
  vector <int> coupon;
  coupon.assign(3,5);
  
  int mytime[]={1,2,3};
  vector <int> itime;
  itime.assign(mytime,mytime+3);
  
  double r=0.10;
    
 cout<<cfd(coupon,itime,r)<<endl;
 
return 0;
}
 
Topic archived. No new replies allowed.