125k views
1 vote
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.

1 Answer

6 votes

Answer:

  1. public static void rotate4ints(int num1,int num2,int num3,int num4){
  2. int temp = num1;
  3. num1 = num4;
  4. int temp2 =num2;
  5. num2 = temp;
  6. int temp3 = num3;
  7. num3 = temp2;
  8. num4 = temp3;
  9. System.out.println("Rotated values "+num1+","+num2+","+num3+","+num4);
  10. }

Step-by-step explanation:

This is implemented in Java programming language

Aside the four int variables that will be rotated, three other teporary variables temp1-3 are created to hold temporary values while values are swapped

num1 gets assigned the value of num4, num2 gets assigned the value of num1, num3 gets assigned the value of num2 and num4 gets assigned the of num3.

Pay close attention to the use of the temp variables which are temporary holders of values in-order to swap their values

See a complete java program below with the output attached

public class num8 {

public static void main(String[] args) {

int num1 =15; int num2 = 47; int num3 = 6; int num4=23;

System.out.println("Original Arrangement");

System.out.println("Rotated values "+num1+","+num2+","+num3+","+num4);

System.out.println("After Rotation");

rotate4ints(num1,num2,num3,num4);

}

public static void rotate4ints(int num1,int num2,int num3,int num4){

int temp = num1;

num1 = num4;

int temp2 =num2;

num2 = temp;

int temp3 = num3;

num3 = temp2;

num4 = temp3;

System.out.println("Rotated values "+num1+","+num2+","+num3+","+num4);

}

}

Write the definition of a function named rotate4ints that is passed four int variables-example-1
User Lashae
by
3.1k points