By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Check error in Arrival time of SJF scheduling

P: 22
In this program there is error in processing time of last process..plz check the error...
the program is as
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. float avgwt,avgtt;
  6. char pname[10][10],c[10][10];
  7. int wt[10],tt[10],bt[10],at[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=0;
  8. clrscr();
  9. printf("\n\n Enter the number of processes:");
  10. scanf("%d",&n);
  11. printf("\n\n Enter the NAME,BURSTTIME and ARRIVALTIME of the processes");
  12. for(i=1;i<=n;i++)
  13. {
  14. printf("\n\n NAME :");
  15. scanf("%s",&pname[i]);
  16. printf("\n\nBURST TIME :");
  17. scanf("%d",&bt[i]);
  18. printf("\n\n ARRIVAL TIME :");
  19. scanf("%d",&at[i]);
  20. }
  21. for(i=1;i<n;i++)
  22. for(j=i+1;j<n;j++)
  23. {
  24. if(at[i]==at[j])
  25. if(bt[i]>bt[j])
  26. {
  27. t=at[i];
  28. at[i]=at[j];
  29. at[j]=t;
  30. q=bt[i];
  31. bt[i]=bt[j];
  32. bt[j]=q;
  33. strcpy(c[i],pname[i]);
  34. strcpy(pname[i],pname[j]);
  35. strcpy(pname[j],c[i]);
  36. }
  37. if(at[i]!=at[j])
  38. if(bt[i]>bt[j])
  39. {
  40. t=at[i];
  41. at[i]=at[j];
  42. at[j]=t;
  43. q=bt[i];
  44. bt[i]=bt[j];
  45. bt[j]=q;
  46. strcpy(c[i],pname[i]);
  47. strcpy(pname[i],pname[j]);
  48. strcpy(pname[j],c[i]);
  49. }
  50. }
  51. wt[1]=0;
  52. for(i=1;i<=n-1;i++)
  53. {
  54. wt[i+1]=wt[i]+bt[i];
  55. sum=sum+(wt[i]-at[i]);
  56. sbt=sbt+(wt[i+1]-at[i]);
  57. tt[i]=wt[i]+bt[i];
  58. ss=ss+bt[i];
  59. }
  60. printf("\n\n GANTT CHART");
  61. printf("\n\n ------------------------------------------------------------------\n");
  62. for(i=1;i<=n;i++)
  63. {
  64. printf("|\t%s\t",pname[i]);
  65. sbt=sbt+wt[i+1];
  66. tt[i]=wt[i]+bt[i];
  67. ss=ss+bt[i];
  68. }
  69. printf("\n\nGANTT CHART");
  70. printf("\n--------------------------------------------------------------------\n");
  71. for(i=1;i<=n;i++)
  72. {
  73. printf("|\t%s\t",pname[i]);
  74. }
  75. printf("\n--------------------------------------------------------------------\n");
  76. for(i=1;i<=n;i++)
  77. {
  78. printf("%d\t\t",wt[i]);
  79. }
  80. printf("%d\n",ss);
  81. printf("\n--------------------------------------------------------------------\n");
  82. printf("\n\n Total WAITING TIME of the process=%d",sum);
  83. printf("\n\nTotal TURNAROUND TIME of the process=%d",sbt);
  84. avgwt=(float)sum/n;
  85. avgtt=(float)sbt/n;
  86. printf("\n\nAverage WAITING TIME of the process=%f",avgwt);
  87. printf("\n\nAverage TURNAROUND TIME of the process=%f",avgtt);
  88. getch();
  89. }
  90.  
Oct 7 '10 #1
Share this Question
Share on Google+
10 Replies


ashitpro
Expert 100+
P: 542
I checked the code.
Its compiling as well as running properly, without any error.
What error are you taking about?
Is it logical error or what?
Oct 7 '10 #2

P: 22
yes,there is logical error in this.....
Oct 7 '10 #3

ashitpro
Expert 100+
P: 542
check line number 21

