Question
Use the following main function for your program. You may not be familiar with this function signature. main can take 2 optional parameters: traditionally called
Use the following main function for your program. You may not be familiar with this function signature. main can take 2 optional parameters: traditionally called argc and argv which stand for argument count and argument vector respectively. However, theyre just parameters so you can technically call them anything you want (but stick with the traditional names since many languages use these names too). I should point out that although argv stand for argument vector it is not a C++ vector object. argv is just an array of c-strings.
int main (int argc, char* argv[]) { return 0; }
This allows you to pass command line arguments to your program when you execute it. Youre already familiar with this from when you compile your code.
g++ -Wall -Wextra -pedantic -std=c++17 main.cpp
The command above has 6 command line arguments. They are:
g++
-Wall
-Wextra
-pedantic
-std=c++17
main.cpp
The program will always receive the path to itself as its first argument. So argc will always be at least 1.
Multilevel Queues
A multilevel queue is simple an array of queues. Each of these queues can use any scheduling algorithm; however, for this assignment simple FIFO queues will be sufficient.
Each queue in the multilevel queue represents a different priority level. For example, we could have a multilevel queue with 3 levels: admins, faculty, students. Admins have the highest priority, faculty come second and students have the lowest priority. When you add an element to the multilevel queue you must include its priority level so that it will be added to the appropriate queue. A multilevel queues dequeue method should return the element from the highest priority level first.
Step 1: Create a Queue Class
Create a Queue class that implements a FIFO queue. The queue will hold std::string elements (You may want to create a Node class).
It should have an enqueue and dequeue method.
void enqueue (std::string data); std::string dequeue ();
You may not use any data structures from the standard library. This queue must be dynamic.
Step 2: Create the multilevel queue class
Create a MultilevelQueue class. The constructor should take the number of priorities as its only parameter. The MultilevelQueue should use a std::vector to hold each of the Queue for each priority level. For example, your class could be constructed like the example below.
MultilevelQueue jobQueue = MultilevelQueue{5};
Add an enqueue method that will take the priority level of the job and the jobs name. Add the jobName to the appropriate queue. If the priority level is out of bounds of the vector then throw a std::out_of_range exception.
void enqueue (int priority, std::string jobName);
Add an dequeue method that will return the job with the highest priority level. If all of the queues are empty then throw a std::length_error exception.
std::string dequeue ();
Step 3: Taking arguments
Create a main function that can take command line arguments. It must take exactly 2 arguments (remember that the first will always be the path to the program itself). The second argument will be the number of elements in the multilevel queue. If there are not exactly 2 arguments then you should output an error message and terminate. You can use the atoi() function to convert the string to an integer. Use this value to initialize the multilevel queue.
# Compile the code g++ -Wall -Wextra -pedantic -std=c++17 *.cpp # Then execute the code with by passing a command line argument # this will create a queue with 3 levels ./a.out 3 # This should result in an error message and the program should terminate # since there is only 1 argument (the program name) ./a.out # This should also result in an error message and terminate # since there are more than 2 arguments ./a.out 3 1
Step 4: Testing your code
Add code to programmatically test your multilevel queue. Do not prompt for input.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started