Question

Consider the following C code which implements a stack; int data[100]; int sp=0; void push(int n)...

Consider the following C code which implements a stack;

int data[100];

int sp=0;

void push(int n)

{data[sp++}=n;}

int pop()

{return(data{data[--sp]);}

For the following questions, show the contents of the data array, as well as the value of sp, in the spaces provided (after all the shown instructions are executed). If the contents are unpredictable, indicate this with "?" Assume the statements are executed in order, i.e., the statements in part (b) are executed after the statements in (a) and so on.

(a) Beginning with an empty stack, consider the following statements:

push(10); push(20); push(15);

sp: ____

data[0]: ______

data[1]: ______

data[2]:______

data[3]: ______

(b) now execute: push(7); pop(); pop();

sp:_______

data[0]: ______

data[1]: ______

data[2]:______

data[3]: ______

data[4]: ______

(c) Now execute: pop(); push(7); push(8); push(6);

sp:_______

data[0]: ______

data[1]: ______

data[2]:______

data[3]: ______

data[4]: ______

(d) Now execute: push(15); pop(); pop(); pop(); pop(); pop();

sp:_______

data[0]: ______

data[1]: ______

data[2]:______

data[3]: ______

data[4]: ______

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here pop() operation means I have considered as deleting n element from stack So I havent mentioned in stack contents after popping. In code it was not like it will just. If you need according to code then let me know> I have added two answers as per code and other as per concept.

1)

Consider the following C code which implements a stack;

int data[100];

int sp=0;

void push(int n)

{data[sp++}=n;}

int pop()

{return(data{data[--sp]);}

For the following questions, show the contents of the data array, as well as the value of sp, in the spaces provided (after all the shown instructions are executed). If the contents are unpredictable, indicate this with "?" Assume the statements are executed in order, i.e., the statements in part (b) are executed after the statements in (a) and so on.

(a) Beginning with an empty stack, consider the following statements:

push(10); push(20); push(15);

sp: __3__

data[0]: ___10___

data[1]: ___20___

data[2]:____15__

data[3]: ______

(b) now execute: push(7); pop(); pop();

sp:___2____

data[0]: ___10___

data[1]: ____20__

data[2]:______

data[3]: _____

data[4]: ______

(c) Now execute: pop(); push(7); push(8); push(6);

sp:____4___

data[0]: ___10___

data[1]: __7____

data[2]:___8___

data[3]: __6____

data[4]: ______

(d) Now execute: push(15); pop(); pop(); pop(); pop(); pop();

sp:__0_____

data[0]: ______

data[1]: ______

data[2]:______

data[3]: ______

data[4]: ______

2)

Consider the following C code which implements a stack;

int data[100];

int sp=0;

void push(int n)

{data[sp++}=n;}

int pop()

