sjf scheduling in c

[b]pls help me debug my code on doing sjf shceduling algo where the buest time and arrival time is asked from the user and form this, the program will display the processes, waiting time of each processes, average waiting time and then ask the user for anther simulation [/b]

[hr]

[code]
#include

#include


main() {


int i, j, n;

char ans;

printf("Input number of process to simulate SJF: ");

scanf("%d",&n);

int burst[n], arrive[n],sjf[n];


printf("Input below the Arrival Time for each process:
");



for(j=0;j<n;j++)

{

printf("P%d: ",j+1);

scanf("%d",&arrive[j]);

}

printf("Input below the burst time for each process:
");



for(i=0;i<n;i++)

{

printf("P%d: ",i+1);

scanf("%d",&burst[i]);



}


//sorting arrival time
int sarrive[n],temp=arrive[0],sParrive[n],sPburst[n];

for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
if(arrive[j]<arrive[i] && arrive[j]<temp){
temp = arrive[j];
sParrive[i]=i+1;
// printf("temp:%d, ",temp);
}
}

sarrive[i]=temp;
printf("sorted arrival:%d, ",sarrive[i]);
}
printf("
");
printf("
");

//sorting burst time from net algo
int sburst[n];
temp=burst[0];
for(i=0;i<n;i++){
for(j=0;j<=n;j++){
if(burst[i]<burst[j]){
temp = burst[i];
sPburst[i]=i+1;
}
}

sburst[i]=temp;

printf("sorted burst:%d, ",sburst[i]);
}

}
/*//my other sorting burst time
int sburst[n],tempt=0,tempo=0;
temp=burst[0];
for(i=0;i<n;i++){
for(j=0;j<=n;j++){
if(burst[i]!=burst[j]){
temp = tempt;
tempt = min(burst[i],burst[j]);
tempo = min(tempt,temp);
}
else{
temp = burst[i];
//temp = min(tempt,temp);
}

}

sburst[i]=tempo;
sPburst[i]=i+1;
printf("sorted burst:%d, Powener:%d
",sburst[i],sPburst[i]);
}


printf("
");
printf("
");
//for the shortest job

int Ptime=0;
for(i=0;i<n;i++)
{
if(sarrive[i]<=sburst[i])
{
sjf[i]=sarrive[i];
printf("%d, ",sjf[i]);
}
else{
sjf[i]=sarrive[i];
printf("%d, ",sjf[i]);
}
Ptime= Ptime + sburst[i];

printf("%d, ",Ptime);
}
}
/*


//for next simulation
if(burst[i+1]==burst[j]){
if(arrive[i+1]<arrive[j]){
temp=arrive[i+1];
}
else{
temp=arrive[j];
}
if(arrive[i+1]==arrive[j]){
temp=arrive[i+1];
}
}


printf("Do you like to continue?(y/n): ");

scanf("%s",&ans);



if(ans == 'y')

{

return main();

}

else

{

return 0;

}
}


[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories