{"id":4853,"date":"2021-07-08T00:01:01","date_gmt":"2021-07-08T07:01:01","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4853"},"modified":"2021-07-09T07:49:30","modified_gmt":"2021-07-09T14:49:30","slug":"check-your-stock-gains-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4853","title":{"rendered":"Check Your Stock Gains &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4836\">this month&#8217;s Exercise<\/a> is to determine the greatest price gain for a stock during the trading day. The gain is calculated moving forward in time, from a low to a high. It&#8217;s easy to see with human eyeballs looking at a chart, but not so easy when you must code a solution.<br \/>\n<!--more--><br \/>\nA program with this level of complexity involves several steps. I puzzled over it for a long time. The approach I take is this:<\/p>\n<ol>\n<li>Moving forward through the prices, determine the maximum gain for each 30 min time period.<\/li>\n<li>Given the period with the greatest gain, obtain the starting time and value.<\/li>\n<li>Use the gain itself to determine which time period has the ending value.<\/li>\n<li>Output the timestamp for the low and high values, along with their values.<\/li>\n<\/ol>\n<p>Even expressed as a list, these steps seem clunky. So why not dive right in?<\/p>\n<p>In my solution, Step 1 is accomplished by a <em>getrange()<\/em> function. It plows through the stock price array and returns the maximum difference between the current element and the remaining elements. This function is similar to a common <em>max()<\/em> function, though it determines the maximum difference for each subsequent element of the array:<\/p>\n<pre class=\"screen\">\r\nint getrange(int t,int s[])\r\n{\r\n    int i,start,largest,spread;\r\n\r\n    largest = 0;\r\n    start = s[t];\r\n    for( i=t+1; i&lt;SIZE; i++ )\r\n    {\r\n        spread = s[i] - start;\r\n        if( spread &gt; largest )\r\n            largest = spread;\r\n    }\r\n\r\n    return(largest);\r\n}<\/pre>\n<p>In the <em>main()<\/em> function, the calculated maximum range values are saved in the <code>range[]<\/code> array:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* evaluate increases *\/<\/span>\r\nfor(t=0; t&lt;SIZE; t++)\r\n{\r\n    range[t] = getrange(t,stock_price);\r\n}<\/pre>\n<p>For this Exercise, array <code>range[]<\/code> contains these elements:<\/p>\n<p><code>5 4 6 7 5 3 1 2 3 0 0 6 3 2 0<\/code><\/p>\n<p>The greatest range returned from the <em>getrange()<\/em> function is 7 at element number 3 (the 4th element). Of course, you and I see this item with our eyeballs. The computer must evaluate the array to find the largest value. The element number for this value is stored in variable <code>low<\/code>, which is Step 2:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* determine the maximum spread *\/<\/span>\r\nmax_spread = low = high = 0;\r\n    <span class=\"comments\">\/* find the low value *\/<\/span>\r\nfor( t=0; t&lt;SIZE; t++ )\r\n{\r\n    if( range[t] &gt; max_spread )\r\n    {\r\n        max_spread = range[t];\r\n        low = t;\r\n    }\r\n}<\/pre>\n<p>Variable <code>max_spread<\/code> is put to work finding the ending value&#8217;s array element, which is Step 3. This value (the element number) is stored in variable <code>high<\/code>:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* find the high value *\/<\/span>\r\nfor( t=low+1; t&lt;SIZE; t++ )\r\n{\r\n    if( stock_price[t]==stock_price[low]+max_spread )\r\n    {\r\n        high = t;\r\n        break;\r\n    }\r\n}<\/pre>\n<p>Each array element is compared with the high value, calculated as the <code>stock_price[low]<\/code> value plus <code>max_spread<\/code>. When this element is found, its element number (not the value) is saved in variable <code>high<\/code>. (You need not save the value, because the element number links to the value, as in <code>stock_price[t]<\/code>.)<\/p>\n<p>Variables <code>high<\/code> and <code>low<\/code> now contain the element numbers representing the greatest price increase throughout the day as stored in the <code>stock_price[]<\/code> array. The code outputs these values, which looks like this:<\/p>\n<p><code>The best time to buy a share is at 10:30, $6\/share<br \/>\nThe best time to sell is at 1:30, $13\/share<\/code><\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_07-Exercise.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view my full solution.<\/p>\n<p>One major difference between the solution to this month&#8217;s Exercise and last month&#8217;s code, is that I created a separate <em>timestamp()<\/em> function. This function takes the array element number and returns an hour-minute timestamp string. It was necessary to create this function to cleanup the code.<\/p>\n<p>I hope your solution met with success. I hope there&#8217;s a more elegant way to solve the puzzle. I feel mine is rather clunky, but it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to determine the greatest price gain for a stock during the trading day. The gain is calculated moving forward in time, from a low to a high. It&#8217;s easy to see with human &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4853\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/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-4853","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\/4853","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=4853"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4853\/revisions"}],"predecessor-version":[{"id":4872,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4853\/revisions\/4872"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}