Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I already did the code for this lab. It\'s attached below. But I have to add the

ID: 3753700 • Letter: I

Question

I already did the code for this lab. It's attached below. But I have to add the total waste. How do I do that?

#include <iostream>

#include <string>

using namespace std;

int SizeOfMem(int memSize);

int MemPartitions(int numOfPart);

void SizeOfPartition(int*& sizeOfPart, int& memLeft, int& memSize, int& numOfPart);

void JobInfo(int*& jobSize, string*& jobName, int numOfPart);

void FirstFit(string*& jobName, int*& jobSize, int*& sizeOfPart, int numOfPart);

int main()

{

int memSize = 0, numOfPart = 0, memLeft; //Total size of memory, number of partitions, memory left after each partition is declared

memSize = SizeOfMem(memSize); //allows user to input the total size of memory

memLeft = memSize;

numOfPart = MemPartitions(numOfPart); //allows user to input the number of partitions needed

string *jobName = new string[numOfPart]; //name of jobs dynamic array

int *sizeOfPart = new int[numOfPart]; //size of each partition dyn array

int *jobSize = new int[numOfPart]; //size of each job dyn array

SizeOfPartition(sizeOfPart, memLeft, memSize, numOfPart); //allows user to input size of each partition

JobInfo(jobSize, jobName, numOfPart); //allows user to input information regarding each job

FirstFit(jobName, jobSize, sizeOfPart, numOfPart); //first fit method executed

delete[] sizeOfPart;

delete[] jobName;

delete[] jobSize;

}

int SizeOfMem(int memSize) //prompts memory size

{

cout << "Input the total size of memory: ";

cin >> memSize;

return memSize;

}

int MemPartitions(int numOfPart) //prompts number of partitions

{

cout << "Input the number of memory partitions required (maximum of 5): ";

cin >> numOfPart;

if (numOfPart > 5)

{

cout << "Error! Max num of partitions allowed is 5." << endl << endl;

MemPartitions(numOfPart);

}

else

return numOfPart;

}

void SizeOfPartition(int*& sizeOfPart, int& memLeft, int& memSize, int& numOfPart) //prompts size of each partition, dispays total memory left

{

for (int i = 0; i < numOfPart; i++)

{

cout << " Enter the size of partition " << i + 1 << ": ";

cin >> sizeOfPart[i];

memLeft = memLeft - sizeOfPart[i];

if (memLeft < 0)

{

cout << "Error! Size of partitions must add up to " << memSize << endl << endl;

main();

}

else if (memLeft == 0)

{

cout << "Size of partition " << i + 1 << " is " << sizeOfPart[i] << endl;

cout << "Memory left: " << memLeft << endl << endl;

for (int j = i + 1; j < numOfPart; j++)

{

sizeOfPart[j] = 0;

cout << "Size of partition " << j + 1 << " is " << sizeOfPart[j] << endl;

cout << "Memory left: " << memLeft << endl << endl;

}

break;

}

cout << "Size of partition " << i + 1 << " is " << sizeOfPart[i] << endl;

cout << "Memory left: " << memLeft << endl;

}

}

void JobInfo(int*& jobSize, string*& jobName, int numOfPart) //prompts job information

{

for (int i = 0; i < numOfPart; i++)

{

cout << " Enter name of job " << i + 1 << " : ";

cin >> jobName[i];

cout << "Enter size of job " << i + 1 << " : ";

cin >> jobSize[i];

cout << endl;

}

}

void FirstFit(string*& jobName, int*& jobSize, int*& sizeOfPart, int numOfPart) //executes first fit method

{

bool status; //status of each job (if Run or Wait)

bool *fullPart = new bool [numOfPart]; //status of each partition (empty or not)

for (int k = 0; k < numOfPart; k++)

fullPart[k] = false;

for (int i = 0; i < numOfPart; i++)

{

status = false;

for (int j = 0; j < numOfPart; j++)

{

if (jobSize[i] <= sizeOfPart[j] && fullPart[j] == false) //allocate job# to partition# if it fits and the partition is empty

{

cout << jobName[i] << " (Size: " << jobSize[i] << ")" << " is allocated to partition " << j + 1 << " (Size: " << sizeOfPart[j] << ")" << endl;

cout << "Status: Run" << endl;

cout << "Memory waste is: " << sizeOfPart[j] - jobSize[i] << endl << endl;

fullPart[j] = true;

status = true;

break;

}

}

if (status != true) //if job has to wait, display wait

cout << jobName[i] << " (Size: " << jobSize[i] << ")" << " is not allocated Status: Wait" << endl << endl;

}

delete[] fullPart;

}

