444,943 views
22 votes
22 votes
Write a 2 integer parameter void function. The parameters are reference parameters. If the first parameter is greater than the second parameter then the function exchanges the values of the two parameters. If the first parameter is not greater than the second parameter then the function does not exchange the values of the parameters.

User Arcanox
by
3.0k points

2 Answers

17 votes
17 votes

Answer:

Here is a Java function that takes two integer parameters and uses reference parameters to exchange their values if the first parameter is greater than the second parameter:

public void exchangeValues(int[] params) {

// check if the first parameter is greater than the second parameter

if (params[0] > params[1]) {

// swap the values of the two parameters

int temp = params[0];

params[0] = params[1];

params[1] = temp;

}

}

To use this function, you can pass an array of two integers as the argument, like this:

int[] params = {5, 3};

exchangeValues(params);

// after calling the function, the array should be [3, 5]

If the first parameter is not greater than the second parameter, the function will not exchange their values. For example, if you call the function with the following array:

int[] params = {3, 5};

exchangeValues(params);

// after calling the function, the array should be [3, 5]

The values of the parameters will remain unchanged because the first parameter (3) is not greater than the second parameter (5).

User TLS
by
3.0k points
8 votes
8 votes

Answer:

void swap(int& x, int& y) {

if (x > y) {

int temp = x;

x = y;

y = temp;

}

}

Step-by-step explanation:

User Tobias Gubo
by
4.0k points