Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part II. Project 2 - Finding the Maximum Profit (20 points, submit your solution file online before the due date). Upload your file named LabProj2.cpp.

image text in transcribedimage text in transcribed

Part II. Project 2 - Finding the Maximum Profit (20 points, submit your solution file online before the due date). Upload your file named LabProj2.cpp. The purpose of this programming assignment is to practice using an array. This problem is selected and modified from some code interview questions from tech companies. Background Suppose you are an analyst in the stock market. Now you have a special task. You are given a 10-day history of the price of a stock. Given this price history, you want to find out the best possible investment strategy that could maximize the profit in these 10 days from trading this stock. The following is the basic setting of the problem. - You may assume that you have a starting fund of $100 on day 1. - For simplicity, we assume that the price of a stock is not changed within the same day. - You are allowed to buy/sell a non-integer number of share(s): say if you have $90 and the price of the stock is $60, you are allowed to buy 1.5 shares. - You only have at most ONE chance to buy and ONE chance to sell (however, you can buy/sell as many shares as you like in one transaction, as long as you have enough funds). - After you buy, you have to wait AT LEAST TWO DAYS before you sell your shares. For example, if you buy your shares on day 1 , then you cannot sell them on day 2. You can only sell it on day 3 or afterward. Example For example, assume that the price history is the following: It is not hard to see that, the best strategy will be buying 10 shares on day 8 (using $100 ), then selling them all on day 10 (getting $350 ), which nets a profit of $250. Your Task: Write a program that allows the user to input the 10-day price of the stock, then the program should show on the screen what is the maximum profit and which day one should buy and sell to achieve this. When your program gets started, the program should prompt the user to input the price history Please type the 10-day price history of the stock: Then the user would type the price history as 10 numbers separated using space. For example, the user types the following entries: 502001003020254010535 Then your program should print to the screen the max profit and the strategy: Max Profit: 250. Best Strategy: buy on day 8 , sell on day 10. Note that in some cases it is not possible to profit. For example, if the prices are: 501005040404040404040 In such cases, your program should print: MaxProfit:0. The grading of this programming assignment will reflect three factors, weighted as shown. 1. Correctness (10pts) - does the program run correctly. 2. Style (5 pts) - Do you paste your sample running output at the end as the comments? Does the code follow the Documentation and Submission Guidelines? Is the code well structured, readable, and indented properly? Do you use meaningful literals? 3. Design (5 pts) -- do you use the most efficient algorithm

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions