icp/oer/courses/c-newcomers/sections/03-lift/07-requests-over-time/program.c

73 lines
1.3 KiB
C

#include <stdio.h>
#include <stdbool.h>
int main()
{
int CurrentFloor = 8;
int TargetFloor = 2;
int RequestsOverTime[15] = {2,6,1,8,10,5,3,2,10,7,8,4,5,1,7};
int PendingFloors[10] = {};
bool reachedAllPendingFloors = false;
bool isDone = false;
for(int Timestep = 0; Timestep < 15 && !isDone; Timestep++)
{
printf("I'm currently at floor %d\n", CurrentFloor);
// TODO: Take the next Request from RequestOverTime and put it into the PendingFloors array
// at the index of the timestep
if(CurrentFloor < TargetFloor)
{
CurrentFloor++;
}
else
{
CurrentFloor--;
}
bool foundFloor = false;
if(!reachedAllPendingFloors && PendingFloors[CurrentFloor])
{
printf("I waited in floor %d.\n", CurrentFloor);
PendingFloors[CurrentFloor] = false;
foundFloor = true;
}
reachedAllPendingFloors = true;
for(int i = 0; i < 15; i++)
{
if(PendingFloors[i] != false)
{
reachedAllPendingFloors = false;
break;
}
}
if(CurrentFloor == TargetFloor)
{
if(!foundFloor)
printf("I waited in floor %d.\n", CurrentFloor);
if(!reachedAllPendingFloors)
{
for(int i = 0; i < 15; i++)
{
if(PendingFloors[i] != false)
{
TargetFloor = PendingFloors[i];
PendingFloors[i] = false;
break;
}
}
}
else
{
isDone = true;
}
}
}
}