CJamie

sfj-ne

Oct 12th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. // C++ program to implement Shortest Job first with Arrival
  2. // Time
  3. #include <iostream>
  4. using namespace std;
  5. int mat[10][6];
  6.  
  7. void swap(int* a, int* b)
  8. {
  9. int temp = *a;
  10. *a = *b;
  11. *b = temp;
  12. }
  13.  
  14. void arrangeArrival(int num, int mat[][6])
  15. {
  16. for (int i = 0; i < num; i++) {
  17. for (int j = 0; j < num - i - 1; j++) {
  18. if (mat[j][1] > mat[j + 1][1]) {
  19. for (int k = 0; k < 5; k++) {
  20. swap(mat[j][k], mat[j + 1][k]);
  21. }
  22. }
  23. }
  24. }
  25. }
  26.  
  27. void completionTime(int num, int mat[][6])
  28. {
  29. int temp, val;
  30. mat[0][3] = mat[0][1] + mat[0][2];
  31. mat[0][5] = mat[0][3] - mat[0][1];
  32. mat[0][4] = mat[0][5] - mat[0][2];
  33.  
  34. for (int i = 1; i < num; i++) {
  35. temp = mat[i - 1][3];
  36. int low = mat[i][2];
  37. for (int j = i; j < num; j++) {
  38. if (temp >= mat[j][1] && low >= mat[j][2]) {
  39. low = mat[j][2];
  40. val = j;
  41. }
  42. }
  43. mat[val][3] = temp + mat[val][2];
  44. mat[val][5] = mat[val][3] - mat[val][1];
  45. mat[val][4] = mat[val][5] - mat[val][2];
  46. for (int k = 0; k < 6; k++) {
  47. swap(mat[val][k], mat[i][k]);
  48. }
  49. }
  50. }
  51.  
  52. int main()
  53. {
  54. int num, temp;
  55.  
  56. cout << "Enter number of Process: ";
  57. cin >> num;
  58.  
  59. cout << "...Enter the process ID...\n";
  60. for (int i = 0; i < num; i++) {
  61. cout << "...Process " << i + 1 << "...\n";
  62. cout << "Enter Process Id: ";
  63. cin >> mat[i][0];
  64. cout << "Enter Arrival Time: ";
  65. cin >> mat[i][1];
  66. cout << "Enter Burst Time: ";
  67. cin >> mat[i][2];
  68. }
  69.  
  70. cout << "Before Arrange...\n";
  71. cout << "Process ID\tArrival Time\tBurst Time\n";
  72. for (int i = 0; i < num; i++) {
  73. cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
  74. << mat[i][2] << "\n";
  75. }
  76.  
  77. arrangeArrival(num, mat);
  78. completionTime(num, mat);
  79. cout << "Final Result...\n";
  80. cout << "Process ID\tArrival Time\tBurst Time\tWaiting "
  81. "Time\tTurnaround Time\n";
  82. for (int i = 0; i < num; i++) {
  83. cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
  84. << mat[i][2] << "\t\t" << mat[i][4] << "\t\t"
  85. << mat[i][5] << "\n";
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment