Ask Question

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. 22 February, 00:14
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers