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

C++. A print queue algorithm assigns printing tasks to 4 printers based on the n

ID: 3889243 • Letter: C

Question

C++. A print queue algorithm assigns printing tasks to 4 printers based on the number of pages to be printed. Printing jobs range from 1-10 pages and come in every 5 seconds. Each printer can print one page per second. Write a program to prioritize print jobs assigning the new job to the printer with the smallest queue. The program should run automatically and must use a timer. The program output should be updated every second showing the size of each print queue and the new job arrival. You must use the queue ADT.

Explanation / Answer

#include<iostream>

using namespace std;

// define the structure Queue (for linked list representation of a queue)

struct Queue

{

// define a integer value to store the data in the queue.

int data;

// a pointer to store the address of the next data in the queue.

Queue *nextPtr;

}

//declare pointer of the structure.

*start = NULL, *rear = NULL, *ptr = NULL, *np = NULL;

// define a function that takes number of pages to print

// and calculates the time taken

void printQueue(int numOfPages)

{

float time = 0;

//small jobs (less than 10 pages)

if (numOfPages >= 0 && numOfPages <= 10)

{

cout << " The task can be done by printer 1" << endl;

//Each printer can print 7 pages per minute.

time = (float)(numOfPages) / 7;

}

//medium jobs (11-25 pages)

if (numOfPages > 10 && numOfPages <= 25)

{

cout << " The task can be done by printer 2" << endl;

time = (float)(numOfPages) / 7;

}

// large jobs (26-50 pages)

if (numOfPages > 25 && numOfPages <= 50)

{

cout << " The task can be done by printer 3" << endl;

time = (float)(numOfPages) / 7;

}

//very large jobs (51 pages and up).

if (numOfPages > 50)

{

cout << " The task can be done by printer 4" << endl;

time = (float)(numOfPages) / 7;

}

cout << " The time required to print " << numOfPages << " pages is: " << time << " seconds" << endl;

}

// define a funtion to keep into the queue.

void push(int value)

{

// create an object for the Queue.

np = new Queue;

// The value is stored as the data of address 'np'

np->data = value;

// the nextPtr of the data pointer to null, because there is no other data.

np->nextPtr = NULL;

// if the start of the queue is null,

if (start == NULL)

{

// then, the data is the first node of the quue.

start = rear = np;

// next part of the first node is NULL

rear->nextPtr = NULL;

}

else // if the start pointer is not NULL

{

//Puts the current node's address in next part of previous node

rear->nextPtr = np;

// makes the current node as np

rear = np;

// the next part of current node NULL

rear->nextPtr = NULL;

}

}// end.

// define a function to remove from the queue.

void remove()

{

int value;

if (start == NULL) // if front is NULL queue is empty

{

cout << "No tasks are there in the queue ";

}

else // otherwise

{

ptr = start; //Assigns start to ptr

value = ptr->data; // gets data of ptr into value

start = start->nextPtr; // the next node's address into start

// call to printQueue() function

printQueue(value);

}

}

int main()

{

int tasks, i = 0, pages;

// read the number of tasks as input

cout << "Enter the number of tasks: ";

cin >> tasks;

while (i < tasks) // loop for taking number of pages in each task

{

cout << "Enter the number of pages in task " << i + 1 << ": ";

cin >> pages;

// call push function to inserts task into the queue

push(pages);

i++;

}

while (true)

{

// if the queue is not empty

if (start != NULL)

{

// call to remove() function

remove();

cout << endl;

}

else

// exit from while loop

break;

}

system("pause");

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote