al__nasim

srtn2.cpp

Jan 14th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int at[10],bt[10],rt[10],endTime,i,smallest;
  5.     int remain=0,n,time,sum_wait=0,sum_turnaround=0;
  6.     printf("Enter no of Processes : ");
  7.     scanf("%d",&n);
  8.     for(i=0;i<n;i++)
  9.     {
  10.         printf("Enter arrival time for Process P%d : ",i+1);
  11.         scanf("%d",&at[i]);
  12.         printf("Enter burst time for Process P%d : ",i+1);
  13.         scanf("%d",&bt[i]);
  14.         rt[i]=bt[i];
  15.     }
  16.     printf("\n\nProcess\t|Turnaround Time| Waiting Time\n\n");
  17.     rt[9]=9999;
  18.     for(time=0;remain!=n;time++)
  19.     {
  20.         smallest=9;
  21.         for(i=0;i<n;i++)
  22.         {
  23.             if(at[i]<=time && rt[i]<rt[smallest] && rt[i]>0)
  24.             {
  25.                 smallest=i;
  26.             }
  27.         }
  28.         rt[smallest]--;
  29.         if(rt[smallest]==0)
  30.         {
  31.             remain++;
  32.             endTime=time+1;
  33.             printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-bt[smallest]-at[smallest]);
  34.             sum_wait+=endTime-bt[smallest]-at[smallest];
  35.             sum_turnaround+=endTime-at[smallest];
  36.         }
  37.     }
  38.     printf("\n\nAverage waiting time = %f\n",sum_wait*1.0/n);
  39.     printf("Average Turnaround time = %f",sum_turnaround*1.0/5);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment