Watch the Stock Market – Solution

Looks like your stock had a great day at the market. It bounced around, highs and lows, lots of nerves wracked and fingernails chewed, but the price ended higher on the day.

Your solution for this month’s Exercise relies upon a provided integer array of stock price values. Your task is primarily to output the timestamps throughout the day, so that the output looks this:

 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, a loop is involved, one that both churns through the stock_price[] array and generates the timestamp values.

For my solution, I chose not to use actual time_t values, which would have been cool, but a bit much. Instead, I opted to create separate int variables for the hour and minute. Scary math is necessary in the code to ensure that these integer values click by properly as the loop progresses.

My first attempt at a solution used the hour as the looping variable, increasing the value by 2 each iteration. Then I output two printf() strings to represent the stock price at the top and bottom of the hour. This effort proved too complex, though I’m sure I could get it to work eventually. If this approach was your solution, more power to you.

Instead, I devised the following code:

2021_06-Exercise.c

#include <stdio.h>

int main()
{
    int stock_price[] = {
        8, 9, 7, 6, 8, 10, 12, 11,
        10, 13, 11, 5, 8, 9, 11
    };
    int t,hour,minute;

    for(t=0; t<15; t++)
    {
        hour = t/2 + 9;
        if( hour > 12 )
        {
            hour -= 12;
        }
        minute = t%2 ? 30 : 0;
        printf("%2d:%02d Price $%d/share\n",
                hour,
                minute,
                stock_price[t]
              );
    }

    return(0);
}

Variable t runs the for loop starting at Line 11, fifteen iterations for each element in the stock_price[] array.

At Line 13, variable hour is set equal to the value of t divided by two plus 9 for 9:00 AM:

hour = t/2 + 9;

As t is an int variable, any calculated half values are rounded, which helps with the output. For example, when t is equal to 3, 3/2+9 is the same as 1+9, or 10.

At Line 14, an if test converts hours beyond noon to their 12-hour clock equivalents.

A ternary operator calculates the value of variable minute at Line 18:

minute = t%2 ? 30 : 0;

When variable t is odd (t%2 == 1), the value 30 is generated, otherwise minute is set to zero. This value is used in the printf() statement at Line 19 to output the minutes. The printf() formatting string uses the %02d placeholder to ensure that two digits are output when minute is equal to zero.

This Exercise is more about the puzzle of outputting the proper time values than really tracking the stock price. That job is left for next month’s Exercise (July 2021), which greatly increases the challenge’s difficulty level.

Leave a Reply