I've created a code which demonstrates the First Fit Algorithm using an array of size 256 , the code displays a map showing how the memory is being stored according to the First Fit Algorithm, the code allows the user to delete a memory location too , I would like to create the same concept of my code but using the Worst Fit Algorithm , I've read many articles and understood how the Worst Fit Algorithm works but I can't think of a way so that my code works using the Worst Fit Algorithm , it's easier to use the First Fit than the Worst Fit on my code, could someone show me how to implement my code to use the Worst Fit Algorithm , would be appreciated a lot.
HERE'S THE CODE :
include <iostream>
#include <cmath>
using namespace std;
int PutInMemory(int memory[], int size) {
if (size < 1) cout << "Error!" << endl;
int firstSize = 0;
int j;
for (int i = 0; i < 256; i++) {
if (memory[i] < 0 && abs(memory[i]) >= size) {
j = i;
firstSize += abs(memory[j]);
break;
}
}
if (firstSize < size) {
cout << "Out of Memory";
return 0;
}
if (j + size <= 256) {
memory[j] = size;
for (int i = j + 1; i < j + size; i++)
memory[i] = 0;
int i = j + size;
int count = 0;
while (memory[i] <= -1 && i < 256) {
count++;
i++;
}
if (count != 0) {
memory[i - 1] = -count;
memory[j + size] = -count;
}
return j;
}
else {
cout << "Out of memory";
}
}
void DelSeg(int memory[], int n) {
int count = memory[n];
int prev = 0;
int next = count - 1;
int i = n + 1;
int pos = n;
if (memory[n - 1] < -1 && n != 0) {
i += memory[n - 1];
prev = -memory[n - 1];
count -= memory[n - 1];
pos = i;
}
while (true) {
for (i; i < pos + count - 1; i++) {
memory[i] = -1;
}
if (memory[i + 1] < -1) {
count += -memory[i + 1] + 1;
next = -memory[i + 1];
}
else {
break;
}
}
memory[n - prev] = 0 - count;
memory[n + next] = 0 - count;
}
void checkMemory(int memory[]) {
int countFreeSeg = 0;
int countFullSeg = 0;
int countFullMem = 0;
int countFreeMem = 0;
for (int i = 0; i < 256; i++) {
if (memory[i] < 0) {
if (memory[i] < 0) cout << "Beginning of the adress:" << i << ", ";
int count = 0;
while (memory[i] < 0 && i < 256) {
count++;
i++;
}
countFreeSeg++;
cout << "Size = " << count << endl;
countFreeMem += count;
i--;
}
}
cout << "Number of free processes = " << countFreeSeg << endl << endl;
cout << "Number of free memory = " << countFreeMem << endl << endl;
for (int i = 0; i < 256; i++) {
if (memory[i] > 0) {
cout << "Beginning adress: " << i << ", size - " << memory[i] << endl;
countFullMem += memory[i];
i += memory[i] - 1;
countFullSeg++;
}
}
cout << "Number of occupied processes = " << countFullSeg << endl;
cout << "Number of occupied memory = " << countFullMem << endl;
}
void print(int memory[]) {
for (int i = 0; i < 256; i++) {
cout << memory[i] << " ";
}
}
int main()
{
int memory[256];
memory[0] = -256;
for (int i = 1; i < 256; i++) {
memory[i] = -1;
}
while (true) {
system("cls");
cout << "1.Allocate Memory
2.Free memory segment
3.Get information about the
memory
4.Exit" << endl;
int choice;
cin >> choice;
int m = 0;
switch (choice)
{
case 1:
system("cls");
cout << "Enter the amount of memory to be entered:" << endl;
cin >> m;
cout << PutInMemory(memory, m) << endl;
break;
case 2:
system("cls");
cout << "Enter the starting address of the memory location:" << endl;
cin >> m;
DelSeg(memory, m);
break;
case 3:
checkMemory(memory);
print(memory);
break;
case 4:
system("cls");
exit(0);
break;
default:
cout << "Incorrect entry" << endl;
break;
}
system("pause");
}
}
See Question&Answers more detail:os