C++ problem not a program?

Write the definition of a function named rotate4ints that is passed four int variables. The function returns nothing but rotates the values of the four variables: the first variable, gets the value of the last of the four, the second gets the value of the first, the third the value of the second, and the last (fourth) variable gets the value of the third. So, if i , j , k , and m have (respectively) the values 15, 47, 6 and 23, and the invocation rotate4ints(i,j,k,m) is made, then upon return, the values of i , j , k , and m will be 23, 15, 47 and 6 respectively.

Comments

  • The problem statement may confuse you by saying the function is "passed four int variables". In order for the values of these arguments to change, they need to be references, or pointers, to the caller's variables. Like this:

    void rotate4ints(int &a, int &b, int &c, int &d) {

      int z = d;

      d = c; c = b; b = a; a = z;

    }

  • You just need a single temp variable to store any one of the values, then shift as required, and fill that last with the temp value:

    temp = m; // last

    m = k;

    k = j;

    j = i;

    i = temp;

    Yes, it's just that easy!

    +add

    cja is correct, in order to shift the values your function must 'pass by reference' as he's shown.

  • Post your code.

    =

    Yahoo Answer members will help you get your homework right.

Sign In or Register to comment.