QUESTION DESCRIPTION
Write a program to implement Worst Fit algorithm. Implement this algorithm to allocating the file size and block size in memory.
Input Method:
Enter the number of blocks:3
Enter the number of files:2
Enter the size of the blocks:-
Block 1:5
Block 2:2
Block 3:7
Enter the size of the files :-
File 1:1
File 2:4
Output Method:
Fileno: Filesize : Blockno: Blocksize: Fragment
1 1 1 5 4
2 4 3 7 3
Write a program to implement Worst Fit algorithm. Implement this algorithm to allocating the file size and block size in memory.
Input Method:
Enter the number of blocks:3
Enter the number of files:2
Enter the size of the blocks:-
Block 1:5
Block 2:2
Block 3:7
Enter the size of the files :-
File 1:1
File 2:4
Output Method:
Fileno: Filesize : Blockno: Blocksize: Fragment
1 1 1 5 4
2 4 3 7 3
TEST CASE 1
INPUT
INPUT
5 4 6 5 7 4 8 2 7 4 6
OUTPUTFileno: 1 2 3 4
Filesize: 2 7 4 6
Blockno: 1 3 2 5
Blocksize: 6 7 5 8
Fragment: 4 0 1 2
TEST CASE 2
INPUT
INPUT
5 4 5 2 7 6 3 1 4 3 5
OUTPUTFileno: 1 2 3 4
Filesize: 1 4 3 5
Blockno: 1 3 4 0
Blocksize: 5 7 6 1
Fragment: 4 3 3 -2
Code :
#include <iostream>
using namespace std;
int main() {
int n,m,*arr_bt,*arr_file,*en,*rec,frag;
cin>>n>>m;
arr_bt = new int[n];
arr_file = new int[m];
en = new int[n];
rec = new int[m];
for(int i = 0;i<n;i++){
cin>>arr_bt[i];
}
for(int i = 0;i<m;i++){
cin>>arr_file[i];
}
for(int i = 0;i<m;i++){
rec[i]=-1;
for(int j = 0;j<n;j++){
if(arr_bt[j]>=arr_file[i]&&en[j]!=1){
en[j] = 1;
rec[i] = j;
frag = arr_bt[j] - arr_file[i];
break;
}
}
}
cout<<"Fileno:";
for(int j = 0 ;j<m;j++){
cout<<' '<<j+1;
}
cout<<endl<<"Filesize:";
for(int j = 0;j<m;j++){
cout<<' '<<arr_file[j];
}
cout<<endl<<"Blockno:";
for(int j = 0;j<m;j++){
cout<<' '<<rec[j]+1;
}
cout<<endl<<"Blocksize:";
for(int j = 0;j<m;j++){
if(rec[j]==-1){
cout<<' '<<1;
continue;
}
cout<<' '<<arr_bt[rec[j]];
}
cout<<endl<<"Fragment:";
for(int j = 0;j<m;j++){
if(rec[j]==-1){
cout<<' '<<arr_bt[n-1] - arr_file[j];
continue;
}
cout<<' '<<arr_bt[rec[j]] - arr_file[j];
}
return 0;
}