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

Related searches

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

```  
  
  
```

I would need to turn this into the reversed form:

```  
  
  
```

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

Reversing a 2D array in C, Iterate loop till start index is less than ending index, swap the value at these indexes and update the index as: swap(arr[i][start], arr[i][end]) start++; end--; How to reverse a 2D array in C++. If the data stored is in the form of matrices, it is necessary to modify the contents of the matrices. Manipulation in the contents of the matrix will result in modification of the data. This article is a tutorial for reversing a two dimensional array. The Libraries included #include<iostream.h> #include<conio.h>

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.

How would I go about reversing a 2D array in C?, 1 Answer. You are reversing each row of the array. So write a function that will reverse an array passed as an parameter to it. The problem with your code is that you yourself are contradicting what you want to achieve. In this post, we will see how to reverse an array in C. 1. Using auxiliary array. Simple solution is to create an auxiliary array of same type and size as the input array, fill it with elements from the input array in reverse order, and then copy contents of the auxiliary array into the original one.

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

```    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
```

9.3. Reversing a Two-Dimensional Array, 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� C Program To Reverse Array Elements (1-Dimensional Array) Learn How To Reverse Array Elements in C Programming. It is important that we should know How A For Loop Works before getting further with the C Program Code. The following code is a C Program to Reverse an Array Without using another Array.

9.3. Reversing a Two-Dimensional Array Problem You need to reverse each row in a two-dimensional array. The Array.Reverse method does not support this. C program to reverse an array: This program reverses the array elements. For example, if 'A' is an array of integers with three elements such that A = 1, A = 2, A = 3 Then after reversing, the array will be A = 3, A = 2, A = 1

Here, it is reversing the array. void ArrayReverese (int a [], int Start, int End) Within the above function of a reverse array in c program, If statement will check whether the Start position is less than End position. If the condition is TRUE, then only statements inside this If block will execute.

C Program to Reverse an Array - This program reverses the array elements. For example if a is an array of integers with three elements such that a = 1 a = 2 a = 3 Then on reversing the Web Design

• 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; } } }`
• 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?
• 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.
• 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.
• 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.
• 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.