You are on page 1of 3

THEORY OF

COMPUTATION

Q2 : the elevator algorithm

The elevator algorithm :

1- The user enters the number of floors for the elevator.

2- He sends the requests that will be sent from these floors.

3- The temporary memory receives these requests and sends them to the processor to calculate the

required time and distance to reach each floor if we assume that the current position of the

elevator is the first floor, then this preliminary data appears in the output.

4- The user enters the current floor of the elevator.

5- Based on the user's entry for the current floor, operations are arranged according to the floors

closest to the current floor, and the furthest floors are arranged sequentially according to ascent

or descent, and data is sent for execution.

6- The time and distance traveled by the elevator are calculated to complete this process.

Student: Esmaail mo. Almawri –


CS3
Console.WriteLine("enter the floor size :");

int floorsize = Convert.ToInt32(Console.ReadLine());


List<int> FloorsNum = new List<int>();
Console.WriteLine("enter the num of floor");
for (int i = 0; i < floorsize; i++)
{

int floorNum = int.Parse(Console.ReadLine());


FloorsNum.Add(floorNum);
string Prosse = ($"Send the floor number:{floorNum.ToString()} for processing" );
Console.WriteLine(Prosse);

}
Console.WriteLine("=================================");
await Task.Delay(1000);
Console.WriteLine("Prossing in CPU...");
await Task.Delay(2000);

for (int i = 0; i < floorsize; i++)


{

Console.WriteLine($"The Height:{ FloorsNum[i] * 3 } Meter");


Console.WriteLine($"time taken: :{ FloorsNum[i] * 3 } Second");

Console.WriteLine("enter The current floor of the elevator :");


int currentFloor = int.Parse(Console.ReadLine());
FloorsNum.Sort((a, b) => Math.Abs(a - currentFloor).CompareTo(Math.Abs(b - currentFloor)));

int totalDistance = 0;
int totalTime = 0;
for (int i = 0; i < floorsize; i++)
{
int distance = Math.Abs(currentFloor - FloorsNum[i]) * 3;
int time = distance / 3 * 3;

totalDistance += distance;
totalTime += time;

currentFloor = FloorsNum[i];
}

foreach (int floorNum in FloorsNum)


{
Console.WriteLine("Go to floor " + floorNum);

}
Console.WriteLine("The elevator will travel " + totalDistance + " meters and it will take " + totalTime + " seconds to reach all the
floors.");

-------------------------------

2
Student: Esmaail mo. Almawri –CS3
These are its outputs:

3
Student: Esmaail mo. Almawri –CS3

You might also like