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

C++ Program Using Heaps and Stacks to Manage A Printer Queue *******************

ID: 3798392 • Letter: C

Question

C++ Program Using Heaps and Stacks to Manage A Printer Queue

******************************************************************************

Rules of Priority for Printer Queue

student jobs are done first in, first out. But all TA print jobs are done before any student print job, with the TA's print jobs being done first in, first out. And All instructor print jobs were done before any TA print job, with the instructor's print jobs being done first in, first out.

What Needs to Be Done

Write program to manage the printer queue. you must use Heaps and Priority Queues.

The program will display a menu and prompt the user to enter a choice:

Printer queue
=========
1. Add job
2. Print job
3 View jobs
4. Exit
Enter choice:

You may assume the user enters 1, 2, 3 or 4. No input validation is required.

If the user chooses 4. Exit, then the program ends. For Othe CHoices see below guide.

Add Job

If the user chooses Add job, then the program asks the user who is requesting the job:

Instructor (I or i)
TA (T or t)
Student (S or s)

You may assume that the the user enters I, i, T, t, S or s. No input validation is necessary.

The program then adds the print job to the priority queue. The data added reflects who the job is for (instructor, TA or student) and the number of the print job (an incrementing number, starting with 1, then 2, 3, 4, etc.).

Print Job

If the user chooses Print job, if there are any print jobs in the queue, then the program outputs, for the highest priority print job, who the job is for (instructor, TA or student) and the number of the print job. That print job also is removed from the priority queue. However, if there are no print jobs in the queue, then the program outputs: "No print jobs in queue."

View Jobs

If the user chooses Print job, then the program outputs, for each print job in the queue, and in order of priority, who the job is for (instructor, TA or student) and the number of the print job. However, if there are no print jobs in the queue, then the program outputs: "No print jobs in queue."

Number of Jobs

You could use a vector and just allocate elements dynamically. But if you want to use an array, you can choose a large number so you don't run out of job numbers.

Thanks in advance for your help with this.

Explanation / Answer

#include <stdio.h>
#define MAXSIZE 5

struct jobqueue{
int jobname;
int priority;
} job [MAXSIZE];

int top = -1;

/* Function declaration/Prototype*/
void add(void);
int print(void);
void view (void);

int main ()
{
   int choice;
   int option = 1;
   top = -1;
   printf ("Printer Queue Operation ");
   while (option)
   {
   printf ("------------------------------------------ ");
   printf (" 1 --> Add Job ");
   printf (" 2 --> Print Job ");
   printf (" 3 --> View Jobs ");
   printf (" 4 --> EXIT ");
   printf ("------------------------------------------ ");
   printf ("Enter your choice ");
   scanf ("%d", &choice);
   switch (choice)
   {
   case 1: add();
   break;
   case 2: print();
   break;
   case 3: view();
   break;
   case 4: return 0;
   }

   printf ("Do you want to continue(Type 0 or 1)? ");
   scanf ("%d", &option);
   }
}

/*Function to add an element to the stack*/
void add ()
{
   int num;
   int user;
   struct jobqueue element;
   if (job[MAXSIZE].jobname > 0)
   {
   printf ("Queue is Full ");
   return;
   }else{
   printf ("Enter the job to be added ");
   scanf ("%d", &num);
   element.jobname = num;
  
   printf ("who is requesting the job. Values must from the followings ");
   printf("1 : Instructor ");
   printf("2 : TA ");
   printf("3 : Student ");
   scanf ("%d", &user);
  
   if(user == 1){
   element.priority = 1;
   }else if(user == 2){
   element.priority = 2;
   }else if( user == 3){
   element.priority = 3;
   }
  
int indx = 0;
  
   if(top == -1){
   top = top + 1;
       printf("---------**** %d",top);
       printf("AAAA %d",job[top].jobname);
           job[top] = element;
           printf("BBBB %d",job[top].jobname);
           top = top +1;
       }else{
       printf("else case entered**** %d",top);
       if(job[indx].priority < element.priority){
       for( indx = top; indx <= MAXSIZE ; indx++){
       int indexb = indx ;
   struct jobqueue tempJob;
   for( ; indexb < top ; indexb++){
   printf(" Queue eleemnt %d",job[indexb].jobname);
       tempJob = job[indexb];
       job[indexb] = element;
   }
   job[indexb] = tempJob;
           break;
       }
   }else{
job[top] = element;
top++;
}
}
   view();
   }
   //return 0;
}

/*Function to delete an element from the stack*/
int print()
{
   /*int num;
   if (s.top == - 1)
   {
   printf ("Stack is Empty ");
   return (s.top);
   }
   else
   {
       num = s.stk[0];
       printf ("poped element is = %d ", num);
       int j = 0;
       for(j = 0 ; j < s.top ; j++){
           s.stk[j] = s.stk[j+1];
       }
       s.top = s.top - 1;
   } */
   return(0);
}

/*Function to display the status of the stack*/
void view()
{
   int i;
   if (top == -1)
   {
   printf ("Queue is empty ");
   return;
   }
   else
   {
   printf (" The Jobs status of the Queue is ");
   for (i = 0; i <= top; i++)
   {
   printf ("%d ", job[i].jobname);
   }
   }
   printf (" ");
}

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