简体   繁体   中英

How would I go about reversing a 2D array in C?

Let's say my array looks like this. The number of rows will always match the number of columns in this program.

[0] [1] [2]
[3] [4] [5]
[6] [7] [8]

I would need to turn this into the reversed form:

[2] [1] [0]
[5] [4] [3]
[8] [7] [6]

I know that multidimensional arrays are just arrays of arrays, which hopefully shortens the scale of my question to just reversing a 1D array, but I don't know how to apply that same idea to a 2D array. The size will not always be a 3 by 3 array, but again the rows and columns will always be the same number.

Try following code, here n is the number of rows and m is the number of columns. Hopefully this will solve your problem. Happy coding!

for(i = 0; i < n; i++) {
    for(j = 0; j < m/2; j++) {
        int temp = arr[i][j];
        arr[i][j] = arr[i][m-j-1];
        arr[i][m-j-1] = temp;
    }
}

If you are looking for a function to reverse 2D array, then you can use a function declaration like this: void reverse_2d_arr(int, int (*)[]);

where,

void reverse_2d_arr(int size, int arr[size][size]) {
    int i = 0, j, k, temp;
    while(i < size) {
        j = 0;
        k = size - 1;
        while(j < k) {
            temp = arr[i][j];
            arr[i][j] = arr[i][k];
            arr[i][k] = temp;
            k--;
            j++;
        }
        i++;
    }
}

and call it using, reverse_2d_arr(3, arr); where arr is your 2d array and 3 its size.

Using Standard Arrays

From an efficiency standpoint, swap two-elements per-iteration when iterating over the column values. Since your array has a fixed width, start with the beginning and end elements, swap them, and continue working from the end to the middle, eg

void rev2d (int (*a)[COLS], int rows, int cols)
{
    for (int i = 0; i < rows; i++)
        for (int j = 0, k = cols - 1; j < k; j++, k--) {
            int tmp = a[i][j];
            a[i][j] = a[i][k];
            a[i][k] = tmp;
        }
}

(above the ..[j] and ..[k] elemnts are each swapped per-iteration of the inner-loop)

Or if you wanted to do the same thing using while loops and pointers to the beginning and end elements in each row (aside from iterating over the rows in reverse), you could do the following:

void rev2dptrs (int (*a)[COLS], int rows, int cols)
{
    while (rows--) {
        int *beg = *(a + rows), *end = *(a + rows) + cols - 1;
        while (end > beg) {
            int tmp = *beg;
            *beg++ = *end;
            *end-- = tmp;
        }
    }
}

In each case, for example if you had:

#define ROWS 3
#define COLS ROWS
...
    int a[][COLS] = {{ 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }};

Your function call would be:

    rev2d (a, ROWS, COLS);

or in the second case:

    rev2dptrs (a, ROWS, COLS);

Just a twist on a normal reversal.

Using Variable Length Arrays

The original intent was to avoid examples with VLA due to C11 Standard - 6.7.6.2 Array declarators(p4) "Variable length arrays are a conditional feature that implementations need not support;" and C11 Standard - 6.10.8.3 Conditional feature macros __STDC_NO_VLA__

However as pointed out in the comments, and with the real-world practicality that virtually all major compilers will continue to provide VLA, you can provide a bit more flexibility by reversing the declarations and specifying the rows and cols sizes fist and then passing the array as a VLA. The benefit is that it frees you from a constant size. For example, but functions can be re-written passing the array as a VLA:

void rev2dvla (int rows, int cols, int a[rows][cols])
{
    for (int i = 0; i < rows; i++)
        for (int j = 0, k = cols - 1; j < k; j++, k--) {
            int tmp = a[i][j];
            a[i][j] = a[i][k];
            a[i][k] = tmp;
        }
}

and with pointers:

void rev2dptrsvla (int rows, int cols, int a[rows][cols])
{
    while (rows--) {
        int *beg = *(a + rows),
            *end = *(a + rows) + cols - 1;
        while (end > beg) {
            int tmp = *beg;
            *beg++ = *end;
            *end-- = tmp;
        }
    }
}

Here, the benefit is you are freed from the integer constant constraint on the number of elements per-row. By specifying the rows and cols parameters before the array parameter, the rows and cols values are known before the array int a[rows][cols] is specified as a parameter allowing the VLA to be a complete type.

The function calls would then be:

    rev2dvla (rows, COLS, a);

and

    rev2dptrsvla (rows, COLS, a);

If you understand each of the ways and how they differ from the others -- then you have sorting a 2D array under control. Let me know if you have further questions.

Putting the full example together to exercise each function above at least once and adding a print2D function, you could do something like the following:

#include <stdio.h>

#define COLS 3

void rev2d (int (*a)[COLS], int rows, int cols)
{
    for (int i = 0; i < rows; i++)
        for (int j = 0, k = cols - 1; j < k; j++, k--) {
            int tmp = a[i][j];
            a[i][j] = a[i][k];
            a[i][k] = tmp;
        }
}

void rev2dptrs (int (*a)[COLS], int rows, int cols)
{
    while (rows--) {
        int *beg = *(a + rows),
            *end = *(a + rows) + cols - 1;
        while (end > beg) {
            int tmp = *beg;
            *beg++ = *end;
            *end-- = tmp;
        }
    }
}

void rev2dvla (int rows, int cols, int a[rows][cols])
{
    for (int i = 0; i < rows; i++)
        for (int j = 0, k = cols - 1; j < k; j++, k--) {
            int tmp = a[i][j];
            a[i][j] = a[i][k];
            a[i][k] = tmp;
        }
}

void rev2dptrsvla (int rows, int cols, int a[rows][cols])
{
    while (rows--) {
        int *beg = *(a + rows),
            *end = *(a + rows) + cols - 1;
        while (end > beg) {
            int tmp = *beg;
            *beg++ = *end;
            *end-- = tmp;
        }
    }
}

void prn2d (int (*a)[COLS], int rows, int cols)
{
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            printf (" %2d", a[i][j]);
        putchar ('\n');
    }
}

int main (void) {

    int a[][COLS] = {{ 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }},
        rows = sizeof *a / sizeof **a;

    puts ("original:");
    prn2d (a, rows, COLS);          /* print original */

    rev2d (a, rows, COLS);          /* reverse col values using indexes */
    puts ("\nreversed using indexes:");
    prn2d (a, rows, COLS);          /* print reversed array */

    rev2dptrs (a, rows, COLS);      /* reverse reversed array to restore original */
    puts ("\nrestore original using pointers:");
    prn2d (a, rows, COLS);          /* print original */

    rev2dptrs (a, rows, COLS);      /* reverse col values using pointers */
    puts ("\nreverse again using pointers:");
    prn2d (a, rows, COLS);          /* print reversed array */

    rev2dvla (rows, COLS, a);       /* reverse col values restoring original */
    puts ("\nrestore original using VLA w/indexes:");
    prn2d (a, rows, COLS);          /* print original */

    rev2dvla (rows, COLS, a);       /* reverse col values using indexes */
    puts ("\nreversed with VLA using indexes:");
    prn2d (a, rows, COLS);          /* print reversed array */

    rev2dptrsvla (rows, COLS, a);   /* reverse reversed array to restore original */
    puts ("\nrestore original using VLA w/pointers:");
    prn2d (a, rows, COLS);          /* print original */

    rev2dptrsvla (rows, COLS, a);   /* reverse col values using pointers */
    puts ("\nreverse again using VLA w/pointers:");
    prn2d (a, rows, COLS);          /* print reversed array */
}

Example Use/Output

$ ./bin/revarr2d
original:
  0  1  2
  3  4  5
  6  7  8

reversed using indexes:
  2  1  0
  5  4  3
  8  7  6

restore original using pointers:
  0  1  2
  3  4  5
  6  7  8

reverse again using pointers:
  2  1  0
  5  4  3
  8  7  6

restore original using VLA w/indexes:
  0  1  2
  3  4  5
  6  7  8

reversed with VLA using indexes:
  2  1  0
  5  4  3
  8  7  6

restore original using VLA w/pointers:
  0  1  2
  3  4  5
  6  7  8

reverse again using VLA w/pointers:
  2  1  0
  5  4  3
  8  7  6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM