fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int n, i;
  5. int bt[20], wt[20], tat[20];
  6. float total_wt = 0, total_tat = 0;
  7.  
  8. printf("Enter number of processes: ");
  9. scanf("%d", &n);
  10.  
  11. printf("Enter burst time for each process:\n");
  12. for (i = 0; i < n; i++) {
  13. printf("P%d: ", i + 1);
  14. scanf("%d", &bt[i]);
  15. }
  16.  
  17. // Waiting time for first process is always 0
  18. wt[0] = 0;
  19. // Calculate waiting time for all processes
  20. for (i = 1; i < n; i++) {
  21. wt[i] = wt[i - 1] + bt[i - 1];
  22. }
  23.  
  24. // Calculate turnaround time and totals
  25. printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
  26. for (i = 0; i < n; i++) {
  27. tat[i] = bt[i] + wt[i];
  28. total_wt += wt[i];
  29. total_tat += tat[i];
  30. printf("P%d\t\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
  31. }
  32.  
  33. // Print averages
  34. printf("\nAverage Waiting Time: %.2f", total_wt / n);
  35. printf("\nAverage Turnaround Time: %.2f\n", total_tat / n);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
Enter number of processes: Enter burst time for each process:

Process	Burst Time	Waiting Time	Turnaround Time

Average Waiting Time: -nan
Average Turnaround Time: -nan