Watch the Stock Market

Difficulty: Medium

Stock prices fluctuate throughout the day based on news, fear, and speculation. If you own stock and desire the price to rise, you anxiously watch the stock tickers throughout the day, puzzled or delighted by the reactions. This month’s Exercise attempts to emulate such anxiety.

Assume that the stock market opens at 9:00 AM. Your stock is priced at $8/share. Every 30 minutes the stock ticker is updated, reflecting the new price for your stock, until the market closes at 4:00 PM:

 9:00 Price $8/share
 9:30 Price $9/share
10:00 Price $7/share
10:30 Price $6/share
11:00 Price $8/share
11:30 Price $10/share
12:00 Price $12/share
12:30 Price $11/share
 1:00 Price $10/share
 1:30 Price $13/share
 2:00 Price $11/share
 2:30 Price $5/share
 3:00 Price $8/share
 3:30 Price $9/share
 4:00 Price $11/share

Obviously it was a good day for your stock: The price started at $8/share, and though it bounced around all day, it ended up at $11/share. Woo-hoo.

Your challenge for this month’s Exercise is to reproduce the above stock market ticker output. Yes, I know that the US stock markets open at 9:30 AM Eastern time, but this is a programming exercise and not a reflection of reality.

Use the following integer array in your code:

int stock_price[] = {
    8, 9, 7, 6, 8, 10, 12, 11,
    10, 13, 11, 5, 8, 9, 11
};

The stock_price[] array represents stock prices taken at 30 minute intervals. Your task is to output these values as shown above, along with the timestamps from 9:00 AM through 4:00 PM.

You may find this Exercise to be a little bit on the easy side of “medium” difficulty. That’s fine. For July’s Exercise, this code is taken one step further into the “hard” level of difficulty. Be forewarned!

Please try this Exercise on your own before you peek at my solution.

3 thoughts on “Watch the Stock Market

Leave a Reply