0

There is a matrix, then I have to make a function to know that the value on the left diagonal and the right diagonal is the same.

const int n = 100;
int main(){
    int n;
    char a[n][n];
    int dimL = 0;
    int dimR = 0;


    cout << "Input the dimension of the matrix: ";
    cin >> n;

    for( int i = 0; i < n; i++ )
        for( int j = 0; j < n; j++ ){
            cin >> a[i][j];
        }

    cout << "Matrix A is: " << endl;

    for( int i = 0; i < n; i++ )
        for( int j = 0; j < n; j++ ){
            cout << a[i][j] << " ";
            if( j == n - 1 )
                cout << endl;
        }

    check(a,n,dimL,dimR);

    if(check(a,n,dimL,dimR)){
        cout << "YES";
    }
    else {
        cout << "NO";
    }

}

In the main is just the simple code to input array.

bool check(char a[n][n], int n, int &dimL, int &dimR ){
    char left[n][n], right[n][n];

    for( int i = 0; i < n; i++ )
        for( int j = 0; j < n; j++ ){
            if( i == j ){
                left[dimL++] = a[i][j]; // left diagonal
            }
            if( i + j == n - 1 ){
                right[dimR++] = a[i][j]; // right diagonal
            }
        }

    for( int i = 0; i < n; i++ )
        if( left[i] != right[i] )
            return false;
        return true;
        
}

and this is the function. Of course isn't work. it said expression must be modifiable lvalue. Because its 1D to 2D. can I convert 2D array to 1D array ?

some code I realize that are wrong. so I changed.

const int x = 100;
bool check(char a[x][x], int n, int &dimL, int &dimR ){
    char left[x], right[x];

    for( int i = 0; i < n; i++ )
        for( int j = 0; j < n; j++ ){
            if( i == j ){
                left[dimL++] = a[i][j];
            }
            if( i + j == n - 1 ){
                right[dimR++] = a[i][j];
            }
        }

    for( int i = 0; i < n; i++ )
        if( left[i] != right[i] )
            return false;
        return true;
        
}

It works. I tried to input array a a a, b b b, c c c, and the output is yes. just a little simple problem isn't solved, I can't see the left or right array.

if i try to put some code in my main.

if(int i = 0; i < n; i++ ){
   cout << left[i];
}

It same shown that I need the expression that modifiable lvalue..

Thank you

aprilio
  • 11
  • 1
  • 5
  • 1
    Does this answer your question? [C++ 2D array to 1D array](https://stackoverflow.com/questions/19913596/c-2d-array-to-1d-array) – Irelia Sep 12 '21 at 23:30
  • Shouldn't the line `char left[n][n], right[n][n];` be `char left[n], right[n];` since you want those to be 1D? – Patrick Poitras Sep 12 '21 at 23:34
  • I tried. it didnt work. Is there any suggestion for me? – aprilio Sep 12 '21 at 23:40
  • Thanks patrick. but still didnt work – aprilio Sep 12 '21 at 23:42
  • In this case, for the example my matrix is: a b c d e f c b a the left diagonal is aea, and the right diagonal is cec. I tried to make an array for the left diagonal as left[n]. so left[0] = a, left[1] = e, left[2] = a, and so on.. – aprilio Sep 12 '21 at 23:47

1 Answers1

0

If it is pure array you have two ooptions:

for (int i = 0; i < rows; ++i)
    for (int j = 0; j < columns; ++i)
        oneDArr[i * columns + j] = twoDArr[i][j];

but you can also do it sneaky way;

int singleArraySize = columns * rows;
for (int i = 0; i < singleArraySize; ++i)
    *(oneDArr + i) = *(twoDArr + i);

the second example is taking advantage of fact that 2D array occupies consecutive spaces. In other word if j = 5 we have 10 columns, and i = 3, twoDArr[5 * 10 + 3] == twoDArr[53]

EDIT: so if you have array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}} in computer memory the are stored like this:

memory address | array[i][j] | value
---------------+-------------+---------
0x1            | array[0][0] | 1
0x2            | array[0][1] | 2
0x3            | array[0][2] | 3
0x4            | array[1][0] | 4
0x5            | array[1][1] | 5
0x6            | array[1][2] | 6
0x7            | array[2][0] | 7
0x8            | array[2][1] | 8
0x9            | array[2][2] | 9

where memory address is address where value is stored.