OneCompiler

1

113

#include<iostream>
#include<vector>
using namespace std;

struct Process
{
int pro_no;
int arrivalTime;
int burstTime;
int waitingTime;
int turnaroundTime;
};

void FCFS(vector<Process>& proc)
{
int n = proc.size();
int currentTime = 0;
int totalWaitingTime = 0;
int totalTurnaroundTime = 0;
cout << "Displaying FCFS output" << endl;
cout << "Process\tTurnAround Time\t Waiting Time" << endl;
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;
    totalWaitingTime += proc[i].waitingTime;
    totalTurnaroundTime += proc[i].turnaroundTime;
    currentTime += proc[i].burstTime;
}
cout << "Average Waiting Time: " << static_cast<double>(totalWaitingTime) / n << endl;
cout << "Average Turnaround Time: " << static_cast<double>(totalTurnaroundTime) / n << endl;

}

int main()
{
int n;
cout << "Enter the number of processes" << 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 << "Arrival time of Process " << i + 1 << ": ";
cin >> proc[i].arrivalTime;
cout << "Burst time of Process " << i + 1 << ": ";
cin >> proc[i].burstTime;
}
FCFS(proc);
return 0;
}