Expand|Select|Wrap|Line Numbers
  1. for(i=1;i<n;i++)
Don't you think it has to be:

Expand|Select|Wrap|Line Numbers
  1. for(i=1;i<=n;i++)
Otherwise it will skip the last record.
Oct 7 '10 #4

P: 22
i change it.but the error remain same....
Expand|Select|Wrap|Line Numbers
  1.  Enter the NAME,BURSTTIME and ARRIVALTIME of the processes
  2.  
  3.  NAME :a
  4.  
  5.  
  6. BURST TIME :3
  7.  
  8.  
  9.  ARRIVAL TIME :1
  10.  
  11.  
  12.  NAME :b
  13.  
  14.  
  15. BURST TIME :2
  16.  
  17.  
  18.  ARRIVAL TIME :1
  19.  
  20.  
  21.  NAME :c
  22.  
  23.  
  24. BURST TIME :5
  25.  
  26.  
  27.  ARRIVAL TIME :1
  28.  
  29.  
  30.  GANTT CHART
  31.  
  32.  ------------------------------------------------------------------
  33. |       b       |       a       |       c
  34.  
  35. GANTT CHART
  36. --------------------------------------------------------------------
  37. |       b       |       a       |       c
  38. --------------------------------------------------------------------
  39. 0               2               5               15
  40.  
  41. --------------------------------------------------------------------
  42.  
  43.  
  44.  Total WAITING TIME of the process=0
  45.  
  46. Total TURNAROUND TIME of the process=12
  47.  
  48. Average WAITING TIME of the process=0.000000
  49.  
  50. Average TURNAROUND TIME of the process=4.000000
  51.  
this is the output of one example which has same arrival time....in this output see in grant chart the process completion time of process c should be 10 but it shows 15..
Oct 7 '10 #5

ashitpro
Expert 100+
P: 542
Expand|Select|Wrap|Line Numbers
  1. # GANTT CHART
  2. # --------------------------------------------------------------------
  3. # |       b       |       a       |       c
  4. # --------------------------------------------------------------------
  5. # 0               2               5               15
  6. #  
  7. # --------------------------------------------------------------------
  8.  
According to your code, these figures are waiting times.
0,2,5 are the waiting times for respective processes. Like you are saying, you need 10 after 5, which makes no sense. Because, there is no fourth process in queue to display its waiting time.

15 is getting printed because of line 80. You are printing 'ss' variable.

You can just comment that line, and everything will start making sense.

If you want some other thing to be printed, like burst time or anything else, tell me.
Oct 7 '10 #6

P: 22
thanku for this....it shows waiting time zero which is incorect and trunaround time is also incorrect.
Trunaround time=time terminated-time entered
TAT of process a=5-1=4
TAt of process b=2-1=1
TAT of process c=10-1=9
then tatal trunaround time should be4+1+9=14
and avrage TAT should be 14/4
bt it show 12
plz check that waiting time and trunaround time..
Oct 7 '10 #7

ashitpro
Expert 100+
P: 542
Sure Sir/Mam,

Give me few minutes.
Oct 7 '10 #8

