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

1.What is the output of the following program? #include <iostream> #include <str

ID: 3565904 • Letter: 1

Question

1.What is the output of the following program?

#include <iostream>

#include <string>

#include "myStack.h"

using namespace std;

template <class type>

void mystery(stackType<type>& s1, stackType<type>& s2,

stackType<type>& s3);

int main()

{

stackType<string> stack1;

stackType<string> stack2;

stackType<string> newStack;

string fNames[] = {"Chelsea", "Kirk", "David", "Stephanie",

"Bianca", "Katie", "Holly"};

string lNames[] = {"Jackson", "McCarthy", "Miller", "Pratt",

"Hollman", "Smith", "Klien"};

for (int i = 0; i < 7; i++)

{

stack1.push(fNames[i]);

stack2.push(lNames[i]);

}

mystery(stack1, stack2, newStack);

while (!newStack.isEmptyStack())

{

cout << newStack.top() << endl;

newStack.pop();

}

}

template <class type>

void mystery(stackType<type>& s1, stackType<type>& s2,

stackType<type>& s3)

{

while (!s1.isEmptyStack() && !s2.isEmptyStack())

{

s3.push(s1.top() + " " + s2.top());

s1.pop();

s2.pop();

}

}

2.Convert the following infix expressions to postfix notations:

a. x * (y + z) - ( w + t)

b. (x + y) / (z - w) * t

c. ((x - y) + (z - w) / t) * u

d. x - y / (z + w) * t / u + (v - s)

Explanation / Answer

Answer to first question is:

Chelsea Jackson
Kirk McCarthy
David Miller
Stephanie Pratt
Bianca Hollman
Katie Smith
Holly Klien

Explanation:
   When the data is inserted into stack 1 and 2, it looks some what like this because of the push operation

       | Holly |
       | Katie |
       | Bianca |
       | Stephanie |
       | David |
       | Kirk |
       | Chelsea |
       -----------------
       Stack s1


       | Klien |
       | Smith |
       | Hollman |
       | Pratt |
       | Miller |
       | McCarthy   |
       | Jackson |
       -------------
       Stack s2

   And when they are popped they values come out in reverse order (remember First In First Out)

       | Chelsea Jackson |
       | Kirk McCarthy |
       | David   Miller |
       | Stephanie Pratt |
       | Bianca Hollman |
       | Katie   Smith |
       | Holly   Klien |
       ---------------------------
       Stack newStack

       That means, that Holly and Klien is popped first and hence it is pushed to the bottom of the newStack, then Katie and Simth and so on.

   Finally when newStack is popped we get our output. Again FIFO.
      
---------------------------------------------------------------------------------------------------------------------------

Answer to second question is:

a. x * (y + z) - ( w + t)

   x * yz+ - wt+    // parantheses have precedence
   xyz+* - wt+    // multiplaction has precedence over subtraction
   xyz+*wt+-      
  
x y z + * w t + -

b. (x + y) / (z - w) * t
   xy+ / zw- * t     // parantheses have precedence
   xy+zw-/ * t     // divion and multiplaction have same precedence (at least in c++) but I choose Left to right parsing hence division was picked first
   xy+zw-/t*
  
x y + z w - / t *

c. ((x - y) + (z - w) / t) * u
   (xy- + zw- / t) * u    // innermost parantheses have precedence
   (xy- + zw-t/) * u    // division has precedence over subtraction
   xy-zw-t/+ * u           // parantheses have precedence
   xy-zw-t/+u*
  
x y - z w - t / + u *

d. x - y / (z + w) * t / u + (v - s)
   x - y / zw+ * t / u + vs-        // parantheses have precedence   
   x - yzw+/ * t / u + vs-           // L to R parsing. Also divison have multiplaction jave equal and higher precedence than addition and subtraction
   x - yzw+/t* / u + vs-            // L to R parsing.
   x - yzw+/t*u/ + vs-               // L to R parsing.
   xyzw+/t*u/- + vs-               // L to R parsing.
   xyzw+/t*u/-vs-+                   // L to R parsing.
x y z w + / t * u / - v s - +

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote