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

Hello c++ experts, I need to some help with this project I\'m stuck on. A revers

ID: 3577220 • Letter: H

Question

Hello c++ experts, I need to some help with this project I'm stuck on.

A reverse poem, or palindrome poem, is a poem that can be read forwards one way and have a meaning, but also be read backward and have another different meaning. Here is a great example on YouTube (the reader reverses it): Lost Generation

For this project, you will create a function that will allow a poet to write the forward poem and output the entire reverse poem: the original forward poem followed by its mirror image. For example, if this poem is entered:

REVENGE
seeks destruction,
like a dragon
breathes
fire

The output should be:

REVENGE
seeks destruction,
like a dragon
breathes
fire
fire
breathes
like a dragon
seeks destruction,
REVENGE

Requirements

To accomplish this objective, create two objects of a template queue class and put each line of the forward poem into each object. Then, create a template function to reverse one of the queues and push each line onto the other queue so that it contains the entire palindrome poem. You can use a loop or you can use recursion to accomplish this. You cannot use an array or vector for this assignment for any reason.

Note: You can use a stack as an additional data structure if you'd like an intermediate structure, but the original poem must be loaded into initialQ and the final palindrome poem must be loaded into finalQ and you cannot use an array or vector anywhere in your code. So, if you use a stack, you must load it from initialQ and unload it into finalQ. However, this is entirely optional and may not even be helpful, depending on your approach.

This function needs to be a template function because the poet may want to create a palindrome poem using characters rather than entire strings. For an example of a character palindrome poem, check out this page (scroll to example 2): Reverse Poetry

For this project, you must use the following prototype (do not change the prototype at all):

template<class Item>
void reversePoem(queue &initialQ, queue &finalQ);
/*
Precondition: Two objects, initialQ and finalQ, of a queue class have been filled with data.
Postcondition: The queue object initialQ has been loaded in reverse order onto the end of the queue object finalQ. In other words, finalQ contains its original data plus the reversed data from initialQ. The initialQ is empty.
*/

Your program must contain the following:

A template queue class of your choosing. It can be the template queue class demonstrated in lecture, a template queue class from a textbook or the Internet (please give attribution) or the STL queue class.  

A function using the prototype listed above. This function must be a template function. If you choose to use recursion, make sure this is also a recursive function (optional).

A main function that loads two queue objects and calls the reversePoem function and pass the two queue objects to that function, then the main function should pop all of the contents of the finalQ for display. This means that your main function will be the only function with cout statements.

Here is an example in which the the poem above would be loaded into the queue objects. The chart below shows the queue states before and after the reversePoem function is called. The data is displayed from the front of the queue on top, to the back of the queue on bottom.

Before the function After the function initialQ REVENGE
seeks destruction,
like a dragon
breathes
fire /*EMPTY*/ finalQ REVENGE
seeks destruction,
like a dragon
breathes
fire REVENGE
seeks destruction,
like a dragon
breathes
fire
fire
breathes
like a dragon
seeks destruction,
REVENGE

Explanation / Answer

#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;

template<class Item>
void reversePoem( queue<Item> &initialQ, queue<Item> &finalQ){
    stack<Item> tempStack;
    //first reverse the intiialQ
    while( !initialQ.empty() ){
        tempStack.push( initialQ.front() );
        initialQ.pop();
    }
    while( !tempStack.empty() ){
        initialQ.push( tempStack.top() );
        tempStack.pop();
    }
    //reversing done : using a stack

    //now remove elements from reversed queue, and push into final queue;
    while( !initialQ.empty() ){
        finalQ.push( initialQ.front() );
        initialQ.pop();
    }
    return;
}

int main(){
    queue< string > initialQ;
    queue< string > finalQ;
    string stringsAre[5] = {"REVENGE","seeks destruction,","like a dragon","breathes","fire"};
    for(int i = 0; i < 5; i++){
        initialQ.push( stringsAre[i] );
        finalQ.push( stringsAre[i] );
    }
    //created two queues with similar matter

    reversePoem( initialQ, finalQ );

    //got the palindrome, time to print it out
    while( !finalQ.empty() ){
        cout << finalQ.front() << endl;
        finalQ.pop();
    }

    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