P: 22
the problem of trunaround time has solved now there was mistake in the for loop....
wt[1]=0;
for(i=1;i<=n-1;i++)
{
wt[i+1]=wt[i]+bt[i];
sum=sum+(wt[i]-at[i]);
sbt=sbt+(wt[i+1]-at[i]);
tt[i]=wt[i]+bt[i];
ss=ss+bt[i];
in this loop the i is start from 1 thats way it skip the first process time...
Expand|Select|Wrap|Line Numbers
  1. wt[1]=0; 
  2. for(i=1;i<=n-1;i++)
  3. {
  4. j=i-1;
  5. wt[i+1]=wt[i]+bt[i];
  6. sum=sum+(wt[i]-at[j]);
  7. sbt=sbt+(wt[i+1]-at[j]);
  8. tt[i]=wt[i]+bt[i]; 
  9. ss=ss+bt[i]; 
plz check it now plz plz
and plz solve the error of waiting time.
Oct 7 '10 #9

ashitpro
Expert 100+
P: 542
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. float avgwt,avgtt;
  6. char pname[10][10],c[10][10];
  7. int wt[10],tt[10],bt[10],at[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=0;
  8. clrscr();
  9. printf("\n\n Enter the number of processes:");
  10. scanf("%d",&n);
  11. printf("\n\n Enter the NAME,BURSTTIME and ARRIVALTIME of the processes");
  12. for(i=1;i<=n;i++)
  13. {
  14. printf("\n\n NAME :");
  15. scanf("%s",&pname[i]);
  16. printf("\n\nBURST TIME :");
  17. scanf("%d",&bt[i]);
  18. printf("\n\n ARRIVAL TIME :");
  19. scanf("%d",&at[i]);
  20. }
  21. for(i=1;i<=n;i++)
  22. for(j=i+1;j<n;j++)
  23. {
  24. if(at[i]==at[j])
  25. if(bt[i]>bt[j])
  26. {
  27. t=at[i];
  28. at[i]=at[j];
  29. at[j]=t;
  30. q=bt[i];
  31. bt[i]=bt[j];
  32. bt[j]=q;
  33. strcpy(c[i],pname[i]);
  34. strcpy(pname[i],pname[j]);
  35. strcpy(pname[j],c[i]);
  36. }
  37. if(at[i]!=at[j])
  38. if(bt[i]>bt[j])
  39. {
  40. t=at[i];
  41. at[i]=at[j];
  42. at[j]=t;
  43. q=bt[i];
  44. bt[i]=bt[j];
  45. bt[j]=q;
  46. strcpy(c[i],pname[i]);
  47. strcpy(pname[i],pname[j]);
  48. strcpy(pname[j],c[i]);
  49. }
  50. }
  51. wt[1]=0;
  52. for(i=1;i<=n-1;i++)
  53. {
  54. wt[i+1]=wt[i]+bt[i];
  55. sum=sum+(wt[i]-at[i]);
  56. sbt=sbt+(wt[i+1]-at[i]);
  57. tt[i]=wt[i]+bt[i];
  58. ss=ss+bt[i];
  59. }
  60. sum = 0;
  61. sbt = 0;
  62. for(i=1;i<=n;i++)
  63. {
  64.   sum=sum + wt[i];
  65.   sbt = sbt + (wt[i] + bt[i] - at[i]);
  66. }
  67.  
  68. printf("\n\n GANTT CHART");
  69. printf("\n\n ------------------------------------------------------------------\n");
  70. for(i=1;i<=n;i++)
  71. {
  72. printf("|\t%s\t",pname[i]);
  73. //sbt=sbt+wt[i+1];
  74. tt[i]=wt[i]+bt[i];
  75. ss=ss+bt[i];
  76. }
  77. printf("\n\nGANTT CHART");
  78. printf("\n--------------------------------------------------------------------\n");
  79. for(i=1;i<=n;i++)
  80. {
  81. printf("|\t%s\t",pname[i]);
  82. }
  83. printf("\n--------------------------------------------------------------------\n");
  84. for(i=1;i<=n;i++)
  85. {
  86. printf("%d\t\t",wt[i]);
  87. }
  88. printf("%d\n",ss);
  89. printf("\n--------------------------------------------------------------------\n");
  90. printf("\n\n Total WAITING TIME of the process=%d",sum);
  91. printf("\n\nTotal TURNAROUND TIME of the process=%d",sbt);
  92. avgwt=(float)sum/n;
  93. avgtt=(float)sbt/n;
  94. printf("\n\nAverage WAITING TIME of the process=%f",avgwt);
  95. printf("\n\nAverage TURNAROUND TIME of the process=%f",avgtt);
  96. getch();
  97. }
  98.  
Added code from 60 to 66.
Commented line 73.

Let me know if that helps you.
Oct 7 '10 #10

P: 22
thanku thanku so much......yes it is working correctly...
really thanku so much..
Oct 7 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.