Can someone explain me this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can someone explain me this code?

Arithmetic Stack

25th Jan 2017, 11:42 PM
Oral
12 Answers
+ 2
The code creates a stack in the form of an array. When you call push, you place a value on the stack. When you call pop, you remove a value from the stack and return it. The function add takes the last two values, adds them together and then pushes that value on the stack. The function mul does the same thing. So push(30); push(20); and push(10); place these values in the array. add(); then adds the last two together resulting in an array of [30, 30]. mul(); then multiplies those two values together and outputs the value, which would be 900 if I'm doing my math correctly.
26th Jan 2017, 12:11 AM
Division by Zero
+ 2
There's a missing ';' after the arithmetic_stack class definition (after the '}}' before in main() ) The code itself is pretty good documented, so what is it you struggle with? The stack itself can be pictured like stacking cubes with values. You can only add new cubes on top of the tower, and also only remove the cube on top. (That's what push and pop do.) add() takes the top two cubes, adds them together and places the new value at the top of the tower, and the same goes for mul(), just that it multiplies the values instead of adding them. In main you push three values onto the stack, so the stack looks like this: [10] [20] [30] Now stack.add() removes the top two values (10 and 20), adds them (30) and places the new value on the stack. [30] [30] Simular stack.mul() removes 30 and 30, multiplies them and pushes the result onto the stack: [900] Now stack.pop() removes the topmost (and in this case only) element from the stack and returns its value, which gets printed out to the console with cout. If you run the code the output should thus be 900.
26th Jan 2017, 12:21 AM
Robobrine
Robobrine - avatar
+ 1
I haven't done much C++ yet, but it looks like this code organizes "stacks" depending on their value (there's some explanation in the notes of your code).
26th Jan 2017, 12:11 AM
Jeremie
Jeremie - avatar
0
#include <iostream> class arithmetic_stack{ double stack[8]; // stack values int position; public: arithmetic_stack() { position = 0; } // PRE: position < 8; post: pushes value to new stack top void push(double value) { stack[position++] = value; } // PRE: position > 0; post: pops and returns value at stack top double pop() { return stack[--position]; } // PRE: position > 1; post: replaces two top-most stack values by the sum void add() { push(pop() + pop()); } // PRE: position > 1; post: replaces two top-most stack values by the product void mul() { push(pop() * pop()); } } int main() { arithmetic_stack stack; stack.push(30); stack.push(20); stack.push(10); stack.add(); stack.mul(); std::cout << stack.pop(); return 0; }
25th Jan 2017, 11:51 PM
Oral
0
after the 3 stack.push the array would be [30,20,10] after the stack.add the array would be [30,20,10,30] after the stack.mul the array would be [30,20,10,30,300] the cout would be 300
26th Jan 2017, 12:07 AM
nick
0
Output is 900
26th Jan 2017, 12:08 AM
Oral
0
maybe I was wrong I just glanced over it fast
26th Jan 2017, 12:09 AM
nick
0
I see where I went wrong I think. the arrays would look like [30,20,10] position =3 [30,30,10] position =1 [900,30,10] position =0
26th Jan 2017, 12:21 AM
nick
0
that pop doesn't actually remove the value from the array I think. doesn't it just change the position int so it reads from a different spot?
26th Jan 2017, 12:23 AM
nick
0
Well, yes, technically it doesn't remove values, but by changing position you'll never be able to access the values again once you used pop() on them.
26th Jan 2017, 12:26 AM
Robobrine
Robobrine - avatar
0
Nick: You're correct. The values aren't actually removed from the array. Instead, the position variable is merely decremented.
26th Jan 2017, 12:26 AM
Division by Zero
0
does anyone know if this is how STL containers function?
26th Jan 2017, 12:29 AM
nick