You are trying to make money in the stock market. On each given day, you can cho
ID: 652224 • Letter: Y
Question
You are trying to make money in the stock market. On each given day, you can choose to invest your money in the stock of any one of k companies. By taking CS 330, you have acquired (temporarily) the gift of clairvoyance. Thus, you know ahead of time than on day i the stock of company j will go up by a factor of fi,j . However, trading stocks costs money: any time you want to switch from one company to another, you have to pay commission, and the value of your investments will go down by a factor of c. You are initially invested in company 1. Thus, for example, if you stay invested in company 1 for days 1 and 2 and switch over to company 2 for days 3 and 4, your money will change by a factor of (f1,1f2,1/c)f3,2f4,2, whereas if you switch over to company 2 right away and stay invested in it until day 4, your money will change by a factor of (1/c)f1,2f2,2f3,2f4,2. Your goal is to make the most money possible in n days. You are given the values f1,1 . . . fn,k and c as input. Design a dynamic programming solution to this problem. Your output should be a sequence of values, one for each day, that tells you what stock to be invested in on that particular day. Analyze the running time of your solution. The straightforward solution is cubic; a quadratic solution is possible, but not required.
Explanation / Answer
CalculateMaxMoney
Start
Read values of f1,f2,f3,f4,c,k
declare variable newvalue as double
// For Plan 1
multiply k and f1 and assign it to newvalue
output newvalue
multiply newvalue and f2 and assign it to newvalue
output newvalue
divide newvalue by c and assign it to newvalue
multiply newvalue by f3 and assign it to newvalue
output newvalue
multiply newvalue and 2f4 and assign it to newvalue
output newvalue
multiply newvalue by 2 and assign it to newvalue
output newvalue
// for Plan 2
divide k by c and assign it to newvalue
multiply newvalue and f1 and assign it to newvalue
output newvalue
multiply newvalue and 2f2 and assign it to newvalue
output newvalue
multiply newvalue by 2f3 and assign it to newvalue
output newvalue
multiply newvalue and 2f4 and assign it to newvalue
output newvalue
multiply newvalue by 2 and assign it to newvalue
output newvalue
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.