Objectives: Learn how First-Fit algorithm work in memory management. - Learn how to implement First-Fit algorithm in C++. Description: Write a C++ program that will implement First-fit, memory management algorithm. Your program must do the following: Input the memory size and the number and sizes of all the partitions (limit the max number of the partitions to 5). Input the job list that includes: 1. 2. Job's name Job's size For each job you should create in your program a data structure that will include the job status (Run/Wait) and the partition number (if the job was allocated). 3. 4. Calculate and display initial memory allocation. 5. Display the memory waste and the jobs that could not be allocated and have to wait. Code Quality Requirement: 1. Do not put every single logic in the main() function 2. You must use functions to separate your program's logic into smaller readable pieces. 3. You must use descriptive variable names. Don't use int b 5. What's b?! It's okay to use i or j or any other letters for loops (only loops) 4. You must write meaningful comments to explain your code.

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;

// Prototype of functions
int SizeOfMem(int memSize);
int MemPartitions(int numOfPart);
void SizeOfPartition(int*& sizeOfPart, int& memLeft, int& memSize, int& numOfPart);
void JobInfo(int*& jobSize, string*& jobName, int numOfPart);
void FirstFit(string*& jobName, int*& jobSize, int*& sizeOfPart, int numOfPart, int memLeft);

// main function definition
int main()
{
// Total size of memory, number of partitions, memory left after each partition is declared
int memSize = 0, numOfPart = 0, memLeft;
// allows user to input the total size of memory
memSize = SizeOfMem(memSize);
// Initially memory left is equals to memory size
memLeft = memSize;
// allows user to input the number of partitions needed
numOfPart = MemPartitions(numOfPart);
// name of jobs dynamic array
string *jobName = new string[numOfPart];
//size of each partition dyn array
int *sizeOfPart = new int[numOfPart];
// size of each job dyn array
int *jobSize = new int[numOfPart];
// allows user to input size of each partition
SizeOfPartition(sizeOfPart, memLeft, memSize, numOfPart);
// allows user to input information regarding each job
JobInfo(jobSize, jobName, numOfPart);
// first fit method executed
FirstFit(jobName, jobSize, sizeOfPart, numOfPart, memLeft);

// Delete the pointers
delete[] sizeOfPart;
delete[] jobName;
delete[] jobSize;
}// End of main function

// Function to accept memory size and returns it
int SizeOfMem(int memSize)
{
cout << "Input the total size of memory: ";
cin >> memSize;
return memSize;
}// End of function

// Function to accept number of partition and returns it
int MemPartitions(int numOfPart)
{
// Accepts number of partition
cout << "Input the number of memory partitions required (maximum of 5): ";
cin >> numOfPart;

// Checks if number of partition entered by the user is more than 5
// Display error message and call the function again
if (numOfPart > 5)
{
cout << "Error! Max num of partitions allowed is 5." << endl << endl;
// Calls the function again
MemPartitions(numOfPart);
}// End of if condition
// Otherwise partition is less than or equals to 5
else
// Returns the partition
return numOfPart;
}// End of function

// Function to accept size of each partition, displays total memory left
void SizeOfPartition(int*& sizeOfPart, int& memLeft, int& memSize, int& numOfPart)
{
// Loops till number of partition
for (int i = 0; i < numOfPart; i++)
{
// Accepts the each partition size
cout << " Enter the size of partition " << i + 1 << ": ";
cin >> sizeOfPart[i];
// Calculates memory left
memLeft = memLeft - sizeOfPart[i];

// Checks if memory left is less than 0 display error message
if (memLeft < 0)
{
cout << "Error! Size of partitions must add up to " << memSize << endl << endl;
main();
}// End of if condition

// Otherwise checks if memory left is zero
else if (memLeft == 0)
{
// Display each partition number, partition size and memory left
cout << "Size of partition " << i + 1 << " is " << sizeOfPart[i] << endl;
cout << "Memory left: " << memLeft << endl << endl;

// Loops from outer loop variable value to number 0f partitions
for (int j = i + 1; j < numOfPart; j++)
{
// Sets the size to zero
sizeOfPart[j] = 0;
// Display each partition number, partition size and memory left
cout << "Size of partition " << j + 1 << " is " << sizeOfPart[j] << endl;
cout << "Memory left: " << memLeft << endl << endl;
}// End of for loop
// Come out of the loop
break;
}// End of else if condition

// Display each partition number, partition size and memory left
cout << "Size of partition " << i + 1 << " is " << sizeOfPart[i] << endl;
cout << "Memory left: " << memLeft << endl;
}// End of outer for loop
}// End of function

