Ask Question
27 October, 10:43

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};

+3
Answers (1)
  1. 27 October, 13:50
    0
    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];

    }

    }

    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];

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a method named swapAll that accepts two arrays of integers as parameters and swaps their entire contents. You may assume that the ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers