Skip to content

Commit

Permalink
Create Cscan. c
Browse files Browse the repository at this point in the history
  • Loading branch information
annaannmathew authored Oct 31, 2022
1 parent cc08ae1 commit 89befbc
Showing 1 changed file with 189 additions and 0 deletions.
189 changes: 189 additions & 0 deletions c/Cscan. c
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#include<stdio.h>

int main()

{

int i,h,n,sum=0,d,t,j; printf("Enter no.of request:");

scanf("%d",&n);

int r[n];

printf("Enter request sequence:\n");

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

{

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

}

printf("Enter initial head position:");

scanf("%d",&h);

printf("Enter total disk size:");

scanf("%d",&t);

printf("Enter head movement direction(low:0,high:1):");

scanf("%d",&d);

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

{

for(j=0;j<n-i-1;j++)

{

if(r[j]>r[j+1])

{

int temp;

temp=r[j];

r[j]=r[j+1];

r[j+1]=temp;

}

}

}

if(d==0)

{

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

{

if(h<=r[i])

{

j=i;

break;

}

}

for(i=j-1;i>=0;i=i-1)

{

sum=sum+(h-r[i]);

h=r[i];

if(i==0)

{

sum=sum+h;

h=0;

}

}

for(i=n-1;i>=j;i=i-1)

{

if(i==n-1)

{

sum=sum+(t-1);

h=t-1;

}

sum=sum+(h-r[i]);

h=r[i];

}

}

else

{

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

{

if(h<=r[i])

{

j=i;

break;

}

}

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

{

sum=sum+(r[i]-h);

h=r[i];

if(i==(n-1))

{

sum=sum+((t-1)-h);

h=t-1;

}

}

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

{

if(i==0)

{

sum=sum+(t-1);

h=0;

}

sum=sum+(r[i]-h);

h=r[i];

}

}

printf("Total head movement:%d",sum);

return 0;

}

0 comments on commit 89befbc

Please sign in to comment.