{return(data{data[--sp]);}

For the following questions, show the contents of the data array, as well as the value of sp, in the spaces provided (after all the shown instructions are executed). If the contents are unpredictable, indicate this with "?" Assume the statements are executed in order, i.e., the statements in part (b) are executed after the statements in (a) and so on.

(a) Beginning with an empty stack, consider the following statements:

push(10); push(20); push(15);

sp: __3__

data[0]: __10____

data[1]: __20____

data[2]:___15___

data[3]: ______

(b) now execute: push(7); pop(); pop();

sp:__2_____

data[0]: __10____

data[1]: ___20___

data[2]:___15___

data[3]: ___7___

data[4]: ______

(c) Now execute: pop(); push(7); push(8); push(6);

sp:___4____

data[0]: __10____

data[1]: __7____

data[2]:___8___

data[3]: __6____

data[4]: ______

(d) Now execute: push(15); pop(); pop(); pop(); pop(); pop();

sp:____0___

data[0]: __10____

data[1]: ___7___

data[2]:___8___

data[3]: __6____

data[4]: __15____

Add a comment
Know the answer?
Add Answer to:
Consider the following C code which implements a stack; int data[100]; int sp=0; void push(int n)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help. write a C program stack-ptr.c that implements a stack using a link list. Below...

    Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly. examplpe: push(5, &top); push(10, &top); push(15, &top); int value = pop(&top); value = pop(&top); value = pop(&top); this program currently has a race condition. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values. -use...

  • Suppose we execute the following stack operations on a stack of ints. push(1); pop(); // #1...

    Suppose we execute the following stack operations on a stack of ints. push(1); pop(); // #1 push(10); pop(); // #2 push(7); push(4); push(3); pop(); // #3 push(5); pop(); //#4 Write the final state of the stack, and for each pop() operation, write the value that will be popped off the stack (pops are numbered so you can refer to them).

  • Help asap 24. Given a stack variable stk which provides a push operation that places an...

    Help asap 24. Given a stack variable stk which provides a push operation that places an integer on he stack, and a pop operation which removes and returns the integer from t top of the stack, what would the stack look like, after the following code executed? top of t for(int k 1; k < 10; k++) if(k % 3 :0) stk.push( k+ stk.pop()); else stk.push( k);

  • 4. Given a stack variable stk which provides a push operation that places an integer o...

    4. Given a stack variable stk which provides a push operation that places an integer o top of the stack, and a pop operation which removes and returns the integer from t top of the stack, what would the stack look like, after the following code executed? for(int k 1; k < 10; k++) if(k % 3 :0) stk.push( k+ stk.pop()); else stk.push( k); 25. Given a queue variable que which provides an add operation which places an integer at...

  • 2. Stacks and Queues Reinernber: Consider the following function: void systery( int num) { int current:...

    2. Stacks and Queues Reinernber: Consider the following function: void systery( int num) { int current: /* create Stack stk and Queue que / Stacks: the function push places the given parameter on the top of the stack, the function pop removes and returns the top of the stack, and the function empty returns true (i.c., 1) if the stack is empty or false (i.e., 0) otherwise. Queues: the function insert places the given parameter on the end of the...

  • Consider the following C++ program: #include <iostream> using namespace std; void f1(int); void f2(int); void f3(int);...

    Consider the following C++ program: #include <iostream> using namespace std; void f1(int); void f2(int); void f3(int); int main() { f1(10); return 0; } void f1(int n) { f2(n + 5); } void f2(int n) { f3(n - 2); } void f3(int n) { cout << n << endl; // LINE 1 } Just before the program statement marked with the comment "LINE 1" is executed, how many stack frames will be on the program call stack?

  • Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top =...

    Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...

  • Assume the stack pointer (SP) is initialized to 0x20000000 . Registers RO, R1, R2 and R12...

    Assume the stack pointer (SP) is initialized to 0x20000000 . Registers RO, R1, R2 and R12 are initialized to 2, 3, 8 and 15 respectively. Answer the following: Show the content of the stack and the SP after the following sequence of operations. PUSH (R12} PUSH {R1-R2} PUSH (RO) Answer format example=> {1,2,3,4} if 1,2,3,4 are values in stack and with no spaces. Answer: Does the following cause a data hazard for the 5-stage LEGV8 pipeline? i1: ADD XO, X1,...

  • A stack S of integers initially contains the following data: 6 (top) 2 7 3 The...

    A stack S of integers initially contains the following data: 6 (top) 2 7 3 The following code is then executed: int x = S.pop(); int y = S.pop(); int z = S.pop(); S.push(x + y); int w = S.pop(); S.push(w + z); After this code has been executed, what are the contents of the stack? A. 8 10 B. 10 8 C. 15 3 D. 10 8 6 3

  • 1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show...

    1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show the Linked List after the following commands are executed: Stack myStack = new Stack(); myStack.push(20); myStack.push(40); myStack.pop(); myStack.push(60); myStack.push(80); 2)If the same commands were used but the Stack was implemented with an Array with maximum size of 10, show what the array would look like after all these commands are executed. Assume O(1) implementation of push and pop here as well. 3)Given a Queue...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT