Posts

Search

Project Duration

QUESTION DESCRIPTION

Software Engineer Saran is assigned with several projects from various companies by his Team lead. He is also insisted to complete the projects based on their duration (ie.,Shortest duration first). Project order date and their duration is specified. You are asked to calculate the individual and the average waiting time of the project. Also find their individual and the average turnaround time of each project.
Input :
Line 1 : No of Process
Line 2 : Burst time and arrival of each process (Single space between them)

TEST CASE 1

INPUT

5
14 2
8 4
6 1
17 3
5 5
OUTPUT
Pid BT AT WT TAT
p[3] 6 1 0 5
p[5] 5 5 1 6
p[2] 8 4 7 15
p[1] 14 2 17 31
p[4] 17 3 30 47
AVERAGE WAITING TIME:11.000000
AVERAGE TURN AROUND TIME:20.799999

TEST CASE 2

INPUT

4
5 4
4 1
6 2
8 3
OUTPUT
Pid BT AT WT TAT
p[2] 4 1 0 3
p[1] 5 4 0 5
p[3] 6 2 7 13
p[4] 8 3 12 20
AVERAGE WAITING TIME:4.750000
AVERAGE TURN AROUND TIME:10.250000

Code :

#include<iostream> 
#include<bits/stdc++.h>
using namespace std; 
int mat[10][6]; 
int avgt=0,avgw=0;
void swap(int *a,int *b) 
{ 
 int temp = *a; 
 *a = *b; 
 *b = temp; 
} 
void arrangeArrival(int num, int mat[][6]) 
{ 
 for(int i=0;i<num;i++) 
 { 
  for(int j=0;j<num-i-1;j++) 
  { 
   if(mat[j][1]>mat[j+1][1]) 
   { 
    for(int k=0;k<5;k++) 
    { 
     swap(mat[j][k],mat[j+1][k]); 
    } 
   } 
  } 
 } 
} 
void completionTime(int num, int mat[][6]) 
{ 
 int temp, val; 
 mat[0][3]=mat[0][1]+mat[0][2]; 
 mat[0][5]=mat[0][3]-mat[0][1]; 
 mat[0][4]=mat[0][5]-mat[0][2]; 
 for(int i=1;i<num;i++) 
 { 
  temp=mat[i-1][3]; 
  int low=mat[i][2]; 
  for(int j=i;j<num;j++) 
  { 
   if(temp>=mat[j][1]&&low>=mat[j][2]) 
   { 
    low=mat[j][2]; 
    val=j; 
   } 
  } 
  mat[val][3]=temp+mat[val][2]; 
  mat[val][5]=mat[val][3]-mat[val][1]; 
  mat[val][4]=mat[val][5]-mat[val][2]; 
  for(int k=0;k<6;k++) 
  { 
   swap(mat[val][k],mat[i][k]); 
  } 
 } 
} 
int main() 
{ 
 int num, temp; 
 cin>>num; 
 for(int i=0;i<num;i++) 
 { 
  cin>>mat[i][2]; 
       mat[i][0]=i+1; 
  cin>>mat[i][1]; 
 } 
 arrangeArrival(num,mat); 
 completionTime(num,mat); 
 cout<<"Pid BT AT WT TAT\n"; 
 for(int i=0;i<num;i++) 
 { 
  if(i==0){
        cout<<"p["<<mat[i][0]<<"] "<<mat[i][2]<<" "<<mat[i][1]<<" 0 "<<mat[i][5]-1<<"\n"; 
    avgt+=mat[i][5]-1;
        }
       else{
           cout<<"p["<<mat[i][0]<<"] "<<mat[i][2]<<" "<<mat[i][1]<<" "<<mat[i][4]-1<<" "<<mat[i][5]-1<<"\n";
         avgw+=mat[i][4]-1;
           avgt+=mat[i][5]-1;
        }
    } 
   cout<<"AVERAGE WAITING TIME:"<<fixed<<setprecision(6)<<(float)avgw/(float)num<<endl;
   cout<<"AVERAGE TURN AROUND TIME:"<<fixed<<setprecision(6)<<(float)avgt/(float)num;
}