To code a simulated elevator ride you must know the floor requests, up and down. This is the challenge given for this month’s Exercise, which can either drive you nuts or delight you depending on how you craft your solution.
For my solution, I use an array lift_stop[]
:
int lift_stop[floors];
The floors
constant sets the number of elements to 15. It didn’t fuss over creating a “lobby” floor as the challenge assumes the lobby is both the starting and ending point of the elevator’s journey.
The array is initialized to all zero values:
for( x=0; x<floors; x++ )
lift_stop[x] = 0;
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 passengers
to represent the number of riders (8):
p = 0;
while( p<passengers )
{
flr = rand() % floors;
if( lift_stop[flr]==0 )
lift_stop[flr] = rand()%2 ? 1 : -1;
else
lift_stop[flr] = 2;
p++;
}
Variable flr
holds the random floor, where a stop is made.
If the element represented by flr
(lift_stop[flr]
) 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’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.
Once the array is built, two for loops scan the array. The first for loop outputs stops going up. The second loop outputs stops going down.
Here is the full code for my solution:
2022_06-Exercise.c
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { const int floors=15,passengers=8; int lift_stop[floors]; int x,p,flr; /* seed randomizer */ srand( (unsigned)time(NULL) ); /* initialize elevator stops */ for( x=0; x<floors; x++ ) lift_stop[x] = 0; /* passengers set their levels */ p = 0; while( p<passengers ) { flr = rand() % floors; if( lift_stop[flr]==0 ) lift_stop[flr] = rand()%2 ? 1 : -1; else lift_stop[flr] = 2; p++; } /* Output results, header row */ printf("%11s","Floor:"); for(x=0; x<floors; x++) printf(" %2d ",x+1); putchar('\n'); /* Going up */ printf("%11s","Going up:"); for(x=0; x<floors; x++) { if(lift_stop[x]==1 || lift_stop[x]==2) printf(" > "); else printf(" "); } putchar('\n'); /* Going down */ printf("%11s","Going down:"); for(x=0; x<floors; x++) { if(lift_stop[x]==-1 || lift_stop[x]==2) printf(" < "); else printf(" "); } putchar('\n'); return(0); }
Obligatory sample runs:
Floor: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Going up: > Going down: < < < < < <
Floor: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Going up: > > > > Going down: < < < <
Floor: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Going up: > > > Going down: < < < <
Floor: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Going up: > > Going down: < < < < < <
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.