110k views
1 vote
Write a method named swapAll that accepts two arrays of integers as parameters and swaps their entire contents. You may assume that the arrays passed are not null and are the same length. For example, if the following arrays are passed:int[] a1 = {11, 42, -5, 27, 0, 89};

int[] a2 = {10, 20, 30, 40, 50, 60};

1 Answer

3 votes

Answer:

void swapAll(int *a1, int *a2, int n){

int i;

int tmp[n];

for(i = 0; i < n; i++){

tmp[i] = a1[i];

a1[i] = a2[i];

a2[i] = tmp[i];

}

}

Step-by-step explanation:

You just need a temporary structure.

I am going to write a c script, in which a1 and a2 are the arrays and n is the length of the arrays.

void swapAll(int *a1, int *a2, int n){

int i;

int tmp[n];

for(i = 0; i < n; i++){

tmp[i] = a1[i];

a1[i] = a2[i];

a2[i] = tmp[i];

}

}

User Makhdumi
by
6.3k points