Swapping Arrays

I’m amazed, and a bit envious, when I look at other programming languages to discover a rich variety of functions, or “methods,” that do specific tasks the C language lacks. One of these tasks deals with arrays.

Arrays in C are quite primitive, which is to be expected for a medium-level programming language. In other, higher languages, plenty of tools available to manipulate the contents of an array or report specific aspects.

For example, you can sort an array, pluck out the minimum or maximum values, collect subsets, reverse the contents, add and remove elements (dynamically), and more. All those actions are available to the array data type without having to code your own functions. Of course, one of the things I enjoy about C is that it forces you to code your own functions for these tasks. The process helps train your programming kung fu.

Suppose that one of the functions available in another programming language swaps the contents of two arrays. In the following code, you see two arrays, a and b. Each has the same number of elements, but with different values:

int main()
{
    int a[SIZE] = { 2, 4, 6, 8, 10 };
    int b[SIZE] = { 1, 3, 5, 7, 9 };

    printf("Original arrays:\n");
    show_arrays(a,b);

    swap_arrays(a,b);

    printf("After swap:\n");
    show_arrays(a,b);

    return(0);
}

Two functions are referenced in the code snippet: show_arrays() and swap_arrays(). Your task for this month’s Exercise is to write these functions. This process isn’t particularly tricky, so don’t believe that I’m throwing you a curve; displaying and swapping arrays are activities that must be hand-coded in C.

Click here to view my solution.

2 thoughts on “Swapping Arrays

  1. I have been using JavaScript quite a lot over the past few years, and its array has a comprehensive set of methods which make it a very powerful and versatile data structure. I assume this is the sort of thing you were referring to in the first paragraph. Strictly speaking it isn’t an array, more of an exotic and possibly unique dynamic object, which makes it inefficient by C standards. (What isn’t?!)

    My long, long list of “things to experiment with in C” includes trying to replicate some of this functionality for C arrays. Of course C and JavaScript are profoundly different so an exact equivalent is impossible as well as being undesirable, but a library of powerful array functions would be a very useful thing. It seems so obvious when you think about it that other people must have already come up with the same idea. I’ll dig around sometime and try to find what other people have come up with.

    Apologies if I am being stupid, but I cannot see the purpose of swapping arrays. Would it not be easier to just use the array which contains the values you need?

  2. Thanks for the input, Chris.

    I’m also thinking of Perl, which has some marvelous functions that work on arrays or sets of data. C is rather (excuse the term) one-dimensional when it comes to data collections. Other languages have such rich features, specifically associative arrays.

    As you point out, the practicality of swapping an array is silly. I forget the source for this problem, but I read it elsewhere on the web, where two datasets needed to be swapped. That inspired me to write this Exercise. The key here is that you can swap with individual elements (which is my solution posting soon) or swap with another array, which is how someone else solved the puzzle.

Leave a Reply