fork download
  1. #include<stdio.h>
  2. int n,i,j,at[10],bt[10],p[10],ct[10],sum=0,tat[10],wt[10];
  3. float avgtat,avgwt;
  4. void swap(int a[])
  5. {
  6. int temp=a[i];
  7. a[i]=a[j];
  8. a[j]=temp;
  9. }
  10. int main()
  11. {
  12. printf("\nEnter the number pf processes: ");
  13. scanf("%d",&n);
  14. for(i=1;i<=n;i++)
  15. {
  16. printf("\nEnter the arrival time of P%d: ",i);
  17. scanf("%d",&at[i]);
  18. printf("\nEnter the burst time of P%d: ",i);
  19. scanf("%d",&bt[i]);
  20. p[i]=i;
  21. }
  22. for(i=1;i<=n;i++)
  23. {
  24. for(j=i+1;j<=n;j++)
  25. {
  26. if(at[i]>at[j])
  27. {
  28. swap(at);
  29. swap(bt);
  30. swap(p);
  31. }
  32. }
  33. }
  34. for(i=1;i<=n;i++)
  35. {
  36. if(sum<at[i])
  37. {
  38. sum=at[i]+sum;
  39. }
  40. sum=sum+bt[i];
  41. ct[i]=sum;
  42. }
  43. for(i=1;i<=n;i++)
  44. {
  45. for(j=i+1;j<=n;j++)
  46. {
  47. if(p[i]>p[j])
  48. {
  49. swap(at);
  50. swap(bt);
  51. swap(p);
  52. swap(ct);
  53. }
  54. }
  55. }
  56. for(i=1;i<=n;i++)
  57. {
  58. tat[i]=ct[i]-at[i];
  59. wt[i]=tat[i]-bt[i];
  60. avgtat=avgtat+tat[i];
  61. avgwt=avgwt+wt[i];
  62. }
  63. printf("\nprocess at bt ct tat wt");
  64. for(i=1;i<=n;i++)
  65. {
  66. printf("\nP%d\t\t%d\t%d\t%d\t%d\t%d",p[i],at[i],bt[i],ct[i],tat[i],wt[i]);
  67. }
  68. printf("\nthe average turnaroundtime is: %f",avgtat/n);
  69. printf("\nthe average waiting time is: %f",avgwt/n);
  70. return 0;
  71.  
  72.  
  73. }
Success #stdin #stdout 0s 5448KB
stdin
3
2
7
5
2
1
4
stdout
Enter the number pf processes: 
Enter the arrival time of P1: 
Enter the burst time of P1: 
Enter the arrival time of P2: 
Enter the burst time of P2: 
Enter the arrival time of P3: 
Enter the burst time of P3: 
process at bt ct tat wt
P1		2	7	12	10	3
P2		5	2	14	9	7
P3		1	4	5	4	0
the average turnaroundtime is: 7.666667
the average waiting time is: 3.333333