User Tools

Site Tools


cs509:cpluscplusswapbyreference

See the following code:

#include <iostream>
using namespace std;


void swapvars( int& a, int& b ) {
    int temp = a;
    a = b;    // a is now b
    b = temp; // b is now a
}


int main ()
{
   int a = 2;
   int b = 3;
   
   swapvars( a, b );
   
   cout << "The value of a is " << a << " and the value of b is " << b;
   
   return 0;
}

What will be output by the above program?

  • a - The value of a is 2 and the value of b is 3;
  • b - The value of a is 3 and the value of b is 2;
  • c - The value of a is 2 and the value of b is 2;
  • d - The value of a is 3 and the value of b is 3;

<poll What will be output by the above program? [d]>

  • a
  • b
  • c
  • d

</poll>

cs509/cpluscplusswapbyreference.txt · Last modified: 2013/02/20 19:41 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki