Skip to content

Commit

Permalink
Merge pull request rjkalash#588 from Saranya2110/main
Browse files Browse the repository at this point in the history
CSCAN Algorithm
  • Loading branch information
rjkalash authored Oct 3, 2021
2 parents fad285d + 86613e9 commit 98f2124
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions cscan algorithm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void scanright(int a[20],int n,int loc)
{
int count=0,i,diff,flag;
float avg;
for(i=loc;i<n;i++)
{
diff=abs(a[i+1]-a[i]);
count+=diff;
}
flag=i;

for(i=loc-1;i>=0;i--)
{
diff=abs(a[flag]-a[i]);
count+=diff;
flag=i;
}

printf("\nSeek sequence is:");
for(i=loc+1;i<=n;i++)
printf("%d\t",a[i]);
for(i=loc-1;i>=0;i--)
printf("%d\t",a[i]);
avg=(float)count/n;
printf("\nTotal Seek time:%d",count);
printf("\nAverage Seek time:%.2f",avg);
}

void scanleft(int a[20],int n,int loc)
{
int count=0,i,diff,flag;
float avg;
for(i=loc;i>0;i--)
{
diff=abs(a[i]-a[i-1]);
count+=diff;
}
flag=i;
for(i=loc+1;i<=n;i++)
{
diff=abs(a[i]-a[flag]);
count+=diff;
flag=i;
}
printf("\nSeek sequence is:");
for(i=loc-1;i>=0;i--)
printf("%d\t",a[i]);
for(i=loc+1;i<=n;i++)
printf("%d\t",a[i]);
avg=(float)count/n;
printf("\nTotal Seek time:%d",count);
printf("\nAverage Seek time:%.2f",avg);
}
void main()
{
int a[20],i,j,temp,n,head,loc;
char dir[10];
printf("\nEnter initial head position:");
scanf("%d",&head);
printf("\nEnter request count & direction");
scanf("%d%s",&n,dir);
for(i=1;i<=n;i++)
{
printf("\nEnter request value %d:",i);
scanf("%d",&a[i]);
}
a[0]=head;
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
if(a[i]==head)
{
loc=i;
break;
}
}
if(strcmp(dir,"left")==0)
{
scanleft(a,n,loc);
}
else
{
scanright(a,n,loc);
}
}

0 comments on commit 98f2124

Please sign in to comment.