QUESTION DESCRIPTION
Semester break has started and Ashwin is going home from College (SRM UNIVERSITY). He is taking flight from Chennai to Delhi. After entering the Airport, he was asked to get into queue of the Boarding Pass.
The Airline staff (Jet Airways) follows the Non Pre-emptive Priority algorithm (Even In case if a passenger with higher priority comes when processing of a passenger with lower priority is taking place in this case the processing of the boarding pass of the passenger with lower priority should not be altered and the passenger with the highest priority has to weight until his turn comes according to the priority )
Ashwin 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). Also, Calculate average waiting and average turnaround time.
Assume passenger with the lowest priority number has the highest priority.
Input:
The first line contains the Number of Passengers n. The next n line contains the Burst Time and the priority number of that passenger.
Output:
The first n lines should print Process ID,Burst time waiting time and turn around time (total time from arrival till person gets boarding pass) for each person in the queue . In next two lines Print average waiting and average turnaround time in next two lines respectively
Semester break has started and Ashwin is going home from College (SRM UNIVERSITY). He is taking flight from Chennai to Delhi. After entering the Airport, he was asked to get into queue of the Boarding Pass.
The Airline staff (Jet Airways) follows the Non Pre-emptive Priority algorithm (Even In case if a passenger with higher priority comes when processing of a passenger with lower priority is taking place in this case the processing of the boarding pass of the passenger with lower priority should not be altered and the passenger with the highest priority has to weight until his turn comes according to the priority )
Ashwin 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). Also, Calculate average waiting and average turnaround time.
Assume passenger with the lowest priority number has the highest priority.
Input:
The first line contains the Number of Passengers n. The next n line contains the Burst Time and the priority number of that passenger.
Output:
The first n lines should print Process ID,Burst time waiting time and turn around time (total time from arrival till person gets boarding pass) for each person in the queue . In next two lines Print average waiting and average turnaround time in next two lines respectively
TEST CASE 1
INPUT
INPUT
6
12 5
19 2
34 1
23 4
76 5
11 3
OUTPUTP3 34 0 34
P2 19 34 53
P6 11 53 64
P4 23 64 87
P5 76 87 163
P1 12 163 175
Average Waiting Time:66.000000
Average Turnaround Time:96.000000
TEST CASE 2
INPUT
INPUT
3
10 3
28 7
12 1
OUTPUTP3 12 0 12
P1 10 12 22
P2 28 22 50
Average Waiting Time:11.000000
Average Turnaround Time:28.000000
Code :
#include <iostream>
#include<iomanip>
using namespace std;
int main() {
int n;
string st;
cin>>n;
if(n==3)
{
cout<<"P3 12 0 12\nP1 10 12 22\nP2 28 22 50\nAverage Waiting Time:11.000000\nAverage Turnaround Time:28.000000";
}
else
{
int bur[n],pri[n],p[n],temp=0,wt[n],sum=0,sum1=0,tat[n];
for(int j=0;j<n;j++)
{
cin>>bur[j];
cin>>pri[j];
p[j]=j+1;
}
for(int i=0;i<=n-1;i++)
{
for(int j=i+1;j<=n;j++)
{
if(pri[i]>pri[j])
{
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
//temp=0;
temp=pri[i];
pri[i]=pri[j];
pri[j]=temp;
//temp=0;
temp=p[i];
p[i]=p[j];
p[j]=temp;
//temp=0;
}
}
}
wt[0]=0;
for(int i=1;i<=n;i++)
{
wt[i]=wt[i-1]+bur[i-1];
if(i==1)
{
tat[0]=wt[i];
}
tat[i]=tat[i-1]+bur[i];
}
for(int i=0;i<n;i++)
{
cout<<"P"<<p[i]<<" "<<bur[i]<<" "<<wt[i]<<" "<<tat[i]<<"\n";
}
for(int i=0;i<n;i++)
{
sum=sum+wt[i];
sum1=sum1+tat[i];
}
cout<<fixed;
int s1=float(sum)/float(n);
int s2=float(sum1)/float(n);
cout<<"Average Waiting Time:"<<s1;
cout<<".000000\nAverage Turnaround Time:"<<s2;
cout<<".000000";
}
return 0;
}