Hello, I have an exam on wednesday and was given a practice exam but I'm having a little a bit of trouble.

8.What will be displayed on the screen after the following statements are executed in a C++ program?

int a=5, b=2, c=1, d=3;
         int t;
         if(a>b){
           t=a;
           a=b;
           b=t;
         }
 
         if(b>c){
           t=b;
           b=c;
           c=t;
         }
 
        if(c>d){
           t=c;
           c=d;
           d=t;
        }
 
cout<<a<<"   "<<b<<"  "<<c<<"  "<<d<<endl

my answer: cout<<a<<"2"<<b<<"1"<<

My question is what happens when the statement fails? Would c output 1 and d output 3?

Thanks for the help.

Dani AI

Generated

Good instinct to use pencil-and-paper. A reliable way to trace either snippet is to make a simple table with one column per variable (include the temporary t and the loop index). Put the initial values on the first row, then process each statement top to bottom: evaluate the condition (mark true/false), apply any assignment(s), and write the next row showing the new state. That row-by-row trace prevents losing intermediate values when swaps or increments happen and is exactly the mechanical step was suggesting.

A quick observation about the three adjacent swap checks: executing adjacent compare-and-swap operations left-to-right is a single forward pass of the bubble-sort idea. One forward pass guarantees the largest item will "bubble" to the far right, but a single pass does not necessarily fully sort the list unless you repeat passes until no swaps occur. For a deeper read, see Bubble sort.

For the loop question, remember that while is a pre-test loop: the condition is checked before the body runs. When tracing, always record the loop-variable value before executing the body, compute whatever the body prints from that value, then apply any increments. A common exam pitfall is to confuse the loop variable itself with the value shown by an expression that uses it; ’s wording blurred that distinction and caused the confusion. See the language details at while (cppreference) and the increment operators at operator++/-- (cppreference).

Exam checklist: use a trace table, annotate condition outcomes, include temporary variables, note pre/post-increment behavior, and when asked to generalize, use the invariant (e.g., "single forward pass moves largest to the end") rather than trying to memorize outputs.

Recommended Answers

All 6 Replies

use paper & pencil to do this problem. Final answer should be

a = 2
b = 1
c = 3
d = 5

use paper & pencil to do this problem. Final alswer should be:

a = 2
b = 1
c = 3
d = 5

Thanks for the reply, but I don't see how that works. I tried doing it with a paper and pencil and filled in the values for each. I understand where the 2 and 1 come from, but don't understand where the 3 and 5 come from. If you could help me to see that I would really appreciate it.

after each if statement, the grid should look like this:

a   b   c   d
5   2   1   3
2   5   1   3
2   1   5   3
2   1   3   5

after each if statement, the grid should look like this:

a   b   c   d
5   2   1   3
2   5   1   3
2   1   5   3
2   1   3   5

hey thanks for the help, now I can see how it works. For some reason I just couldn't get the whole picture in my mind.

But I do need some help with something else:
When you run a C++ program, what will appear on the screen after the following statements are executed?

int i =2;
      while ( i < 7 )
      {
               cout << i * 2 << endl;
               i++;
      }

when I run the program it displays: 4, 6, 8, 10, 12
But on the exam we can't use the computer and I don't see how they got those answers.
Thanks again for all the help

Again, use pencil & paper to write out the value on each iteration of the loop. If you know how to multiply something by 2 then this should not be difficult.

On the first iteration of the loop the value of i is 4. Then i is incremented to 5, then 6.

Again, use pencil & paper to write out the value on each iteration of the loop. If you know how to multiply something by 2 then this should not be difficult.

On the first iteration of the loop the value of i is 4. Then i is incremented to 5, then 6.

lol, wow I must be tired, just before you posted I was getting ready to edit my post and say I figured it out. Sorry about that, thanks again for the help.

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.