2
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Process
{
int pro_no;
int arrivalTime;
int burstTime;
int turnaroundTime;
int waitingTime;
};
void SJC(vector<Process> &proc)
{
int n = proc.size();
int currentTime = 0;
int totalTurnaroundTime = 0;
int totalWaitingTime = 0;
cout<<"Displaying SJF output:"<<endl;
cout<<"Process No\tTurnAround Time\tWaiting time"<<endl;
sort(proc.begin(), proc.end(), [](Process &a, Process &b){
return a.burstTime < b.burstTime;
});
for(int i=0; i<n; i++)
{
if(currentTime<proc[i].arrivalTime)
{
currentTime = proc[i].arrivalTime;
}
proc[i].waitingTime = currentTime - proc[i].arrivalTime;
proc[i].turnaroundTime = proc[i].waitingTime + proc[i].burstTime;
cout<<proc[i].pro_no<<"\t\t"<<proc[i].turnaroundTime<<"\t\t"<<proc[i].waitingTime<<endl;
totalTurnaroundTime += proc[i].turnaroundTime;
totalWaitingTime += proc[i].waitingTime;
currentTime += proc[i].burstTime;
}
cout<<"The average of total turnaraound time is: "<<totalTurnaroundTime/n<<endl;
cout<<"The average of total waiting time is: "<<totalWaitingTime/n<<endl;
}
int main()
{
int n;
cout<<"Enter the process number"<<endl;
cin>>n;
vector <Process> proc(n);
cout<<"Enter the arrival time and burst time: "<<endl;
for(int i=0; i<n; i++)
{
proc[i].pro_no = i+1;
cout<<"Enter arrival time "<<i+1<<endl;
cin>>proc[i].arrivalTime;
cout<<"Enter burst time "<<i+1<<endl;
cin>>proc[i].burstTime;
}
SJC(proc);
return 0;
}