// Function to accept job name, size of each job
void JobInfo(int*& jobSize, string*& jobName, int numOfPart)
{
// Loops till number of partitions
for (int i = 0; i < numOfPart; i++)
{
// Accepts job name
cout << " Enter name of job " << i + 1 << " : ";
cin >> jobName[i];
// Accepts job size
cout << "Enter size of job " << i + 1 << " : ";
cin >> jobSize[i];
cout << endl;
}// End of for loop
}// End of function

// Function to apply first fit algorithm
void FirstFit(string*& jobName, int*& jobSize, int*& sizeOfPart, int numOfPart, int memLeft)
{
// To store total waste
int totalWaste = 0;
// Status of each job (if Run or Wait)
bool status;
// Status of each partition (empty or not)
bool *fullPart = new bool [numOfPart];

// Loops till number of partitions
for (int k = 0; k < numOfPart; k++)
// Sets each index position to false for full part
fullPart[k] = false;

// Loops till number of partitions
for (int i = 0; i < numOfPart; i++)
{
// Sets the job allocation status to false (wait)
status = false;
// Loops till number of partitions
for (int j = 0; j < numOfPart; j++)
{
// Allocate job# to partition# if it fits and the partition is empty
// Checks if current job size is less than or equals to current partition size
if (jobSize[i] <= sizeOfPart[j] && fullPart[j] == false)
{
// Displays job name, job size, allocates partition number, size of partition
cout << jobName[i] << " (Size: " << jobSize[i] << ")" << " is allocated to partition "
<< j + 1 << " (Size: " << sizeOfPart[j] << ")" << endl;
cout << "Status: Run" << endl;
// Displays the memory wasted after deducting current job size from current partition
cout << "Memory waste is: " << sizeOfPart[j] - jobSize[i] << endl << endl;
// Calculates total waste by adding waste
totalWaste += sizeOfPart[j] - jobSize[i];
// Sets the full part for the current partition to true
fullPart[j] = true;
// Sets the status of the process to true
status = true;
// Come out of the loop
break;
}// End of if condition
}// End of inner for loop

// Checks if job has to wait, display wait
if (status != true)
// Displays job name, job size, and waiting
cout << jobName[i] << " (Size: " << jobSize[i] << ")" << " is not allocated Status: Wait" << endl << endl;
}// End of outer for loop
cout<<" Total memory left after partition: "<<memLeft;
cout<<" Memory waste after job allocation: "<<totalWaste;
cout<<" Total memory waste: "<<memLeft + totalWaste;
delete[] fullPart;
}// End of function

Sample Output:

Input the total size of memory: 50
Input the number of memory partitions required (maximum of 5): 4

Enter the size of partition 1: 20
Size of partition 1 is 20
Memory left: 30

Enter the size of partition 2: 10
Size of partition 2 is 10
Memory left: 20

Enter the size of partition 3: 5
Size of partition 3 is 5
Memory left: 15

Enter the size of partition 4: 10
Size of partition 4 is 10
Memory left: 5

Enter name of job 1 : Prime
Enter size of job 1 : 8


Enter name of job 2 : Factor
Enter size of job 2 : 9


Enter name of job 3 : Fibo
Enter size of job 3 : 8


Enter name of job 4 : Even
Enter size of job 4 : 4

Prime (Size: 8) is allocated to partition 1 (Size: 20)
Status: Run
Memory waste is: 12

Factor (Size: 9) is allocated to partition 2 (Size: 10)
Status: Run
Memory waste is: 1

Fibo (Size: 8) is allocated to partition 4 (Size: 10)
Status: Run
Memory waste is: 2

Even (Size: 4) is allocated to partition 3 (Size: 5)
Status: Run
Memory waste is: 1


Total memory left after partition: 5
Memory waste after job allocation: 16
Total memory waste: 21