Question

In: Computer Science

Show what is written by the following segments of code, given that item1, item 2 and...

Show what is written by the following segments of code, given that item1, item 2 and item 3 are int variables.

StackType stack;

item1 = 1;

item2 = 0;

item3 = 4;

stack.Push(item2);

stack.Push(item1);

stack.Push(item1 + item3);

item2 = stack.Top();

stack.Pop();

stack.Push(item3*item3);

stack.Push(item2);

stack.Push(3);

item1 = stack.Top();

stack.Pop();

cout << item1 << endl <<item2 << endl << item 3 << endl;

while (!stack.isEmpty())

{

item1 =stack.Top();

stack.Pop();

cout << item1 << endl;

}

Solutions

Expert Solution

For the given code we'll go line by line. The output that is printed by the code is given in read color.

StackType stack;

Declares an empty stack.

item1 = 1;

item2 = 0;

item3 = 4;

Declares three variables with given values.

stack.Push(item2);

Adds 0 to the stack.

stack.Push(item1);

Adds 1 to the stack.

stack.Push(item1 + item3);

Adds 1 + 4 = 5 to the stack.

item2 = stack.Top();

Changes item2 from 0 to 5.

stack.Pop();

Deletes the element at the top of the stack i.e 5;

stack.Push(item3*item3);

Adds 4*4 = 16 to the stack.

stack.Push(item2);

Adds 5 to the stack.

stack.Push(3);

Adds 3 to the stack.

item1 = stack.Top();

Changes item1 from 1 to 3.

stack.Pop();

Deletes the element at the top of the stack i.e 3.

cout << item1 << endl <<item2 << endl << item 3 << endl;

Prints to the screen the values:

3

5

4

while (!stack.isEmpty())

{

item1 =stack.Top();

stack.Pop();

cout << item1 << endl;

}

Till the time stack is not empty, assign the item at the top of the stack to item1, delete the item at the top of the stack and print item1.

So this loop will print the entire stack from top to bottom. Here's its output:

5

16

1

0


Related Solutions

1. a) Given the two Verilog code segments, derive the schematics for the logic that will...
1. a) Given the two Verilog code segments, derive the schematics for the logic that will be synthesized from these blocks. BLOCK 1 input Y; reg A, B; always @(posedge clk) begin A = Y; B = A; end BLOCK 2 input Y; reg A, B; always @(posedge clk) begin A <= Y; B <= A; end b) Define setup time by using timing diagrams. Explain what would happen if any one of these timing requirements are violated. c) Describe...
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
What will the following code segments print on the screen? int num = 3; switch (num){...
What will the following code segments print on the screen? int num = 3; switch (num){             case 1:                         System.out.println(“Spring”); case 2:                         System.out.println(“Summer”); case 3:                         System.out.println(“Autumn”); case 4:                         System.out.println(“Winter”); }
Thinking in Assembly language What values will be written to the array when the following code...
Thinking in Assembly language What values will be written to the array when the following code executes? .data array DWORD 4 DUP(0) .code main PROC mov eax,10 mov esi,0 call proc_1 add esi,4 add eax,10 mov array[esi],eax INVOKE ExitProcess,0 main ENDP proc_1 PROC call proc_2 add esi,4 add eax,10 mov array[esi],eax ret proc_1 ENDP proc_2 PROC call proc_3 add esi,4 add eax,10 mov array[esi],eax ret proc_2 ENDP proc_3 PROC mov array[esi],eax ret proc_3 ENDP
Find the error in each of the following code segments and explain how to correct it....
Find the error in each of the following code segments and explain how to correct it. x = 1; while ( x <= 10 ); x++; } for ( y = .1; y != 1.0; y += .1 System.out.println( y ); switch ( n ) { case 1: System.out.println( “The number is 1” ); case 2: System.out.println( “The number is 2” ); break; default: System.out.println( “The number is not 1 or 2” ); break; } The following code should print...
Find and correct at least two errors in each of the following segments of code. a)...
Find and correct at least two errors in each of the following segments of code. a) final int ARRAY_SIZE = 5; ARRAY_SIZE = 10; int[] table = new int[ARRAY_SIZE]; for (int x = 0; x <= ARRAY_SIZE; x++) table[x] = 99; b) String[] names = {“Mina” , “George”}; int totalLength = 0; for (int i = 0; i <= names.length() ; i++) totalLength += names[i].length; c) String[] words = “Hello”,“Goodbye”; System.out.println(words.toUpperCase()); d) public class MyClass { private int x; private...
Java language (a) Write code segments to perform the following: (i) declare and create an integer...
Java language (a) Write code segments to perform the following: (i) declare and create an integer array freqArray of size 8 (ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68 (iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement (b) A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3};...
2. Specification - Given the following code base, add the appropriate function that will give the...
2. Specification - Given the following code base, add the appropriate function that will give the correct results/output. CODE: # TODO : Add Two Missing Functions HERE mlist = [(" Orange ", 10 , 0.25) ,( " Apple ", 5 , .20) , (" Banana ", 2 , 0.3) ,(" Kiwi ", 1 , 0.5)] addFruit (10 ," Lemon " ,0.1) displayFruitList ( mlist ) OUTPUT: Orange --- $ 2.50 Apple --- $ 1.00 Banana --- $ 0.60 Kiwi ---...
Write a pseudocode for the following code: /*every item has a name and a cost */...
Write a pseudocode for the following code: /*every item has a name and a cost */ public class Item {    private String name;    private double cost;    public Item(String name, double cost) {        this.name = name;        this.cost = cost;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getCost() {        return cost;...
VHDL code for a 4 to 2 priority encoder. The structural behavior must be written using...
VHDL code for a 4 to 2 priority encoder. The structural behavior must be written using gates.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT