#include <stdio.h> int main() { int bt[20], p[20], wt[20], tat[20], i, j, n, total = 0, pos, temp; float avg_wt, avg_tat; printf("Enter number of processes: "); scanf("%d", &n); printf("\nEnter Burst Time:\n"); for (i = 0; i < n; i++) { printf("p%d: ", i + 1); scanf("%d", &bt[i]); p[i] = i + 1; // Process number } // Sorting of burst times and process numbers in ascending order for (i = 0; i < n; i++) { pos = i; for (j = i + 1; j < n; j++) { if (bt[j] < bt[pos]) pos = j; } // Swapping burst time temp = bt[i]; bt[i] = bt[pos]; bt[pos] = temp; // Swapping process number temp = p[i]; p[i] = p[pos]; p[pos] = temp; } wt[0] = 0; // Waiting time for the first process is 0 // Calculating waiting time for (i = 1; i < n; i++) { wt[i] = 0; for (j = 0; j < i; j++) wt[i] += bt[j]; total += wt[i]; } avg_wt = (float) total / n; // Average waiting time total = 0; printf("\nProcess\t Burst Time Waiting Time Turnaround Time\n"); for (i = 0; i < n; i++) { tat[i] = bt[i] + wt[i]; // Calculating turnaround time total += tat[i]; printf("p%d\t\t %d\t\t %d\t\t %d\n", p[i], bt[i], wt[i], tat[i]); } avg_tat = (float) total / n; // Average turnaround time printf("\nAverage Waiting Time = %.2f", avg_wt); printf("\nAverage Turnaround Time = %.2f\n", avg_tat); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter