ab_tanjir

Untitled

Dec 17th, 2023
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void calculateTimes(int n, int bt[], int at[], int wt[], int tat[]) {
  4. int timer = 0;
  5.  
  6. for (int i = 0; i < n; i++) {
  7. int minArrivalIndex = -1;
  8.  
  9. // Find process with the lowest arrival time that has not been processed
  10. for (int j = 0; j < n; j++) {
  11. if (at[j] <= timer && wt[j] == -1) {
  12. if (minArrivalIndex == -1 || at[j] < at[minArrivalIndex]) {
  13. minArrivalIndex = j;
  14. }
  15. }
  16. }
  17.  
  18. // Process the selected process
  19. wt[minArrivalIndex] = timer - at[minArrivalIndex];
  20. tat[minArrivalIndex] = wt[minArrivalIndex] + bt[minArrivalIndex];
  21. timer += bt[minArrivalIndex];
  22. }
  23. }
  24.  
  25. void printResults(int n, int wt[], int tat[]) {
  26. float avgWT = 0, avgTAT = 0;
  27.  
  28. printf("Process\tWaiting Time\tTurnaround Time\n");
  29. for (int i = 0; i < n; i++) {
  30. printf("%d\t%d\t\t%d\n", i + 1, wt[i], tat[i]);
  31. avgWT += wt[i];
  32. avgTAT += tat[i];
  33. }
  34.  
  35. avgWT /= n;
  36. avgTAT /= n;
  37.  
  38. printf("Average Waiting Time: %.2f\n", avgWT);
  39. printf("Average Turnaround Time: %.2f\n", avgTAT);
  40. }
  41.  
  42. int main() {
  43. int n;
  44.  
  45. printf("Enter the number of processes: ");
  46. scanf("%d", &n);
  47.  
  48. int bt[n], at[n], wt[n], tat[n];
  49.  
  50. printf("Enter CPU times:\n");
  51. for (int i = 0; i < n; i++) {
  52. scanf("%d", &bt[i]);
  53. }
  54.  
  55. printf("Enter arrival times:\n");
  56. for (int i = 0; i < n; i++) {
  57. scanf("%d", &at[i]);
  58. wt[i] = -1; // Initialize waiting time to -1 to mark as not processed
  59. }
  60.  
  61. calculateTimes(n, bt, at, wt, tat);
  62. printResults(n, wt, tat);
  63.  
  64. return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment