theorangedog.net

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:

  1. // this is a test to create a program that calculates bond prices
  2.  
  3. #include <iostream>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7. double calc(double coupon, double numyears, double numpmts, double parval, double rdisc);
  8.  
  9. int main()
  10. {
  11.         double coupon, numyears, numpmts, parval, rdisc;
  12.         cout << “Enter the coupon payment: “;
  13.         cin >> coupon;
  14.         cout << “nEnter the number of years to maturity: “;
  15.         cin >> numyears;
  16.         cout << “nEnter the number of payments per year: “;
  17.         cin >> numpmts;
  18.         cout << “nEnter the par value: “;
  19.         cin >> parval;
  20.         cout << “nEnter your discount rate: “;
  21.         cin >> rdisc;
  22.         cout << “n”;
  23.  
  24.         calc(coupon, numyears, numpmts, parval, rdisc);
  25.  
  26.         cout << “nnOnly a sucker would pay more than that.nn”;
  27.         return 0;
  28. }
  29.  
  30. double calc( double coupon, double numyears, double numpmts, double parval, double rdisc)
  31. {
  32.         double rdiscbynumpmts, numperiods, value, pvfactor;
  33.  
  34.         rdiscbynumpmts = rdisc / numpmts;
  35.         numperiods = numyears * numpmts;
  36.         pvfactor = pow((1 + rdiscbynumpmts), numperiods);
  37.        
  38.         value = (coupon * ((1 - (1 / pvfactor))/rdiscbynumpmts)) + (parval * (1 / pvfactor));
  39.         cout << “The value of this bond is: “ << value;
  40.         return 0;
  41. }

And for the record, I realize that this may not be the most efficient code… its just an example.

:,
3 comments for this entry:
  1. Alex

    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.

  2. foq

    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.

  3. sumtaru

    No problem with the code because it was written in C++.

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Try Google.

Endurance

The best long distance runners eat raw meat, run naked, and sleep in the snow.