The price of a stock fluctuates as people buy and sell it, as a response to supply and demand. In general, if more people are buying a stock than selling it, the price will increase, and if more people are selling than buying, the price will decrease. One way that people try to make money in the stock market is by purchasing a stock at one price and waiting for the stock to increase in price, then selling it. For instance, if a stock is priced at $10, and I buy one share of it, then some time later the price goes up to $12 and I sell my single share, my profit is $2. Obviously profits are magnified the more shares I buy at a time --- if I had bought 10 shares (paying $100 total) and then sold them (making $120 total), then my profit would have been $20. (We ignore transaction costs for this project.)
When the stock market opens for the day, the initial price that someone can buy or sell a given stock at is called the stock's opening price. The last price of the day, right before the market closes, is called the stock's closing price. The price fluctuates throughout the day; this project is only concerned with opening and closing prices.
I highly suggest you write and test the pieces in this order:
The program should compile and run right out of the box, even before you change anything. You should write each piece, test it, then move on to the next piece.
Test this code by using the print_prices function. There is a section of code in main that you can uncomment to see if what you wrote is working.
Notice that this function has two pass-by-reference parameters, called swing and day, that you will need to set: the calling function expects it. Because C++ can only return a single value from a function, we use these pass-by-reference parameters to send a pair of values back to main. These parameters should be set to the largest difference (in absolute value), and the day on which that swing in price occurred.
Notice that this function has two pass-by-reference parameters, called swing and day, that you will need to set. These parameters should be set to the largest difference (in absolute value), and the day on which that swing in price occurred (day here means the day of the closing price, not the day of the opening price).
Also note that there is a to_money function I've given you that is useful for printing prices; it converts a double into a string with a leading dollar sign and always two decimal places.
Enter number of days to track prices for: 2 Enter the 2 opening prices, all at once: 10 11.50 Enter the 2 closing prices, all at once: 10.75 9.25 The maximum closing price of the stock during these days was: $10.75. The largest daily swing was $2.25 on day 1. The largest overnight swing was $0.75 from day 0 to day 1. Day 0: you start with $100.00. Day 0: you buy 10 shares at $10.00 per share. Day 0: you sell your shares at $10.75 per share. Day 0: you end with $107.50. Day 1: you start with $107.50. Day 1: you buy 9.34783 shares at $11.50 per share. Day 1: you sell your shares at $9.25 per share. Day 1: you end with $86.47.Run 2:
Enter number of days to track prices for: 5 Enter the 5 opening prices, all at once: 20 21 21.6 22.8 19.9 Enter the 5 closing prices, all at once: 19 21.1 22.1 19.8 20.1 The maximum closing price of the stock during these days was: $22.10. The largest daily swing was $3.00 on day 3. The largest overnight swing was $2.00 from day 0 to day 1. Day 0: you start with $100.00. Day 0: you buy 5 shares at $20.00 per share. Day 0: you sell your shares at $19.00 per share. Day 0: you end with $95.00. Day 1: you start with $95.00. Day 1: you buy 4.52381 shares at $21.00 per share. Day 1: you sell your shares at $21.10 per share. Day 1: you end with $95.45. Day 2: you start with $95.45. Day 2: you buy 4.41909 shares at $21.60 per share. Day 2: you sell your shares at $22.10 per share. Day 2: you end with $97.66. Day 3: you start with $97.66. Day 3: you buy 4.28342 shares at $22.80 per share. Day 3: you sell your shares at $19.80 per share. Day 3: you end with $84.81. Day 4: you start with $84.81. Day 4: you buy 4.26189 shares at $19.90 per share. Day 4: you sell your shares at $20.10 per share. Day 4: you end with $85.66.
As an example, consider 4 days of prices:
Day: 0 1 2 3 Open: 11 10 12 9 Close: 16 14 15 10The best choice here is to buy on the opening of day 1 at $10/share, then sell at the close of day 2 at $15/share. Notice that neither of these prices correspond to the minimum opening price (which happens on day 3) nor the maximum closing price (which happens on day 0).