#include using namespace std; struct Process { int id; // Process ID int burstTime; // Burst Time int arrivalTime; // Arrival Time int remainingTime; // Remaining time for execution }; // Function to calculate average waiting, turnaround, and response times void findAverageTime(Process processes[], int n) { int currentTime = 0; // Current time in the scheduling simulation int completed = 0; // Counter for completed processes int waitingTime[n] = {0}; // Waiting times for each process int turnaroundTime[n] = {0}; // Turnaround times for each process int responseTime[n]; // Response times for each process bool firstResponse[n] = {false}; // Check if a process has started for response time calculation for (int i = 0; i < n; i++) { responseTime[i] = -1; } while (completed != n) { int shortest = -1; // Process index with the shortest remaining time int minRemainingTime = INT_MAX; // Find the process with the shortest remaining time that has arrived for (int i = 0; i < n; i++) { if (processes[i].arrivalTime <= currentTime && processes[i].remainingTime > 0 && processes[i].remainingTime < minRemainingTime) { minRemainingTime = processes[i].remainingTime; shortest = i; } } // If no process is available at this time, increment the time if (shortest == -1) { currentTime++; continue; } // Record the response time if the process is executing for the first time if (!firstResponse[shortest]) { responseTime[shortest] = currentTime - processes[shortest].arrivalTime; firstResponse[shortest] = true; } // Process the selected process for 1 time unit processes[shortest].remainingTime--; currentTime++; // If the process is completed if (processes[shortest].remainingTime == 0) { completed++; int finishTime = currentTime; turnaroundTime[shortest] = finishTime - processes[shortest].arrivalTime; waitingTime[shortest] = turnaroundTime[shortest] - processes[shortest].burstTime; } } // Calculate and print average times int totalWaitingTime = 0, totalTurnaroundTime = 0, totalResponseTime = 0; for (int i = 0; i < n; i++) { totalWaitingTime += waitingTime[i]; totalTurnaroundTime += turnaroundTime[i]; totalResponseTime += responseTime[i]; } float avgWaitingTime = (float)totalWaitingTime / n; float avgTurnaroundTime = (float)totalTurnaroundTime / n; float avgResponseTime = (float)totalResponseTime / n; cout << "Average Turnaround Time: " << avgTurnaroundTime << endl; cout << "Average Waiting Time: " << avgWaitingTime << endl; cout << "Average Response Time: " << avgResponseTime << endl; } int main() { int n; cout << "Enter the number of processes: "; cin >> n; Process processes[n]; for (int i = 0; i < n; i++) { processes[i].id = i + 1; cout << "Enter burst time for Process " << processes[i].id << ": "; cin >> processes[i].burstTime; cout << "Enter arrival time for Process " << processes[i].id << ": "; cin >> processes[i].arrivalTime; processes[i].remainingTime = processes[i].burstTime; // Initialize remaining time } findAverageTime(processes, n); return 0; }