QUESTION DESCRIPTION
Write a C program for implementing the Best Fit algorithm in memory partitioning. The Best Fit Memory Allocation Algorithm allocates the smallest free partition available in the memory that is sufficient enough to hold the process within the system.
Write a C program for implementing the Best Fit algorithm in memory partitioning. The Best Fit Memory Allocation Algorithm allocates the smallest free partition available in the memory that is sufficient enough to hold the process within the system.
TEST CASE 1
INPUT
INPUT
3
2
5
2
7
1
4
OUTPUTFileno: 1 2
Filesize: 1 4
Blockno: 2 1
Blocksize: 2 5
Fragment: 1 1
TEST CASE 2
INPUT
INPUT
6
6
7
8
9
6
2
5
4
3
6
5
7
8
OUTPUTFileno: 1 2 3 4 5
Filesize: 4 3 6 5 7
Blockno: 6 4 1 2 3
Blocksize: 5 6 7 8 9
Fragment: 1 3 1 3 2
Code :
#include <iostream>
using namespace std;
int main() {
int n,m , *arr_bt , *arr_file , temp , sub , *alloc , *copy;
cin>>n>>m;
arr_bt = new int[n];
arr_file = new int[m];
alloc = new int[m];
copy = new int[n];
for(int i = 0;i<n;i++){
cin>>arr_bt[i];
copy[i] = arr_bt[i];
}
for(int i = 0;i<m;i++){
cin>>arr_file[i];
}
cout<<"Fileno:";
for(int i = 0;i<m;i++){
sub = 100;
temp=-1;
for(int j = 0; j<n; j++){
if(arr_bt[j] >= arr_file[i]){
if(arr_bt[j] - arr_file[i] <= sub){
sub = arr_bt[j] - arr_file[i];
temp = j;
}
}
}
if(sub != 100)
arr_bt[temp] = sub;
alloc[i] = temp+1;
}
for(int j = 0;j<m;j++){
if(alloc[j] != 0)
cout<<' '<<j+1;
}
cout<<endl<<"Filesize:";
for(int j = 0;j<m;j++){
if(alloc[j]!=0)
cout<<' '<<arr_file[j];
}
cout<<endl<<"Blockno:";
for(int j = 0;j<m;j++){
if(alloc[j]!=0)
cout<<' '<<alloc[j];
}
cout<<endl<<"Blocksize:";
for(int j = 0;j<m;j++){
if(alloc[j]!=0)
cout<<' '<<copy[alloc[j]-1];
}
cout<<endl<<"Fragment:";
for(int j = 0;j<m;j++){
if(alloc[j]!=0)
cout<<' '<<arr_bt[alloc[j]-1];
}
return 0;
}