Hello all,

I just want to make sure if my answer to this question is right or no. If no please help me solve it. Thanks in advance.

Here is the problem:

What is the output of the following psuedocode

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 << end1

My answer is: 5 1

Trace the code through with particular attention to changes on the original variables.

Trace the code through with particular attention to changes on the original variables.

Ok I did trace it and the answer I got was: 1 5.........is this correct?

on line 1 the answer I've got is 1
then on line 2 the answer became 1 4
then on line 3 it became 4
line 4 my answer was 4 4

Untill here everything is working good with me, but on line 6 I got confused.

on line 6, I got 4 5
on line 7, I got 5 1

and my final answer is 5 1

that was what I got, please help me to understand the last three lines and tell me if I am correct or not.

Thanks alot in advance and by the way this is my first semester in C++ so forgive me if my question looks easy for everybody else here!

I'm assuming the parameter passed into the dequeue method is a reference one which will assign the value being dequeued into that variable (which you didn't include in the specification)? Remember to dequeue is to remove the first item in the queue. Reread what your methods do to double check.

Also, your output statement has 3 variables in the cout statement, you know you'll have to have 3 numbers.

>> Untill here everything is working good with me

Looks good here too.
>> but on line 6 I got confused.

What happened to line 5? And what do these numbers represent? What's in the queue or what's in the variables?

>> I got 4 5

Again, "got" needs to be more specific, and read what jonsca said too. We're assuming dequeue means "take it out of the queue and stick it in the variable. Your queue contains only 4's, so take one out and stick it in num1.

For the next line, take the remaining 4 out and stick it in num2, so num1 and num2 now contain 4 and num3 is unchanged at 4.

At first I thought it was a trick question (because otherwise those variables would go unmodified), but there must be a provision about passing those last two variables as references. I think you're right on the money, Vernon.

The question should ask for a diagram of the queue at the different points.

    static void Main(string[] args)
    {
        int num1 = 5;
        int num2 = 1;
        int num3 = 4;

        Queue<int> aQueue = new Queue<int>();
        aQueue.Enqueue(num2);
        aQueue.Enqueue(num3);
        aQueue.Dequeue();
        aQueue.Enqueue(num1 - num2);

        num1 = (Int32)aQueue.Dequeue();
        num2 = (Int32)aQueue.Dequeue();

        Console.Write(num1+" "+num2+" "+num3);
        Console.ReadLine();


    }
    //writen by : saeid Hoveizi
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.