-
Notifications
You must be signed in to change notification settings - Fork 0
/
cscan.c
104 lines (85 loc) · 1.79 KB
/
cscan.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
101
102
103
104
#include <stdio.h>
#include<stdlib.h>
int limit,head,numberofsearches;
int list[15];
int index=0,movement=0,temp,choice;
void hightolow()
{
temp = head;
for(int i=index;i>=0;i--)
{
movement = movement + abs(temp - list[i]);
temp = list[i];
}
movement = movement + list[0] + limit;
temp = limit;
for(int i=numberofsearches-1;i>index;i--)
{
movement = movement + abs(temp - list[i]);
temp = list[i];
}
printf("\n\nTotal Head Movement is: \n%d",movement);
}
void lowtohigh()
{
temp = head;
for(int i = index+1;i<numberofsearches;i++)
{
movement = movement + abs(list[i] - temp);
temp = list[i];
}
movement = movement + abs(limit - list[numberofsearches-1]) + limit;
temp = 0;
for(int i = 0;i<=index;i++)
{
movement = movement + abs(list[i] - temp);
temp = list[i];
}
printf("\n\nTotal Head Movement is: \n%d",movement);
}
void main()
{
printf("\nEnter the limit of the Disk:\n");
scanf("%d",&limit);
printf("Enter the current head position:\n");
scanf("%d",&head);
printf("Enter the number of searches that you wish to enter:\n");
scanf("%d",&numberofsearches);
printf("Enter the search sequence in order:\n");
for(int i=0;i<numberofsearches;i++)
{
scanf("%d",&list[i]);
}
for(int i=0;i<numberofsearches-1;i++)
{
for(int j=0;j<numberofsearches-i-1;j++)
{
if(list[j] > list[j+1])
{
int temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
for(int i=0;i<numberofsearches;i++)
{
if(list[i]<head)
{
index = i;
}
}
printf("Enter your choice\n1: High to low\n2: Low to high\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
hightolow();
break;
case 2:
lowtohigh();
break;
default:
exit(0);
}
}