Help! Please edit my code in C++ and include the output screenshot! Prompt the u
ID: 3835266 • Letter: H
Question
Help! Please edit my code in C++ and include the output screenshot!
Prompt the user to enter elements into the que. Remove those elements and then prompt the user if they would like to try again.
The sample output needs to come out like this
Here is my code:
#include <iostream>
#include <cstdlib>
using namespace std;
const int maximum_size = 200;
//Queue using array
class Queue {
public:
int front, back;
int data[maximum_size];
public:
Queue()
{
front = 0;
back = 0;
}
void Enqueue(int element)
{
// Limits queue from growing more than maximum_size -1
if (Size() == maximum_size - 1)
cout << "The queue is full.";
else
{
data[back] = element;
// mod
//back indicator will wrap around
back = ++back % maximum_size;
}
}
int Dequeue()
{
if (isEmpty())
{
cout << "The Queue is empty.";
return 0;
}
else
{
int ret = data[front];
// mod
//front indicator will wrap around
front = ++front % maximum_size;
return ret;
}
}
int Front()
{
if (isEmpty())
{
return 0;
}
return data[front];
}
int Size()
{
return abs(back - front);
}
bool isEmpty()
{
return (front == back) ? true : false;
}
void print_queue()
{
if (isEmpty())
{
cout << " The queue is now empty." << endl;
}
else
{
for (int i = front; i < back; i++)
cout << data[i] << endl;
}
}
};
class PriorityQueue :public Queue //PriorityQueue class derived from Queue Class
{
public:
PriorityQueue() :Queue()
{
}
//function to remove elements from the queue
int Bque()
{
int min = 0;
if (isEmpty())
{
cout << "The queue is empty";
return 0;
}
else //find the minimum element in the queue and delete it
{
min = Front(); //assume first element to be minimum
int index = front;
for (int i = front + 1; i < back; i++)
{
if (min>data[i]) //if any other element is less then the minimum element change the values
{
min = data[i];
index = i; //save the index value so that we can shift the array after finding minimum element
}
}
//shifting the entire array after minimum element is found
for (int i = index; i < back - 1; i++)
{
data[i] = data[i + 1];
}
back = back - 1; //now rear will decrease as one element is deleted
}
return min;
}
};
int main()
{
cout << "********* Welcome. ********" << endl;
cout << "Assignment #3 by Andre Nguyen -- Priority Queue" << endl;
cout << "****************************" << endl;
PriorityQueue q;
// Enqueue elements
q.Enqueue(25);
q.Enqueue(12);
q.Enqueue(5);
q.Enqueue(10);
q.Enqueue(-3);
cout << "This queue contains 5 elements:" << endl;
q.print_queue();
// Dequeue elements
cout << " Now removing the elements:" << endl;
cout << "element:" << q.Dequeue() << endl;
cout << "element:" << q.Dequeue() << endl;
cout << "element:" << q.Dequeue() << endl;
cout << "element:" << q.Dequeue() << endl;
cout << "element:" << q.Dequeue() << endl;
q.print_queue();
}
Explanation / Answer
Explanation: this code can now able to take dynamic input and also it will ask again for input
Example case:
Input :
5
25
12
2
10
-3
y
1
2
Y
2
1
2
n
Output:
********* Welcome. ********
Assignment #3 by Andre Nguyen -- Priority Queue
****************************
Enter no of elements
Enter elements
This queue contains 5 elements:
25
12
2
10
-3
Now removing the elements:
element:-3
element:2
element:10
element:12
element:25
The queue is now empty.
Again(y/n)
Enter no of elements
Enter elements
This queue contains 1 elements:
2
Now removing the elements:
element:2
The queue is now empty.
Again(y/n)
Enter no of elements
Enter elements
This queue contains 2 elements:
1
2
Now removing the elements:
element:1
element:2
The queue is now empty.
Again(y/n)
Code :
#include <iostream>
#include <cstdlib>
using namespace std;
const int maximum_size = 200;
//Queue using array
class Queue {
public:
int front, back;
int data[maximum_size];
public:
Queue()
{
front = 0;
back = 0;
}
void Enqueue(int element)
{
// Limits queue from growing more than maximum_size -1
if (Size() == maximum_size - 1)
cout << "The queue is full.";
else
{
data[back] = element;
// mod
//back indicator will wrap around
back = ++back % maximum_size;
}
}
int Dequeue()
{
if (isEmpty())
{
cout << "The Queue is empty.";
return 0;
}
else
{
int ret = data[front];
// mod
//front indicator will wrap around
front = ++front % maximum_size;
return ret;
}
}
int Front()
{
if (isEmpty())
{
return 0;
}
return data[front];
}
int Size()
{
return abs(back - front);
}
bool isEmpty()
{
return (front == back) ? true : false;
}
void print_queue()
{
if (isEmpty())
{
cout << " The queue is now empty." << endl;
}
else
{
for (int i = front; i < back; i++)
cout << data[i] << endl;
}
}
};
class PriorityQueue :public Queue //PriorityQueue class derived from Queue Class
{
public:
PriorityQueue() :Queue()
{
}
//function to remove elements from the queue
int Dequeue()
{
int min = 0;
if (isEmpty())
{
cout << "The queue is empty";
return 0;
}
else //find the minimum element in the queue and delete it
{
min = Front(); //assume first element to be minimum
int index = front;
for (int i = front + 1; i < back; i++)
{
if (min>data[i]) //if any other element is less then the minimum element change the values
{
min = data[i];
index = i; //save the index value so that we can shift the array after finding minimum element
}
}
//shifting the entire array after minimum element is found
for (int i = index; i < back - 1; i++)
{
data[i] = data[i + 1];
}
back = back - 1; //now rear will decrease as one element is deleted
}
return min;
}
};
int main()
{
cout << "********* Welcome. ********" << endl;
cout << "Assignment #3 by Andre Nguyen -- Priority Queue" << endl;
cout << "****************************" << endl;
bool is_true = true;
int n;
while(is_true){
cout << "Enter no of elements" << endl;
cin >> n;
cout << "Enter elements" << endl;
PriorityQueue q;
int i=0;
for(i=0;i<n;i++)
{
int element;
cin >> element;
q.Enqueue(element);
}
// Enqueue elements
// q.Enqueue(25);
// q.Enqueue(12);
// q.Enqueue(5);
// q.Enqueue(10);
// q.Enqueue(-3);
cout << "This queue contains "<< n << " elements:" << endl;
q.print_queue();
// Dequeue elements
cout << " Now removing the elements:" << endl;
for(i=0;i<n;i++)
{
cout << "element:" << q.Dequeue() << endl;
}
// cout << "element:" << q.Dequeue() << endl;
// cout << "element:" << q.Dequeue() << endl;
// cout << "element:" << q.Dequeue() << endl;
// cout << "element:" << q.Dequeue() << endl;
q.print_queue();
cout << "Again(y/n)" << endl;
char again;
cin >> again;
if (!(again == 'Y' || again == 'y')){
is_true = false;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.