Relearning C++
- George Fane
- Aug 12, 2019
- 3 min read
Updated: Aug 19, 2019
Every program tells a story.
Euler's Number/Compound Interest
Note: this is a newer version that allows the user to enter the principal and interest rate of their choice.
A few weeks ago I started watching Numberphile, a YouTube channel that explains various mathematical concepts for the average viewer. I'm not quite sure why I started; I tried to find the first video of theirs that I watched but I can't remember which it is. Nevertheless, I soon began to binge them, enjoying the clear and concise explanations of wonky maths that I never knew existed (my work on this website seems to have made me more interested in math outside of school. I guess I have fun doing this now).
In some video, the origin of Euler's number was explained: if a dollar is placed in a bank account with 100% annual interest, the account amount will approach e as the number of annual compounding periods approaches infinity. It stuck with me: I Googled for an online C++ compiler, clicked the first link I saw, and began typing.
I had not done any coding since my C++ class in the first semester of my sophomore year, which ended a year and a half ago in January 2018. However, I remembered just enough to get this program done: defining variables, cout, cin, the coumpound interest formula, and a for loop that allowed me to test higher and higher values for the compounding period entry. Sure enough, when I held my finger on the keyboard's "9" button and pressed "Enter", the program (linked below) would output 2.71828.
And then I didn't do anything more. I enjoyed the task of writing a few lines of code and seeing my creation work properly, but I stopped and went back to tennis, reading, and streaming in my free time.
Fibonacci Numbers
Note: I don't have the original; I made the linked program just now to show my method.
But then I saw a video put out by Andrew Dotson (a physics PhD student who makes vlogs and skits) in which he outputted the Fibonacci numbers with LolCode while drinking. I didn't observe for myself if he actually succeeded, because I stopped the video, pulled up a compiler, and did it myself (in normal C++ syntax, however). Writing the program seemed harder at first than it actually was; I thought I would have to automate the creation of variables for each new term, defining them as the sum of the two previous variables. Thinking through the problem some more, I realized I could simply define two variables then make the first equal to the sum of the first and second and output the first variable, the second equal to the sum of the first and second and output the second variable, and so on.
Student Debt https://onlinegdb.com/SyGZhhYXr
With that, I really started to enjoy that sort of computational programming on my own time. I soon wrote a program that shows the user how many months it will take to pay off their student debt given an initial debt, interest rate, and monthly payment
I made something with a similar function for my DECA Financial Consulting Event presentation, but that was in Google Sheets. I could not perform the rapid recursions that C++ does so well and had to stick random constants into my formula to make it line up with monthly payment calculators on the internet (I had to make student debt compound only 54 times per year when it actually compounds every day).
If only I had this program then. I'm guessing a similar program calculating the growth of retirement savings would have been more useful than the Google Sheets formula I designed and used in reality, which assumes that the retirement account's choice of investment (say, an S&P 500 index fund) only compounded 2.8 times every 5 years, which seems ridiculously silly to me now.
These are definitely on the easy side, though they provided enough of a challenge to hook me back onto programming. Every task I give myself is a nice little problem, and solving problems is the duty and pleasure of intelligent sapience.
Thank you for reading,
George Fane
Comments