We are running an elevator company and are currently developing a new product. We want our product to work for every building in the country, so we need it to be able to handle a variable number of floors.
Come up with a list of requirements it has to fulfil. Then implement a Pseudocode algorithm that meets the requirements.
// Declare bubble sort function with input type array and return type array FUNCTION bubble_sort(Numbers: ARRAY) DECLARE NumUnsorted, Index, TempNumber: INTEGER DECLARE IsSorted: BOOLEAN NumUnsorted <- LENGTH(Numbers) - 1 REPEAT IsSorted <- TRUE FOR Index <- 1 TO NumUnsorted IF Numbers[Index] > Numbers[Index + 1] THEN TempNumber <- Numbers[Index] Numbers[Index] <- Numbers[Index + 1] Numbers[Index + 1] <- TempNumber IsSorted <- FALSE ENDIF NEXT Index NumUnsorted <- NumUnsorted - 1 UNTIL IsSorted = TRUE RETURN Numbers ENDFUNCTION // Declare our variables DECLARE NumOfFloors, NewFloor, i: INTEGER DECLARE IsInArray: BOOLEAN DECLARE FloorRequestArray: ARRAY[0:NumOfFloors] NumOfFloors <- 4 // Create a function that takes in floor requests FUNCTION make_floor_request(NewFloor: INTEGER, FloorRequestArray: ARRAY) NewFloor <- INPUT("Enter a floor") IsInArray <- FALSE FOR i <- 0 TO LENGTH(FloorRequestArray) IF FloorRequestArray[i] = NewFloor THEN IsInArray <- TRUE ENDIF NEXT i IF IsInArray = FALSE THEN INSERT NewFloor INTO FloorRequestArray FloorRequestArray <- bubble_sort(FloorRequestArray) ENDIF RETURN FloorRequestArray ENDFUNCTION // Create a function that processes the floor requests DECLARE ElevatorDirection: STRING DECLARE CurrentFloor, NextFloor: INTEGER ElevatorDirection <- "idle" CurrentFloor <- 1 FUNCTION floor_request_process(FloorRequestArray: ARRAY) FUNCTION return_next_higher_floor() DECLARE i: INTEGER i <- 0 WHILE CurrentFloor < FloorRequestArray[i] DO NextFloor <- FloorRequestArray[i] i <- i + 1 ENDWHILE ENDFUNCTION FUNCTION return_next_lower_floor() DECLARE i: INTEGER i <- 0 WHILE CurrentFloor > FloorRequestArray[i] DO NextFloor <- FloorRequestArray[i-1] i <- i + 1 ENDWHILE ENDFUNCTION IF ElevatorDirection = 'DOWN' THEN return_next_lower_floor() ELSE return_next_higher_floor() ENDIF ENDFUNCTION DECLARE DoorOpen: BOOLEAN DoorOpen <- FALSE FUNCTION elevator_carrying_out_request(FloorRequestArray: ARRAY) SWITCH NextFloor CASE NextFloor > CurrentFloor THEN ElevatorDirection <- 'UP' CurrentFloor <- CurrentFloor + 1 elevator_carrying_out_request(FloorRequestArray) CASE NextFloor = CurrentFloor THEN DoorOpen <- TRUE DoorOpen <- FALSE floor_request_process(FloorRequestArray) elevator_carrying_out_request(FloorRequestArray) CASE NextFloor < CurrentFloor THEN ElevatorDirection <- 'DOWN' CurrentFloor <- CurrentFloor - 1 elevator_carrying_out_request(FloorRequestArray) ENDSWITCH ENDFUNCTION