#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
struct process
{
int size;
int hole;
}*p;
int *h,*tmp,hn,pn,i,j,k,rem,lh,sh;
//rem=0;
void input()
{
clrscr();
printf("Enter no of holes : ");
fflush(stdin);
scanf("%d",&hn);
h=(int *)malloc(sizeof(int)*hn);
for(i=0;i<hn;i++)
{
printf("size of hole %d : ",i+1);
fflush(stdin);
scanf("%d",&h[i]);
tmp[i]=h[i];
}
printf("Enter no of processes : ");
fflush(stdin);
scanf("%d",&pn);
p=(struct process *)malloc(sizeof(struct process)*pn);
for(i=0;i<pn;i++)
{
printf("size of process %d : ",i+1);
fflush(stdin);
scanf("%d",&p[i].size);
p[i].hole=0;
}
}
void ff()
{
tmp=(int *)malloc(sizeof(int)*hn);
for(i=0;i<hn;i++)
{
tmp[i]=h[i];
}
for(i=0;i<pn;i++)
{
for(j=0;j<hn;j++)
{
if(p[i].size<=tmp[j])
{
tmp[j]-=p[i].size;
p[i].hole=j+1;
break;
}
}
}
rem=0;
lh=tmp[0];
sh=tmp[0];
for(i=0;i<hn;i++)
{
rem+=tmp[i];
if(tmp[i]<sh)
sh=tmp[i];
if(tmp[i]>lh)
lh=tmp[i];
}
}
void nf()
{
int t=0;
tmp=(int *)malloc(sizeof(int)*hn);
for(i=0;i<hn;i++)
{
tmp[i]=h[i];
}
for(i=0;i<pn;i++)
{
for(j=t;j<hn;j++)
{
if(p[i].size<=tmp[j])
{
tmp[j]-=p[i].size;
p[i].hole=j+1;
t=j;
break;
}
}
}
rem=0;
lh=tmp[0];
sh=tmp[0];
for(i=0;i<hn;i++)
{
rem+=tmp[i];
if(tmp[i]<sh)
sh=tmp[i];
if(tmp[i]>lh)
lh=tmp[i];
}
}
void bf()
{
int *diff,t1,t2,flag=0;
diff=(int *)malloc(sizeof(int)*hn);
tmp=(int *)malloc(sizeof(int)*hn);
for(i=0;i<hn;i++)
{
tmp[i]=h[i];
}
for(i=0;i<pn;i++)
{
flag=0;
for(j=0;j<hn;j++)
{
diff[j]=tmp[j]-p[i].size;
}
for(j=0;j<hn;j++)
{
if(diff[j]>=0)
{
t1=diff[j];
t2=j;
flag++;
break;
}
}
for(j=0;j<hn;j++)
{
if(diff[j]>=0 && diff[j]<t1)
{
t1=diff[j];
t2=j;
flag++;
}
}
if(flag)
{
tmp[t2]-=p[i].size;
p[i].hole=t2+1;
}
}
rem=0;
lh=tmp[0];
sh=tmp[0];
for(i=0;i<hn;i++)
{
rem+=tmp[i];
if(tmp[i]<sh)
sh=tmp[i];
if(tmp[i]>lh)
lh=tmp[i];
}
}
void wf()
{
int *diff,t1,t2,flag=0;
diff=(int *)malloc(sizeof(int)*hn);
tmp=(int *)malloc(sizeof(int)*hn);
for(i=0;i<hn;i++)
{
tmp[i]=h[i];
}
for(i=0;i<pn;i++)
{
flag=0;
for(j=0;j<hn;j++)
{
diff[j]=tmp[j]-p[i].size;
}
for(j=0;j<hn;j++)
{
if(diff[j]>=0)
{
t1=diff[j];
t2=j;
flag++;
break;
}
}
for(j=0;j<hn;j++)
{
if(diff[j]>=0 && diff[j]>t1)
{
t1=diff[j];
t2=j;
flag++;
}
}
if(flag)
{
tmp[t2]-=p[i].size;
p[i].hole=t2+1;
}
}
rem=0;
lh=tmp[0];
sh=tmp[0];
for(i=0;i<hn;i++)
{
rem+=tmp[i];
if(tmp[i]<sh)
sh=tmp[i];
if(tmp[i]>lh)
lh=tmp[i];
}
}
void result()
{
// clrscr();
printf("Process\tHole");
for(i=0;i<pn;i++)
{
if(p[i].hole>0)
printf("\n%d\t%d",i+1,p[i].hole);
else
printf("\n%d\tcant fit",i+1);
}
printf("\n\nHole status ");
printf("\n\nHole no\tHole size\n");
for(i=0;i<hn;i++)
{
printf("\n%d\t%d",i+1,tmp[i]);
}
printf("\n\nRemaining hole --> %d",rem);
printf("\nSmallest hole --> %d",sh);
printf("\nLargest hole --> %d",lh);
}
void main()
{
int ch;
input();
st: printf("\ndifferent schemes ");
printf("\n1. First fit\n2. Next fit\n3. Best fit\n4. Worst fit\n5. exit");
printf("\n\nEnter your choice : ");
fflush(stdin);
scanf("%d",&ch);
switch(ch)
{
case 1: ff();
break;
case 2: nf();
break;
case 3: bf();
break;
case 4: wf();
break;
case 5: exit(1);
break;
default:printf("\ninvalid choice ..");
printf("\npress any key to continue ....");
getch();
goto st;
}
result();
printf("\npress any key to continue ....");
getch();
goto st;
}