简体   繁体   中英

Reversing a two-dimentional array in C++

So I am trying to take a 10x10 array with random number inputs and in a certain format, and then display the reversed version of it (position [0][0] will now be [9][9] and so on and so forth, but I'm getting a C6385 error at the part that's supposed to create the reversed array. (problematic part encased in \\\\)

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    const int ROW = 10;
    const int COLUMN = 10;
    srand(time(NULL));

    int array[ROW][COLUMN] = {};
    int transposed[ROW][COLUMN] = {};

    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COLUMN; j++)
        {
            array[i][j] = rand() % 10;

        }
    }
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COLUMN; j++)
        {
            int it = ROW - i;
            int jt = COLUMN - j;
            transposed[i][j] = array[it][jt];
        }
    }
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

    cout << "     ORIGINAL" << endl;
    for (int i = 0; i < ROW; i++)
    {
        if (i == 0)
        {
            cout << "     1   2   3   4   5   6   7   8   8   10" << endl;
            cout << "   +---+---+---+---+---+---+---+---+---+---+" << endl;
        }
        for (int j = 0; j < COLUMN; j++)
        {
            if (((j > 0) && (j < 9)) && (i < 10))
            {
                cout << " | " << array[i][j];
            }
            else if (j == 9)
            {
                cout << " | " << array[i][j] << " |";
            }
            else if ((j == 0) && (i == 9))
            {
                cout << i + 1 << " | " << array[i][j];
            }
            else if ((j == 0) && (i < 9))
            {
                cout << i+1 << "  | " << array[i][j];
            }

        }
        cout << endl;
        if (i < 10)
        {
            cout << "   +---+---+---+---+---+---+---+---+---+---+" << endl;
        }
    }

    cout << "     Transposed" << endl;
    for (int i = 0; i < ROW; i++)
    {
        if (i == 0)
        {
            cout << "     1   2   3   4   5   6   7   8   8   10" << endl;
            cout << "   +---+---+---+---+---+---+---+---+---+---+" << endl;
        }
        for (int j = 0; j < COLUMN; j++)
        {
            if (((j > 0) && (j < 9)) && (i < 10))
            {
                cout << " | " << transposed[i][j];
            }
            else if (j == 9)
            {
                cout << " | " << transposed[i][j] << " |";
            }
            else if ((j == 0) && (i == 9))
            {
                cout << i + 1 << " | " << transposed[i][j];
            }
            else if ((j == 0) && (i < 9))
            {
                cout << i + 1 << "  | " << transposed[i][j];
            }

        }
        cout << endl;
        if (i < 10)
        {
            cout << "   +---+---+---+---+---+---+---+---+---+---+" << endl;
        }
    }
    return (0);
}

This is problematic:

            int it = ROW - i;
            int jt = COLUMN - j;

When i is 0 , then it is 10 . Same for jt Remember [0,0] maps to [9,9] because arrays always start at an index of 0. The last valid element in an N sized array is [N-1] , not N .

So when this line is executed:

        transposed[i][j] = array[it][jt];

Oops. transposed[0][0] = array[10][10] . That's not what you want.

Hence you want this:

            int it = ROW - i - 1;
            int jt = COLUMN - j - 1;

Hence, your code runs pretty well at that point:

     ORIGINAL
     1   2   3   4   5   6   7   8   8   10
   +---+---+---+---+---+---+---+---+---+---+
1  | 6 | 6 | 8 | 8 | 6 | 6 | 7 | 9 | 6 | 5 |
   +---+---+---+---+---+---+---+---+---+---+
2  | 4 | 2 | 9 | 6 | 0 | 2 | 8 | 9 | 4 | 6 |
   +---+---+---+---+---+---+---+---+---+---+
3  | 4 | 8 | 0 | 3 | 0 | 3 | 2 | 5 | 8 | 0 |
   +---+---+---+---+---+---+---+---+---+---+
4  | 7 | 0 | 5 | 5 | 3 | 0 | 3 | 5 | 7 | 3 |
   +---+---+---+---+---+---+---+---+---+---+
5  | 8 | 3 | 4 | 5 | 4 | 1 | 9 | 7 | 9 | 9 |
   +---+---+---+---+---+---+---+---+---+---+
6  | 6 | 7 | 2 | 2 | 3 | 3 | 3 | 8 | 2 | 8 |
   +---+---+---+---+---+---+---+---+---+---+
