Posts

Search

Airport Algorithms

QUESTION DESCRIPTION

Semester break has started and Bogar is going home from College (Tamil Siddha College). He is taking flight from Chennai to Delhi. After entering the Airport, he was asked to get into queue of the Boarding Pass and his turn came very quickly. Bogar a smart person observed that all the passengers of this airline were happy unlike other airlines where some of the people were always unhappy with the service.

So, Bogar asked the Staff about this and the Staff told that they follow the Round Robin algorithm so a fairness is seen in every order unlike priority algorithm therefore every customer feel special and are happy as they can see that their Boarding pass is in process.

Although the waiting time is more but the Passengers feel that their Boarding pass is in process so they are happy. He also told Bogar that they maintain a database which records how much time the Boarding pass of a passenger will be needing more for completion after it passes the time quantum and shifts that process to last in the queue of the processes that are under process.

You can assume Time quantum as a time after which the Staff checks if some other Passenger has arrived. Bogar is a programmer and wanted to gift a program to the airline which shows the service time (time at which that person will reach the counter), waiting time (service time -arrival time) for each person in the queue and turnaround time (total time from arrival till person gets boarding pass). Bogar is smart so he does not need your help in this but still you are dumb so solve this problem. Also, Calculate average waiting and average turnaround time.

Input: The first line contains the Number of friend/s. The next n line contains the Friend name, order give time(Burst Time) and time taken by chef to cook that order for F1, F2, F3. Fn. (Arrival Time=0) and Quantum Time (in milliseconds)

Output: - The first n lines should print input values, waiting time and Time in which the order completes (Turn Around time) for each friend in each line. Print average waiting and average turnaround time in next two lines respectively

TEST CASE 1

INPUT

4
eLabTeam
eCurriculaTeam
eVerifyTeam
eThinkTeam
3 6 4 2
0 0 0 0
2
OUTPUT
Waiting Time
Time Taken for eLabTeam=6
Time Taken for eCurriculaTeam=9
Time Taken for eVerifyTeam=9
Time Taken for eThinkTeam=6
Average Waiting Time=7.500000
TurnAround Time
Time Taken for eLabTeam=9
Time Taken for eCurriculaTeam=15
Time Taken for eVerifyTeam=13
Time Taken for eThinkTeam=8
Average TurnAround Time=11.250000

TEST CASE 2

INPUT

5
eLabTeam
eCurriculaTeam
eVerifyTeam
eThinkTeam
eResearchTeam
3 6 4 2 5
0 0 0 0 0
3
OUTPUT
Waiting Time
Time Taken for eLabTeam=0
Time Taken for eCurriculaTeam=11
Time Taken for eVerifyTeam=14
Time Taken for eThinkTeam=9
Time Taken for eResearchTeam=15
Average Waiting Time=9.800000
TurnAround Time
Time Taken for eLabTeam=3
Time Taken for eCurriculaTeam=17
Time Taken for eVerifyTeam=18
Time Taken for eThinkTeam=11
Time Taken for eResearchTeam=20
Average TurnAround Time=13.800000

Code :

#include<iostream>
#include<iomanip>
using namespace std;
 void roundRobin(int len,string p[], int a[],int b[], int n) 
    { 
         int res = 0; 
        int resc = 0; 
     string seq;
    int res_b[len];  
        int res_a[len];
  
        for (int i = 0; i < len; i++) 
        { 
            res_b[i] = b[i]; 
            res_a[i] = a[i]; 
        } 
    int t = 0; 
        int w[len];
    int comp[len];  
        while (true) { 
            bool flag = true; 
            for (int i = 0; i <len; i++) 
            { 
                if (res_a[i] <= t)
                { 
                    if (res_a[i] <= n)
                    { 
                        if (res_b[i] > 0)
                        { 
                            flag = false; 
                            if (res_b[i] > n)
                            { 
                                t = t + n; 
                                res_b[i] = res_b[i] - n; 
                                res_a[i] = res_a[i] + n; 
                                seq += "->" + p[i]; 
                            } 
                            else 
                            { 
                              t = t + res_b[i]; 
           comp[i] = t - a[i]; 
           w[i] = t - b[i] - a[i]; 
                                res_b[i] = 0; 
                                seq += "->" + p[i]; 
                            } 
                        } 
                    } 
                    else if (res_a[i] > n)
                    { 
  
                        for (int j = 0; j <len; j++)
                        {
                          if (res_a[j] < res_a[i])
                          { 
                                if (res_b[j] > 0)
                                { 
                                    flag = false; 
                                    if (res_b[j] > n)
                                    { 
                                        t = t + n; 
                                        res_b[j] = res_b[j] - n; 
                                        res_a[j] = res_a[j] + n; 
                                        seq += "->" + p[j]; 
                                    } 
                                    else { 
                                        t = t + res_b[j]; 
                                        comp[j] = t - a[j]; 
                                        w[j] = t - b[j] - a[j]; 
                                        res_b[j] = 0; 
                                        seq += "->" + p[j]; 
                                    } 
                                } 
                            } 
                        } 
                        if (res_b[i] > 0) 
                        { 
                            flag = false;   
                            if (res_b[i] > n) 
                            { 
                                t = t + n; 
                                res_b[i] = res_b[i] - n; 
                                res_a[i] = res_a[i] + n; 
                                seq += "->" + p[i]; 
                            } 
                            else
                            { 
                                t = t + res_b[i]; 
                                comp[i] = t - a[i]; 
                                w[i] = t - b[i] - a[i]; 
                                res_b[i] = 0; 
                                seq += "->" + p[i]; 
                            } 
                        } 
                    } 
                } 
     else if (res_a[i] > t) 
     { 
                    t++; 
                    i--; 
                } 
            } 
            if (flag) { 
                break; 
            } 
        } 
        cout<<"Waiting Time";
        for (int i = 0; i < len; i++) { 
          cout<<"\nTime Taken for "<<p[i]<<"="<<w[i];
          res = res + w[i]; 
            resc = resc + comp[i]; 
        } 
   cout<<fixed;
   cout<<"\nAverage Waiting Time="<< (float)res / len; 
   cout<<"\nTurnAround Time";
    for (int i = 0; i < len; i++) { 
          cout<<"\nTime Taken for "<<p[i]<<"="<<comp[i];
    }
  
      
        cout<<"\nAverage TurnAround Time="<< (float)resc / len; 
      
    } 
  int main() 
{   int n ;
  cin>>n;
  int processes[n],arrivaltime[n];
  string name[n];
    int bursttime[n]; 
    int q ;
  for(int i=0;i<n;i++)
  {
    cin>>name[i];
  }
  for(int i=0;i<n;i++)
  {
    cin>>bursttime[i];
     processes[i]=i;
  }
  for(int i=0;i<n;i++)
  {
    cin>>arrivaltime[i];
  }
 cin>>q;
   roundRobin(n,name, arrivaltime, bursttime, q);
    return 0; 
}