#include <stdio.h>
// Structure to represent a process
struct process {
int pid; // Process ID
int arrivaltime; // Arrival time of the process
int bursttime; // Burst time (execution time) of the process
int waitingtime; // Waiting time for the process
int turnaroundtime; // Turnaround time for the process
};
// Function to calculate the waiting time for each process
void calculatewaitingtime(struct process p[], int n) {
p[0].waitingtime = 0; // First process has no waiting time
for (int i = 1; i < n; i++) {
p[i].waitingtime = p[i - 1].waitingtime + p[i - 1].bursttime; // Cumulative waiting time
}
}
// Function to calculate the turnaround time for each process
void calculateturnaroundtime(struct process p[], int n) {
for (int i = 0; i < n; i++) {
p[i].turnaroundtime = p[i].bursttime + p[i].waitingtime; // Turnaround time = Burst time + Waiting time
}
}
// Function to display process details and calculate averages
void displayprocessdetails(struct process p[], int n) {
float totalwaitingtime = 0, totalturnaroundtime = 0;
printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
totalwaitingtime += p[i].waitingtime; // Accumulate total waiting time
totalturnaroundtime += p[i].turnaroundtime; // Accumulate total turnaround time
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrivaltime, p[i].bursttime, p[i].waitingtime, p[i].turnaroundtime);
}
printf("\nAverage Waiting Time: %.2f\n", totalwaitingtime / n); // Calculate and print average waiting time
printf("Average Turnaround Time: %.2f\n", totalturnaroundtime / n); // Calculate and print average turnaround time
}
int main() {
int n;
// Input the number of processes
printf("Enter number of processes: ");
scanf("%d", &n);
struct process p[n]; // Array of processes
// Input arrival time and burst time for each process
for (int i = 0; i < n; i++) {
p[i].pid = i + 1; // Assign process ID
printf("Enter arrival time and burst time for process P%d: ", i + 1);
scanf("%d %d", &p[i].arrivaltime, &p[i].bursttime);
}
// Calculate waiting and turnaround times
calculatewaitingtime(p, n);
calculateturnaroundtime(p, n);
// Display the process details and averages
displayprocessdetails(p, n);
return 0; // Exit the program
}