7  | 6 | 2 | 3 | 9 | 6 | 7 | 0 | 5 | 1 | 6 |
   +---+---+---+---+---+---+---+---+---+---+
8  | 6 | 9 | 5 | 8 | 0 | 9 | 8 | 9 | 3 | 0 |
   +---+---+---+---+---+---+---+---+---+---+
9  | 5 | 5 | 0 | 8 | 3 | 3 | 2 | 6 | 4 | 8 |
   +---+---+---+---+---+---+---+---+---+---+
10 | 1 | 1 | 7 | 0 | 2 | 8 | 5 | 2 | 7 | 4 |
   +---+---+---+---+---+---+---+---+---+---+
     Transposed
     1   2   3   4   5   6   7   8   8   10
   +---+---+---+---+---+---+---+---+---+---+
1  | 4 | 7 | 2 | 5 | 8 | 2 | 0 | 7 | 1 | 1 |
   +---+---+---+---+---+---+---+---+---+---+
2  | 8 | 4 | 6 | 2 | 3 | 3 | 8 | 0 | 5 | 5 |
   +---+---+---+---+---+---+---+---+---+---+
3  | 0 | 3 | 9 | 8 | 9 | 0 | 8 | 5 | 9 | 6 |
   +---+---+---+---+---+---+---+---+---+---+
4  | 6 | 1 | 5 | 0 | 7 | 6 | 9 | 3 | 2 | 6 |
   +---+---+---+---+---+---+---+---+---+---+
5  | 8 | 2 | 8 | 3 | 3 | 3 | 2 | 2 | 7 | 6 |
   +---+---+---+---+---+---+---+---+---+---+
6  | 9 | 9 | 7 | 9 | 1 | 4 | 5 | 4 | 3 | 8 |
   +---+---+---+---+---+---+---+---+---+---+
7  | 3 | 7 | 5 | 3 | 0 | 3 | 5 | 5 | 0 | 7 |
   +---+---+---+---+---+---+---+---+---+---+
8  | 0 | 8 | 5 | 2 | 3 | 0 | 3 | 0 | 8 | 4 |
   +---+---+---+---+---+---+---+---+---+---+
9  | 6 | 4 | 9 | 8 | 2 | 0 | 6 | 9 | 2 | 4 |
   +---+---+---+---+---+---+---+---+---+---+
10 | 5 | 6 | 9 | 7 | 6 | 6 | 8 | 8 | 6 | 6 |
   +---+---+---+---+---+---+---+---+---+---+

它编译于我没有错误,但有一个问题关于价值观像这样我使用的克利翁v3.15和C ++ 14

In addition to the answer by @serbie fixing your indexing problem, you can simplify the algorithm for reversing the array itself -- in-place without the need of a second array. With your current algorithm, you are not so much reversing an array as you are simply filling a second array with the elements of the first in reverse order. If you attempted to actually reverse array in-place, you would just end up swapping each element twice and end up with the same array as you started with. Additionally, an odd or even number of rows would provide different results.

For your annotated output, you would still need to output the array with the headings and row-numbers you like, but for the reversal itself, in-place, it can be reduced to, eg:

#define ROW  3
#define COL ROW

void rev2d (int (*a)[COL])
{
    /* loop rows increment from 0 while row < end row decrementing from ROW-1 */
    for (int i = 0, j = ROW-1; i <= j; i++, j--) {
        /* loop cols 0->COL-1 and COL-1->0 while i != j or col < endcol */
        for (int k = 0, l = COL-1; i != j ? k < COL : k < l; k++, l--) {
            /* swap element */
            int n = a[i][k];
            a[i][k] = a[j][l];
            a[j][l] = n;
        }
    }
}

( note: you can modify the function to take the row and col values if you are not using constants)

The algorithm will work for row and col values greater or equal to 2 (you can add a check for an array of size 1x1 and issue a warning if desired, or just let things remain unchanged as it would be currently).

The handling of odd / even number of rows is done through the ternary used in the inner-loop conditional, eg i != j ? k < COL : k < l i != j ? k < COL : k < l which reverses full rows unless there are an odd number of rows, and then when i = j it simply reverses to the middle-element

It provides the complete reversal of the array, eg:

Example

$ ./bin/revarr2da
original array:
  1  2  3
  4  5  6
  7  8  9

reversed array:
  9  8  7
  6  5  4
  3  2  1

Your algorithm is fine if you want to fill a separate array, if you are interested in swapping the values in place, this is just an additional way to approach it with

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