Code Snippet Test
by theorangedog on Nov.04, 2007, under Skills
I installed the Code Snippet (codesnippet) plugin, so that any copied and pasted code looks nice.
A sample of a simple bond value calculator that I wrote a few months back is below:
-
// this is a test to create a program that calculates bond prices
-
-
#include <iostream>
-
#include <cmath>
-
-
using namespace std;
-
double calc(double coupon, double numyears, double numpmts, double parval, double rdisc);
-
-
int main()
-
{
-
double coupon, numyears, numpmts, parval, rdisc;
-
cout << “Enter the coupon payment: “;
-
cin >> coupon;
-
cout << “nEnter the number of years to maturity: “;
-
cin >> numyears;
-
cout << “nEnter the number of payments per year: “;
-
cin >> numpmts;
-
cout << “nEnter the par value: “;
-
cin >> parval;
-
cout << “nEnter your discount rate: “;
-
cin >> rdisc;
-
cout << “n”;
-
-
calc(coupon, numyears, numpmts, parval, rdisc);
-
-
cout << “nnOnly a sucker would pay more than that.nn”;
-
return 0;
-
}
-
-
double calc( double coupon, double numyears, double numpmts, double parval, double rdisc)
-
{
-
double rdiscbynumpmts, numperiods, value, pvfactor;
-
-
rdiscbynumpmts = rdisc / numpmts;
-
numperiods = numyears * numpmts;
-
pvfactor = pow((1 + rdiscbynumpmts), numperiods);
-
-
value = (coupon * ((1 - (1 / pvfactor))/rdiscbynumpmts)) + (parval * (1 / pvfactor));
-
cout << “The value of this bond is: “ << value;
-
return 0;
-
}
And for the record, I realize that this may not be the most efficient code… its just an example.
3 comments for this entry:




November 7th, 2007 on 1:26 pm
One downside of most syntax highlighters that do line numbering is that if you try to copy the code, you get the line numbers too.
I think that was the primary reason I never used one on my site.
November 7th, 2007 on 7:25 pm
Thanks for the tip, Alex. This one allows me to select whether or not the line numbers appear, and since I see the point in terms of copying, I’ll leave the numbers off in the future.
January 31st, 2008 on 10:24 pm
No problem with the code because it was written in C++.