{"id":4797,"date":"2021-06-08T00:01:37","date_gmt":"2021-06-08T07:01:37","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4797"},"modified":"2021-06-05T09:07:03","modified_gmt":"2021-06-05T16:07:03","slug":"watch-the-stock-market-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4797","title":{"rendered":"Watch the Stock Market &#8211; Solution"},"content":{"rendered":"<p>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.<br \/>\n<!--more--><br \/>\nYour solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4791\">this month&#8217;s Exercise<\/a> 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:<\/p>\n<p><code>&nbsp;9:00 Price $8\/share<br \/>\n&nbsp;9:30 Price $9\/share<br \/>\n10:00 Price $7\/share<br \/>\n10:30 Price $6\/share<br \/>\n11:00 Price $8\/share<br \/>\n11:30 Price $10\/share<br \/>\n12:00 Price $12\/share<br \/>\n12:30 Price $11\/share<br \/>\n&nbsp;1:00 Price $10\/share<br \/>\n&nbsp;1:30 Price $13\/share<br \/>\n&nbsp;2:00 Price $11\/share<br \/>\n&nbsp;2:30 Price $5\/share<br \/>\n&nbsp;3:00 Price $8\/share<br \/>\n&nbsp;3:30 Price $9\/share<br \/>\n&nbsp;4:00 Price $11\/share<\/code><\/p>\n<p>Obviously, a loop is involved, one that both churns through the <code>stock_price[]<\/code> array and generates the timestamp values.<\/p>\n<p>For my solution, I chose not to use actual <em>time_t<\/em> values, which would have been cool, but a bit much. Instead, I opted to create separate <em>int<\/em> 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.<\/p>\n<p>My first attempt at a solution used the hour as the looping variable, increasing the value by 2 each iteration. Then I output two <em>printf()<\/em> strings to represent the stock price at the top and bottom of the hour. This effort proved too complex, though I&#8217;m sure I could get it to work eventually. If this approach was your solution, more power to you.<\/p>\n<p>Instead, I devised the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_06-Exercise.c\" rel=\"noopener\" target=\"_blank\">2021_06-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int stock_price[] = {\r\n        8, 9, 7, 6, 8, 10, 12, 11,\r\n        10, 13, 11, 5, 8, 9, 11\r\n    };\r\n    int t,hour,minute;\r\n\r\n    for(t=0; t&lt;15; t++)\r\n    {\r\n        hour = t\/2 + 9;\r\n        if( hour &gt; 12 )\r\n        {\r\n            hour -= 12;\r\n        }\r\n        minute = t%2 ? 30 : 0;\r\n        printf(\"%2d:%02d Price $%d\/share\\n\",\r\n                hour,\r\n                minute,\r\n                stock_price[t]\r\n              );\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>t<\/code> runs the <em>for<\/em> loop starting at Line 11, fifteen iterations for each element in the <code>stock_price[]<\/code> array.<\/p>\n<p>At Line 13, variable <code>hour<\/code> is set equal to the value of <code>t<\/code> divided by two plus 9 for 9:00 AM:<\/p>\n<p><code>hour = t\/2 + 9;<\/code><\/p>\n<p>As <code>t<\/code> is an <em>int<\/em> variable, any calculated half values are rounded, which helps with the output. For example, when <code>t<\/code> is equal to 3, 3\/2+9 is the same as 1+9, or 10.<\/p>\n<p>At Line 14, an <em>if<\/em> test converts hours beyond noon to their 12-hour clock equivalents.<\/p>\n<p>A ternary operator calculates the value of variable <code>minute<\/code> at Line 18:<\/p>\n<p><code>minute = t%2 ? 30 : 0;<\/code><\/p>\n<p>When variable <code>t<\/code> is odd (<code>t%2 == 1<\/code>), the value 30 is generated, otherwise <code>minute<\/code> is set to zero. This value is used in the <em>printf()<\/em> statement at Line 19 to output the minutes. The <em>printf()<\/em> formatting string uses the <code>%02d<\/code> placeholder to ensure that two digits are output when <code>minute<\/code> is equal to zero.<\/p>\n<p>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&#8217;s Exercise (July 2021), which greatly increases the challenge&#8217;s difficulty level.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-4797","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4797"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4797\/revisions"}],"predecessor-version":[{"id":4809,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4797\/revisions\/4809"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}