code is not working properly for priorities .what changes should i do?? failing if priority of 1st process is less
Code:
#include<stdio.h>
int wt=0;
float sum=0;
int main()
{
int pr[5];
int wait[5];
int bt[5],tat[5];
int n,prt,brt,i,j,temp;
printf("enter the no. of processes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the burst time for %d th process",(i+1));
scanf("%d",&brt);
bt[i]=brt;
}
for(i=0;i<n;i++)
{
printf("\n Burst time of[%d] process:%d",(i+1),bt[i]);
}
//priority
for(i=0;i<n;i++)
{
printf("enter the priority for %dth processes::",(i+1));
scanf("%d",&prt);
pr[i]=prt;
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(pr[j]>pr[j+1])
{
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(pr[j]>pr[j+1])
if(pr[j]>pr[j+1])
{
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(pr[j]>pr[j+1])
{
temp=wait[j];
wait[j]=wait[j+1];
wait[j+1]=temp;
}
}
}
//waiting time loop
for(i=0;i<n;i++)
{
wait[i]=wt;
printf("\nwaiting time of [%d] is ::%d",(i+1),wait[i]);
wt=wt+bt[i];
}
//calculate sum for avg
for(i=0;i<n;i++)
{
sum=sum+wait[i];
}
sum=sum/n;
printf("average waiting time=%d",sum);
for(i=0;i<n;i++)
{
tat[i]=wait[i]+bt[i];
printf("\nturn around time of %dth process:%d",(i+1),tat[i]);
}
return 0;
}