Ask Question
20 July, 15:26

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

+5
Answers (1)
  1. 20 July, 15:44
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers