0

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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
sxsa
  • 9
  • 3
  • Is each individual array in your array a row or a column? – The Zach Man Feb 12 '20 at 05:27
  • not sure what you mean but there is one 2D array that has rows and columns. in the post i did my best to visualize it. i edited my array format in the post just now to make it a little clearer as to what it looks like, but its a simple 2d array. – sxsa Feb 12 '20 at 05:29
  • 2
    The rows don't interact with each other, so start with code to reverse a 1D array. Once you've posted that, it's easy to explain how to adapt the code to the 2D array. – user3386109 Feb 12 '20 at 05:34
  • that's called mirroring or flipping, not reversing – phuclv Feb 12 '20 at 06:04

3 Answers3

2

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;
    }
}
Md Golam Rahman Tushar
  • 2,175
  • 1
  • 14
  • 29
2

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.

tonyjosi
  • 717
  • 1
  • 9
  • 17
  • 1
    I like the use of VLA, and making it handle non-square matrices is not hard. I would use `for` loops, though, more like: `void reverse_2d_arr(int size, int arr[size][size]) { for (int i = 0; i < size; i++) { for (int j = 0, k = size - 1; j < k; j++, k--) { int temp = arr[i][j]; arr[i][j] = arr[i][k]; arr[i][k] = temp; } } }` – Jonathan Leffler Feb 12 '20 at 06:40
  • 1
    Aside from VLA being optional for compiler support from C11 on. Most compilers will continue support. – David C. Rankin Feb 12 '20 at 07:07
0

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, e.g.

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
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • If the array has a fixed width (`COLS`), why are you passing the width as a parameter (`cols`) and not using `COLS` in the code? – Jonathan Leffler Feb 12 '20 at 06:35
  • In case the arrays are allocated and the parameters passed from the caller. (yes, you can remove both `rows` and `cols` parameters and just rely on the `#define` values in the simple case), but just trying to make the functions generic. – David C. Rankin Feb 12 '20 at 06:37
  • 2
    If you want the function generic, then I'd suggest using `void rev2d (int rows, int cols, int a[rows][cols])` instead. Shouldn't affect the function body you've written. – Lundin Feb 12 '20 at 08:00
  • Yes, that would work, but it would still leave me with `a` as a VLA -- which I intended to avoid. I guess I could add it with that note. – David C. Rankin Feb 12 '20 at 08:12
  • Does anyone know of a C compiler that both claims C11 (or C18) support and denies supporting VLAs by defining `__STDC_NO_VLA__`? MSVC is unlikely to claim support for C11, I think, though it might be getting better on that. – Jonathan Leffler Feb 12 '20 at 15:09
  • As as VS2017 MS claims C11 support and allows a standard choice with `/STD`. Prior to VS2017 the only parts of C11 provided were those required to support C++11. I'll try to find the link that discussed this. – David C. Rankin Feb 12 '20 at 19:48
  • Here is the SO link I was thinking about [Is there any option to switch between C99 and C11 C standards in Visual Studio?](https://stackoverflow.com/questions/48981823/is-there-any-option-to-switch-between-c99-and-c11-c-standards-in-visual-studio) – David C. Rankin Feb 12 '20 at 20:16