need help understanding this code and the output

Hello. My teacher gave me a question to trace, but im not sure as why the output comes out the way it is. I tried tracing it. first you enqueue num 2 which is 1 and then followed by num2 which is 4. then comes the dequeue part where we remove the 1 and then insert num1-num2 into queue which is 4. now heres the confusing part. so far i got 4 4. but i looked online and the solution is 4 4 4. im confused as since when did num 2 and num 1 become 4? is it because of aQueue.enqueue(num1 - num2)? Thanks for any clarifications
1
2
3
4
5
6
7
8
9
10
11
num1 = 5
num2 = 1
num3 = 4
aQueue.enqueue(num2)
aQueue.enqueue(num3)
aQueue.dequeue()
aQueue.enqueue(num1 - num2)
aQueue.dequeue(num1)
aQueue.dequeue(num2)
cout << num2 << " " << num1 << " " << num3 << endl
I think this is what is happening.

1
2
3
4
5
6
7
8
9
10
11
12
13
num1 = 5
num2 = 1
num3 = 4
aQueue.enqueue(num2) 2
aQueue.enqueue(num3) 2, 4
aQueue.dequeue() 4
aQueue.enqueue(num1 - num2) 5-1, 4
aQueue.dequeue(num1) num1 = 5-1
aQueue.dequeue(num2) num2 = 4
cout << num2 << " " << num1 << " " << num3 << end

4 4 4
Last edited on
for the first two enqueue i think u meant 1 not 2.

im good up to line 7 but the last two dequeue i still dont get. i thought 4 was num 3 so why did it go away when num1 is dequeued same for num2, it was never inserted into the queue. this is one confusing problem
Topic archived. No new replies allowed.