#include<stdio.h>
#include<conio.h>
#include<limits.h>
#include<math.h>
#define max 20
#define cymax 199
int i,j,req,ttl_tracks=0,cp,np,cposn;
int cyposn[max],ind[max],temp;
void input()
{
do
{
clreol();
printf("\n Enter the current header position : ");
scanf("%d",&cposn);
}while(cposn>cymax || cposn <=0);
printf("\n Enter the %d I/O Requests : ",req);
cyposn[0] = cposn;
for(i=1;i<=req;i++)
{
scanf("%d",&cyposn[i]);
ind[i] = 0;
}
}
void search_short()
{
int npl,npr,tmpr,tmpl;
if(cp == 0)
{
np = cp;
while(ind[np] == 1 && np < req)
np++;
}
else if(cp == req)
{
np = cp;
while(ind[np] == 1 && np > 0)
np--;
}
else
{
npl = cp;
while(ind[npl] == 1 && npl > 0)
npl--;
if(npl == 0 && ind[npl] == 1)
tmpl = INT_MAX;
else
tmpl = cyposn[npl];
npr = cp;
while(ind[npr] == 1 && npr < req)
npr++;
if(npl == req && ind[npr] == 1)
tmpr = INT_MAX;
else
tmpr = cyposn[npr];
if(abs(cyposn[cp] - tmpl) < abs(cyposn[cp] - tmpr))
np = npl;
else
np = npr;
}
ind[np] = 1;
}
void SSTF()
{
for(i=0;i<=req;i++)
{
for(j=0;j<req-i;j++)
{
if(cyposn[j] > cyposn[j+1])
{
temp = cyposn[j];
cyposn[j] = cyposn[j+1];
cyposn[j+1] = temp;
}
}
}
cp=0;
do
{
if(cyposn[cp] == cposn)
break;
cp++;
}while(cp!=req);
ind[cp] = 1;
printf("\nS.No. Current Position Next Position Displacement \n");
printf("---------------------------------------------------------- \n\n");
i = 0 ;
do
{
search_short();
printf(" %d\t\t%d\t\t%d\t\t%d\t\t%d\n",i+1,cyposn[cp],cyposn[np],abs(cyposn[cp]-cyposn[np]),np);
ttl_tracks += (abs(cyposn[cp]-cyposn[np]));
cp = np;
i++;
}while(i!=req);
printf("---------------------------------------------------------- \n\n");
printf(" Total Tracks Displaced : %d",ttl_tracks);
}
void main()
{
do
{
clrscr();
printf("\n Enter the number of requests : ");
scanf("%d",&req);
}while(req>max || req <=0);
input();
SSTF();
getch();
}
No comments:
Post a Comment