#include <stdio.
h>
struct Process {
int pid; // Process ID
int at; // Arrival Time
int bt; // Burst Time
int ct; // Completion Time
int wt; // Waiting Time
int tat; // Turnaround Time
};
int main() {
int n, i, j;
struct Process p[20], temp;
float total_wt = 0, total_tat = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
// Input arrival and burst times
for (i = 0; i < n; i++) {
p[i].pid = i + 1;
printf("Enter arrival time of Process %d: ", i + 1);
scanf("%d", &p[i].at);
printf("Enter burst time of Process %d: ", i + 1);
scanf("%d", &p[i].bt);
}
// Sort by arrival time
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].at > p[j].at) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
// Calculate completion, turnaround and waiting time
int current_time = 0;
for (i = 0; i < n; i++) {
if (current_time < p[i].at)
current_time = p[i].at;
p[i].ct = current_time + p[i].bt;
current_time = p[i].ct;
p[i].tat = p[i].ct - p[i].at;
p[i].wt = p[i].tat - p[i].bt;
total_wt += p[i].wt;
total_tat += p[i].tat;
}
// Display results
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n",
p[i].pid, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt);
}
printf("\nAverage Waiting Time: %.2f", total_wt / n);
printf("\nAverage Turnaround Time: %.2f\n", total_tat / n);
return 0;
}