Tuesday, August 31, 2010

SSTF : in os subject

#include<stdio.h>
#include<conio.h>
#include<math.h>


void
main()
{

clrscr();
int
n;
int
arr[100];

int
curr_pos = 0;
printf("Enter no of numbers; ");
scanf("%d", &n);

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


printf("Enter no ", i + 1, ": ");
scanf("%d", &arr[i]);
}


printf("\nEnter current position: ");
scanf("%d", &curr_pos);
int
temp_n = n;

int
total = 0;
for
(int j = 0; j < temp_n; j++)
{


int
diff = 0;
int
k = 0;

int
temp_k = 0;
// if(n>1){
for (k = 0; k < n; k++)
{


int
temp = abs(curr_pos - arr[k]);

if
(k == 0)
{

diff = abs(curr_pos - arr[0]);
}


if
(temp <= diff)
{

temp_k = k;

diff = temp;
// printf("\ntemp = %d",temp);
}
}

k = temp_k;

printf("\n%6d - %6d = %6d", curr_pos, arr[temp_k], abs(arr[temp_k] - curr_pos));

total = total + abs(arr[temp_k] - curr_pos);

curr_pos = arr[temp_k];
for
(int l = temp_k; l < n; l++)
{


arr[l] = arr[l + 1];
}

n--;
}


printf("\n\n\Total is : %d", total);

getch();
}

No comments:

Post a Comment