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.

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.