{"id":5388,"date":"2022-06-08T00:01:09","date_gmt":"2022-06-08T07:01:09","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5388"},"modified":"2022-06-08T07:42:48","modified_gmt":"2022-06-08T14:42:48","slug":"the-elevator-ride-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5388","title":{"rendered":"The Elevator Ride &#8211; Solution"},"content":{"rendered":"<p>To code a simulated elevator ride you must know the floor requests, up and down. This is the challenge given for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5369\">this month&#8217;s Exercise<\/a>, which can either drive you nuts or delight you depending on how you craft your solution.<br \/>\n<!--more--><br \/>\nFor my solution, I use an array <code>lift_stop[]<\/code>:<\/p>\n<p><code>int lift_stop[floors];<\/code><\/p>\n<p>The <code>floors<\/code> constant sets the number of elements to 15. It didn&#8217;t fuss over creating a &#8220;lobby&#8221; floor as the challenge assumes the lobby is both the starting and ending point of the elevator&#8217;s journey.<\/p>\n<p>The array is initialized to all zero values:<\/p>\n<p><code>for( x=0; x&lt;floors; x++ )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;lift_stop[x] = 0;<\/code><\/p>\n<p>To set the elevator stops, I use values 1 and -1: Going up a stop is set to 1; going down a stop is set to -1. This way I can use only the single array to monitor both directions. And if a request is made on a floor both as a destination (going up) and retrieval (going down), I set the element value to 2. The loop that assigns the stops uses constant <code>passengers<\/code> to represent the number of riders (8):<\/p>\n<p><code>p = 0;<br \/>\nwhile( p&lt;passengers )<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;flr = rand() % floors;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if( lift_stop[flr]==0 )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lift_stop[flr] = rand()%2 ? 1 : -1;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;else<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lift_stop[flr] = 2;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;p++;<br \/>\n}<\/code><\/p>\n<p>Variable <code>flr<\/code> holds the random floor, where a stop is made.<\/p>\n<p>If the element represented by <code>flr<\/code> (<code>lift_stop[flr]<\/code>) is zero, a random value marks the floor as a stop going up (1) or down (-1). If the floor is already chosen as a stop (non-zero), the value 2 is set, meaning it&#8217;s both a stop on the way up and down. Of course, 2 might also represent a second request for the same direction, which is fine for this simplistic type of simulation.<\/p>\n<p>Once the array is built, two <em>for<\/em> loops scan the array. The first <em>for<\/em> loop outputs stops going up. The second loop outputs stops going down.<\/p>\n<p>Here is the full code for my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_06-Exercise.c\" rel=\"noopener\" target=\"_blank\">2022_06-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    const int floors=15,passengers=8;\r\n    int lift_stop[floors];\r\n    int x,p,flr;\r\n\r\n    <span class=\"comments\">\/* seed randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* initialize elevator stops *\/<\/span>\r\n    for( x=0; x&lt;floors; x++ )\r\n        lift_stop[x] = 0;\r\n\r\n    <span class=\"comments\">\/* passengers set their levels *\/<\/span>\r\n    p = 0;\r\n    while( p&lt;passengers )\r\n    {\r\n        flr = rand() % floors;\r\n        if( lift_stop[flr]==0 )\r\n            lift_stop[flr] = rand()%2 ? 1 : -1;\r\n        else\r\n            lift_stop[flr] = 2;\r\n        p++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* Output results, header row *\/<\/span>\r\n    printf(\"%11s\",\"Floor:\");\r\n    for(x=0; x&lt;floors; x++)\r\n        printf(\" %2d \",x+1);\r\n    putchar('\\n');\r\n\r\n    <span class=\"comments\">\/* Going up *\/<\/span>\r\n    printf(\"%11s\",\"Going up:\");\r\n    for(x=0; x&lt;floors; x++)\r\n    {\r\n        if(lift_stop[x]==1 || lift_stop[x]==2)\r\n            printf(\"  &gt; \");\r\n        else\r\n            printf(\"    \");\r\n    }\r\n    putchar('\\n');\r\n\r\n    <span class=\"comments\">\/* Going down *\/<\/span>\r\n    printf(\"%11s\",\"Going down:\");\r\n    for(x=0; x&lt;floors; x++)\r\n    {\r\n        if(lift_stop[x]==-1 || lift_stop[x]==2)\r\n            printf(\"  &lt; \");\r\n        else\r\n            printf(\"    \");\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Obligatory sample runs:<\/p>\n<pre>     Floor:  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15 \r\n  Going up:                      >                                     \r\nGoing down:                  <   <       <   <   <       <<\/pre>\n<pre>     Floor:  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15 \r\n  Going up:  >                           >               >       >     \r\nGoing down:  <   <                   <                           <<\/pre>\n<pre>     Floor:  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15 \r\n  Going up:              >           >               >                 \r\nGoing down:                          <       <           <   <<\/pre>\n<pre>     Floor:  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15 \r\n  Going up:                      >                                   > \r\nGoing down:      <               <   <                   <   <   <<\/pre>\n<p>I hope your solution met with success and that you enjoyed the exercise. Remember, your code need not look exactly like mine to be correct; many different ways exist to solve common programming puzzles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To code a simulated elevator ride you must know the floor requests, up and down. This is the challenge given for this month&#8217;s Exercise, which can either drive you nuts or delight you depending on how you craft your solution.<\/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-5388","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\/5388","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=5388"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5388\/revisions"}],"predecessor-version":[{"id":5413,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5388\/revisions\/5413"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}