fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int main() {
  6. int n;
  7. cout << "Enter number of processes: ";
  8. cin >> n;
  9.  
  10. int burst[100], process[100];
  11. int waiting[100], turnaround[100];
  12.  
  13. // Input
  14. for (int i = 0; i < n; i++) {
  15. process[i] = i + 1;
  16. cout << "Enter Burst Time for P" << i + 1 << ": ";
  17. cin >> burst[i];
  18. }
  19.  
  20. // Sort according to Burst Time
  21. for (int i = 0; i < n - 1; i++) {
  22. for (int j = i + 1; j < n; j++) {
  23. if (burst[i] > burst[j]) {
  24. swap(burst[i], burst[j]);
  25. swap(process[i], process[j]);
  26. }
  27. }
  28. }
  29.  
  30. // Calculate Waiting Time
  31. waiting[0] = 0;
  32.  
  33. for (int i = 1; i < n; i++) {
  34. waiting[i] = waiting[i - 1] + burst[i - 1];
  35. }
  36.  
  37. // Calculate Turnaround Time
  38. for (int i = 0; i < n; i++) {
  39. turnaround[i] = waiting[i] + burst[i];
  40. }
  41.  
  42. // Display result
  43. cout << "\nExecution Order: ";
  44.  
  45. for (int i = 0; i < n; i++) {
  46. cout << "P" << process[i] << " ";
  47. }
  48.  
  49. int totalWaiting = 0;
  50. int totalTurnaround = 0;
  51.  
  52. cout << "\n\nProcess\tBurst\tWaiting\tTurnaround\n";
  53.  
  54. for (int i = 0; i < n; i++) {
  55. cout << "P" << process[i] << "\t"
  56. << burst[i] << "\t"
  57. << waiting[i] << "\t"
  58. << turnaround[i] << endl;
  59.  
  60. totalWaiting += waiting[i];
  61. totalTurnaround += turnaround[i];
  62. }
  63.  
  64. cout << "\nAverage Waiting Time: "
  65. << (float)totalWaiting / n;
  66.  
  67. cout << "\nAverage Turnaround Time: "
  68. << (float)totalTurnaround / n;
  69.  
  70. return 0;
  71. }
Success #stdin #stdout 0.01s 5276KB
stdin
3
stdout
Enter number of processes: Enter Burst Time for P1: Enter Burst Time for P2: Enter Burst Time for P3: 
Execution Order: P1 P3 P2 

Process	Burst	Waiting	Turnaround
P1	-137833632	0	-137833632
P3	-137727538	-137833632	-275561170
P2	5411	-275561170	-275555759

Average Waiting Time: -1.37798e+08
Average Turnaround Time: -2.2965e+08