fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int A[100][4], n, total_wt = 0, total_tat = 0;
  6. float avg_wt, avg_tat;
  7.  
  8. cout << "Enter number of processes: ";
  9. cin >> n;
  10.  
  11. cout << "Enter Burst Time for each process:\n";
  12. for (int i = 0; i < n; i++) {
  13. cout << "P" << i + 1 << ": ";
  14. cin >> A[i][1]; // Burst Time
  15. A[i][0] = i + 1; // Process ID
  16. }
  17.  
  18. // Sort processes by Burst Time (Selection Sort)
  19. for (int i = 0; i < n - 1; i++) {
  20. int min_idx = i;
  21. for (int j = i + 1; j < n; j++)
  22. if (A[j][1] < A[min_idx][1])
  23. min_idx = j;
  24.  
  25. swap(A[i][0], A[min_idx][0]); // Swap Process IDs
  26. swap(A[i][1], A[min_idx][1]); // Swap Burst Times
  27. }
  28.  
  29. A[0][2] = 0; // Waiting Time for first process is 0
  30.  
  31. // Calculate Waiting Time and Turnaround Time
  32. for (int i = 1; i < n; i++) {
  33. A[i][2] = A[i - 1][2] + A[i - 1][1];
  34. total_wt += A[i][2];
  35. }
  36.  
  37.  
  38. }
Success #stdin #stdout 0.01s 5276KB
stdin
45
stdout
Enter number of processes: Enter Burst Time for each process:
P1: P2: P3: P4: P5: P6: P7: P8: P9: P10: P11: P12: P13: P14: P15: P16: P17: P18: P19: P20: P21: P22: P23: P24: P25: P26: P27: P28: P29: P30: P31: P32: P33: P34: P35: P36: P37: P38: P39: P40: P41: P42: P43: P44: P45: