fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int time_quantum(int n, int b_t[]){
  5. int sum = 0;
  6. for(int i=0; i<n; i++){
  7. sum += b_t[i];
  8. }
  9. float mean = (float)sum/n;
  10. float median;
  11. if(n % 2 == 0){
  12. median = ((b_t[n/2]) + (b_t[n/2] + 1)) / 2;
  13. }
  14. else{
  15. median = b_t[(n+1)/2];
  16. }
  17. int tq = sqrt(mean + median);
  18. return tq;
  19. }
  20. void calculate_wt(int p[], int n, int b_t[], int wt[], int quantum){
  21. int rem_b_t[n];
  22. for (int i = 0 ; i < n ; i++)
  23. rem_b_t[i] = b_t[i];
  24.  
  25. int t = 0;
  26. while(true){
  27. bool ok = true;
  28. for(int i = 0 ; i < n; i++){
  29. if(rem_b_t[i] > 0){
  30. ok = false;
  31. if(rem_b_t[i] > quantum){
  32. t += quantum;
  33. rem_b_t[i] -= quantum;
  34. }
  35. else{
  36. t += rem_b_t[i];
  37. wt[i] = t - b_t[i];
  38. rem_b_t[i] = 0;
  39. }
  40. }
  41. }
  42. if(ok == true)
  43. break;
  44. }
  45. }
  46.  
  47. void calculate_tat(int p[], int n, int b_t[], int wt[], int tat[]){
  48.  
  49. for(int i = 0; i < n ; i++)
  50. tat[i] = b_t[i] + wt[i];
  51. }
  52.  
  53. void solvio(int p[], int n, int b_t[], int quantum){
  54. int wt[n], tat[n], total_wt = 0, total_tat = 0;
  55. calculate_wt(p, n, b_t, wt, quantum);
  56. calculate_tat(p, n, b_t, wt, tat);
  57. cout << "PN\t "<< " \tb_t " << " WT " << " \tTAT\n";
  58. for(int i=0; i<n; i++){
  59. total_wt = total_wt + wt[i];
  60. total_tat = total_tat + tat[i];
  61. cout << " " << i+1 << "\t\t" << b_t[i] <<"\t " << wt[i] <<"\t\t " << tat[i] <<endl;
  62. }
  63. cout << "Average waiting time = " << (float)total_wt/(float)n << endl;
  64. cout << "Average turn around time = " << (float)total_tat/(float)n << endl;
  65. }
  66.  
  67. int main(){
  68. int n;
  69. cout << "Enter the number of p : ";
  70. cin >> n;
  71. int p[n], a[n], b[n];
  72. cout << "Enter the p name arrival time and burst time of the p : " << endl;
  73. for(int i=0; i<n; i++){
  74. cin >> p[i] >> a[i] >> b[i];
  75. }
  76. int tq = time_quantum(n, b);
  77. solvio(p, n, b, tq);
  78. return 0;
  79. }
  80.  
Success #stdin #stdout 0.01s 5520KB
stdin
3
p1 0 6
p2 0 3
p3 0 9
stdout
Enter the number of p : Enter the p name arrival time and burst time of the p : 
PN	  	b_t  WT  	TAT
 1		3	 0		 3
 2		0	 0		 0
 3		1552849664	 3		 1552849667
Average waiting time = 1
Average turn around time = 5.17617e+08