DancingDylan
DancingDylan

Reputation: 53

Vertically/Horizontally flip a 2d array

This might be considered a low-brow question. Yet, I haven't found any code, or any forums discussing how to go about this in my programming language, C. Plenty of attempt have been made, and all end up in practically "hard-coding" the new arrays.

I'm trying to vertically, then horizontally flip a 2d array. It seemed easy enough, just went through systematically and manipulated each row and column value. But, what if I want to go about an array that isn't just a basic 3x3 array. Such as 11x11, or 15x15?

**CODE SNIPPET:**

int array[3][3]= { {1,2,3},
                   {4,5,6},
                   {7,8,9} };

int vertFlip[3][3],
    horzFlip[3][3]; 

for(i = 0; i < rows; i++)
{
  for(j = 0; j < cols; j++)
  {
     vertFlip[0][i]= array[2][i];
     vertFlip[1][i]= array[1][i];
     vertFlip[2][i]= array[0][i];   
  }
} //and so on for horizontal, obviously my nested for loop was different. 

But, what if this array was something much larger, (123x123 sized array) does anyone know an effective way to accomplish this task?

Upvotes: 2

Views: 27162

Answers (3)

Markus Dutschke
Markus Dutschke

Reputation: 10626

Template solution

works for arrays of ints, floats, doubles, ...

template <class t>
void flip_horizontal(const int nrows, const int ncols, t* data)  // flips left-right
{
    for (int rr = 0; rr < nrows; rr++)
    {
        for (int cc = 0; cc < ncols/2; cc++)
        {
            int ccInv = ncols - 1 - cc;
            std::swap<t>(data[rr * ncols + cc], data[rr * ncols + ccInv]);
        }
    }
}

template <class t>
void flip_vertical(const int nrows, const int ncols, t* data)  // flips: bottom-up
{
    for (int cc = 0; cc < ncols; cc++)
    {
        for (int rr = 0; rr < nrows/2; rr++)
        {
            int rrInv = nrows - 1 - rr;
            std::swap<t>(data[rr * ncols + cc], data[rrInv * ncols + cc]);
        }
    }
}

Upvotes: 0

Rahul
Rahul

Reputation: 229

Hello there You can try a simple approach to transpose your 2d array :-

int ar[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int i,j,ar1[3][3];
for(i = 0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
       ar1[i][j] = ar[j][i];
    }
}

as you can see you just have to iterate loop to your array length and into the loop ar1[i][j] = ar[j][i] perform the flip operation.

Upvotes: 1

Harvey Pham
Harvey Pham

Reputation: 660

I would start out with a simple approach. For horizontal flip:

int swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp; 
}

void FlipRow(int *row, int columns)
{
    // A row is a simple one dimensional array
    // just swap item 0 with item n-1, 1 with n-2, ...
    for (int index = 0; index < columns / 2; index++)
    {
        swap(row+index, row+columns-1-index);
    } 
}

void HFlipArray(int **array, int columns, int rows)
{
    for (int row = 0; row < rows; row++)
    {
        FlipRow(array[row], columns);
    }
}

For vertical flip, use similar approach:

// Reuse swap
void FlipColumn(int **array, int column, int rows)
{
    // Flip column 'column' of an array that has n rows.
    for (int row = 0; row < rows/2; row++)
    {
        swap(array[row]+column, array[rows-1-row]+column);
    }
}

void VFlipArray(int **array, int columns, int rows)
{
    for (int column = 0; column < columns; column++)
    {
        FlipColumn(array, column, rows);
    }
}

Note that the above code changes the contents of the input. If that is not desired, you can modify the code to pass in destination and source arrays and adjust your loops accordingly.

Upvotes: 3

Related Questions