A friend of mine has a tree farm up in Wisconsin. He's had the farm for about 30 years, and he tells me stories about how his kids could be millionaires if just a few good walnut trees grow to maturity. Of course, it could be that he'd be better off investing his money in a mutual fund rather than spending it on trees and chainsaws. In fact, he dedicated a chapter of his forestry book to an economic analysis of growing and selling trees.
In this assignment, I'd like you to perform some of the same calculations he used to determine whether or not it would be lucrative to grow trees. Our goal is to compare the effective rate of return from our tree investment to an actual return rate which we could have gotten by putting the money directly into a bank account.
Your program should first ask the user to input an interest rate, and then ask how many years the investment should span.
Then, you should accept a series of positive and negative values from the user, which represent earnings and expenditures for each year of the investment. Use an array to store these values.
If your input data is as follows:
.05 5 -10000 -500 0 0 50000we would interpret it to mean that in the first year, he spent $10,000, in the second year, $500, nothing in the 3rd or 4th year, and in the 5th year he earned $50,000. The first two values are the bank interest rate and the number of years.
Prepare a data file containing the appropriate integers to represent the following transactions:
purchase land for $10000, purchase tractor for $1000, purchase refreshments for friends for $50 (all invited to look at the land), purchase accident insurance for $1000 (guests and tractors don't mix), purchase nothing (spouse was mad about the accident), purchase nothing, purchase nothing, purchase walnuts from local Amish farmers for $100, purhcase nothing (planted the seeds, then thought better of the idea), purchase seedlings from nursery for $8000, purchase fertilizer every year @ $50 for total of 5 years, purchase chain saw for $500, sell 60 puny trees for $50 each, do nothing for 7 years, sell 50 small size trees for $200 each, do nothing for 25 years, spend $20000 on kitchen renovations, sell 65 medium size trees for $1000 each, do nothing for 25 years, sell 80 full-grown trees for $10,000 each
Don't forget to put the number of years and the interest rate at the beginning of the file.
If your program reads from cin, you can run the program like this to read from the data file one line at a time:
$ trees < datafile
If I put all that money in the bank instead of into the farm, how much would be in the account at the end? Let's assume the bank gives me 5% interest compounded continuously. If I only made one deposit, I could calculate the balance at the end of term using this formula for continuous compouded interest:
A = Pe^(rt)
A is the final amount, P is the principal, or starting amount, e is the base of the natural logarithm, r is the interest rate, expressed as a decimal, like .05, t is the term in years, and ^ means exponentiation .
This is called a future value calculation, because we're calculating how much we would have at some point in the future if we used a certain investment vehicle.
We don't want to do that calculation by hand for each transaction, so we should use some sort of loop and accumulate the value obtained each time in a variable.
We want to calculate the sum over each year of
P_i*e^(rate*t_i)where P_i is the amount otherwise spent in the ith year, and t_i is the amount of time left between the ith year and the final year.
We should ignore any positive values in the array, as those represent money earned as a result of our expenditures. If the money was in the bank, we wouldn't have such earnings.
When we have the future value of our expenditures, we should compare that to how much we really have after spending it all on trees instead. In this part, we *do* include the earnings. Then we compare the rate of return on the two kinds of investments.
The rate of return on the bank investment is simply the interest rate that you assume. To determine the effective return rate of your tree investment (and here's where I could well be wrong), start with the final amount you'd have in the bank, and project backwards to find out how much you would have had to invest in the bank to get that much money in the end. This is the "present value" of the bank investment. Now we pretend we had that much money at the beginning, and compare it to the amount that we end up with the tree investment.
For example, if our transactions looked like this:
[ -1000, -500, 0, 0, 10000 ]We'd have $8500 if we grew trees. We'd have $1894.73 if we had put $1000 in the bank the first year, and $500 the next. Clearly trees wins this round, but what rate of return have we actually achieved? We should figure out the present value that corresponds to $1894.73, and then find out what growth rate would get us from that present value to a future value of $8500 in 5 years.
When this is my datafile:
$ cat datafile .05 6 -10000 -1000 -50 -1000 0 30000
My program looks like this when I run it:
$ ./a.out < datafile What bank interest rate would you like to assume? How many years would you like to enter? If these amounts were each invested at a flat %5, at the end of 6 years, you'd have $16005.52 Since you just spent it all on trees and chainsaws, you have 17950.00 instead. The present value of your investment is 11857.18 The effective return on your investment was %6.91 Well done! You beat the bank!
Write a function that takes an array and returns the sum of the future value of all the expenditures represented by the values in the array.
Use functions or variable assignments to break up complicated calculations into smaller expressions.
Use an #include directive to have access to e and exponentiation.
For more information on calculating the time-value of money, check out this page on analysis of future and present values. For more information on calculating continuous interest, check out this page or look in a intro level finance book.
If you have any questions about this assignment, please email cs106@cs.uchicago.edu!
Write separate program, with it's own main function, which shows the differences between call-by-value and call-by-reference. It should be clear to someone running your program what functionality is being exhibited. Also, create some example that showcase what's really happening when you pass in entire arrays as arguments. Discuss the uses of the various calling mechanism.