forked from rjkalash/hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcscan algorithm.c
101 lines (96 loc) · 1.53 KB
/
cscan algorithm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#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);
}
}