Question

In: Computer Science

Show what is written by the following segment of code, given item1, item2, and item3 are...

Show what is written by the following segment of code, given item1, item2, and item3 are int variable:

item1 = 4;

item3= 0;

item2 = item1 + 1;

stack.Push(item2);

stack.Push(item2 +1);

stack.Push(item1);

item2 = stack.Top();

stack.Pop();

item1 = item2 + 1;

stack.Push(item1);

Stack.Push(item3);

while (!stack.IsEmpty())

{

     item3 = stack.Top():

     cout <<item3<<endl;

     stack.Pop();

}

cout<<item1 <<endl <<item2<<endl<<item3<<endl;

Solutions

Expert Solution

Stack is linear data structure which follows LIFO (Last In First Out) order, which means the value which is last pushed (inserted) into the stack will be popped (deleted) first. Top in a stack points to the value which is last inserted.

The value of item1 = 4.

The value of item2 = item1 + 1, that is, it will be 5.

The value of item3 = 0.

stack.Push(item2), this statement pushes item2's value to the stack, which means 5 will be inserted into the stack.

stack.Push(item2 +1), this statement pushes item2 + 1 value to the stack, which means 6 will be inserted into the stack.

stack.Push(item1), this statement pushes item1's value to the stack, which means 4 will be inserted into the stack.

Values in stack are: 4, 6, 5 (from top to bottom).

item2 = stack.Top(), this statement changes the value of item2 with the value which is at the top (last inserted), so now item2's value is 4.

stack.Pop(), this will delete the element at top, so 4 gets deleted, and now the top points to 6. Now stack has only 6 and 5 in it.

item1 = item2 + 1, this statement increments item1's value by 1 = 5.

stack.Push(item1), this statement inserts item1's value into the stack, which is 5. Now stack has 5, 6, 5 (from top to bottom). Top points to 5.

Stack.Push(item3), this statement inserts item3's value into the stack, which is 0. Now stack has 0, 5, 6, 5 (from top to bottom). Top points to 0.

while (!stack.IsEmpty()), this loop terminates when the stack gets empty, i.e., have no value in it.

{

     item3 = stack.Top(), this changes the value of item3 to the top most value of the stack.

     cout <<item3<<endl, this statement prints item3's value.

     stack.Pop(), this statement deletes the top most value of stack.

}

cout<<item1 <<endl <<item2<<endl<<item3<<endl, this prints the value of item1, item2 and item3, which 5, 4 and 5 respectively.

Final Output:

0

5

6

5

5

4

5


Related Solutions

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; }
I. Given the following code segment below what is the best description of line 5 and...
I. Given the following code segment below what is the best description of line 5 and line 6? Identify the public and private members. 1. #include <iostream.h> 2. class SimpleCat 3. { 4. public: 5. SimpleCat (int age, int weight); 6. ~SimpleCat(){} 7. int GetAge() {return itsAge;} 8. int GetWeight() {return itsWeight;} 9. private: 10. int itsAge; 11. int itsWeight; 12. }; II. Multiple Choice questions 1. A function that is called automatically each time an object is created or...
Given the following code segment, tell the output if it is part of an executable program....
Given the following code segment, tell the output if it is part of an executable program. Assume the addresses of the variables, a and b, are 0012A32A and 0012A340, respectively.        int* intPtr1, *intPtr2;        int a = 10, b;        intPtr1 = &a;        cout<< "The data in a is "<<a<<endl;        cout << "The address of a is "<<&a<<endl;        cout <<"The data in intPtr1 is "<< intPtr1 <<endl;        cout <<"The data in the variable pointed by...
21. What is the output of the following segment of code if 7 is input by...
21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout...
Trace the following code segment 1. use the red numbers under each statement to show the...
Trace the following code segment 1. use the red numbers under each statement to show the order that you execute the code by. For example: (1) means int p=3; (2) means p<10; (3) means p+=3; (1)(2)(3) means you execute int p=3 then p<10 then P+=3; (2)(1)(3) means you execute p<10 then int p =3 then P+=3; 2. show its output for (int p = 3 ; p <10; p += 3 )    (1) (2) (3)          {    for...
Trace the following code segment 1. use the red numbers under each statement to show the...
Trace the following code segment 1. use the red numbers under each statement to show the order that you execute the code by. For example: (1) means int p=3; (2) means p<10; (3) means p+=3; (1)(2)(3) means you execute int p=3 then p<10 then P+=3; (2)(1)(3) means you execute p<10 then int p =3 then P+=3; 2. show its output: int x = 30,  num = 100;     (1)    while ( x > 0) (2)           { cout   <<   endl                          <<  “ x...
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
Given the code segment: def f1(a, b = 2): if a : print(1) f1(1) what is...
Given the code segment: def f1(a, b = 2): if a : print(1) f1(1) what is the result of executing the code segment? a) Syntax error b) Runtime error c) No error d) Logic error in which scenario(s) is using a dictionary useful? a) To store the dates of lesson that each student is absent for so that warning letters may be issued to student exceeding a limit. b) To store the student numbers for students who sat for an...
no need to explain 11. What is output by the following code segment? boolean isHighTemp =...
no need to explain 11. What is output by the following code segment? boolean isHighTemp = true; double degree = 100.01; if (isHighTemp) if(degree >= 110) System.out.print(“Extremely Hot”); else System.out.println(“Not Hot”); A) Extremely Hot B) Not Hot C) Hot D) Extremely Hot Not Hot 12. To produce the output 2 4 6 8 10, what is the loop condition for the following loop? int n = 0; Page 3 do { n = n + 2; System.out.print(n + “ ”);...
The following code segment is stored in memory starting at memory location 0x00445670. What are the...
The following code segment is stored in memory starting at memory location 0x00445670. What are the two possible values for the contents of the PC after the branch instruction has executed?       bgez $a0, skip                      # mem location: 0x00445670 subu $s2, $s1, $t0 # branch NOT taken (false) ori    $v0, $t1, 0x0003 #       skip: addi $t0, $t1, 2 # branch taken (true) if taken: if not taken: Hint: Remember how many bytes each instructions takes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT