#need help about matrix question

53 messages · Page 1 of 1 (latest)

late tide
#

Given a two-dimensional square matrix of order n x n, swap the elements in the upper and lower quarters and also swap the elements in the left and right quarters, excluding elements located on the diagonals. The matrix is divided into four quarters, bounded by the main and secondary diagonals: upper, lower, left, and right.


5
1 4 5 6 5
5 6 7 8 9
9 0 1 2 4
3 4 5 6 0
1 2 3 2 1
Sample Output 0

1 2 3 2 5
9 6 5 8 5
4 2 1 0 9
0 4 7 6 3
1 4 5 6 1
worn vaultBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

balmy mist
#

So what part is giving you trouble?

late tide
#

it's too hard

balmy mist
#

yeah, at least from what you've written, it wasn't immediately clear to me what you had to do.

#

maybe this explains it

#

you have to swap the red trianges and the blue triangles

late tide
#

actually i got that point but the problem is that i don't know how to code something like this

balmy mist
#

ok, so the first thing you want to do is to iterate over the matrix

#

and then for each element, you want to figure out where it has to go

#

and then you swap it with the element at that position

#

there's one small catch: If you do it like I just said, you end up swapping each element twice, once on the left side and once on the right side, and so it ends up where it was originally

#

eh, I guess I didn't explain that last part properly

#

ok, so notice that element on the left of that diagonal end up on the right side of it, and elements on the right side end up on the left side:

late tide
balmy mist
#

do you know how to solve this?

late tide
#

like i < n / 2?

balmy mist
#

kind of

#

depends on what you mean, but it sounds like that would work

late tide
#

let me understand and try if i have problem i will mention it

#

thanks a lot for explanation

balmy mist
#

np

#

hmm actually, I don't like this i < n / 2 approach all that much

#

depending on what i is here (row or column), this means you only iterate over the upper or left half of the matrix

#

and the idea of iterating over only half of the matrix is correct, but I wouldn't use the upper or left half

balmy mist
#

so I would iterate over the elements to the left of this diagonal

#

do you know how you can do that?

worn vaultBOT
#

@late tide Has your question been resolved? If so, type !solved :)

late tide
#
#include <iostream>
using namespace std;

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

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (i != j && i + j != n - 1) {
                if (i < j && i + j < n - 1) {
                    int new_i = n - 1 - j;
                    int new_j = n - 1 - i;
                    swap(matrix[i][j], matrix[new_i][new_j]);
                } else if (i > j && i + j < n - 1) {
                    int new_i = j;
                    int new_j = i;
                    swap(matrix[i][j], matrix[new_i][new_j]);
                }
            }
        }
    }

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

i managed to do something but failed

balmy mist
balmy mist
balmy mist
#

ok, so assuming your code right now looks like ```c++
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
...

that means you're considering the entries to the left of this line:
#

so now you have to figure out if an entry belongs to the left triangle or to the lower triangle

#

can you think of a condition that would test that?

late tide
balmy mist
#

yeah that looks correct

#

we're already making sure that j < i, so we can't get the upper or right triangle

#

which means we only need to test if i + j is less than, equal, or greater than n - 1

#

if it's equal, we don't do anything

#

if it's less than n-1, it's a left triangle, and to get to the right triangle, we need to flip the entry horizontally

#

which means we need to change j and keep i the same

#

and if it's greater than n - 1, we change i and keep j the same

late tide
#

THANK U

#

I UNDERSTAND IT

#

EASY SHIT

#

thanks a lot bro

#

!solved

worn vaultBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity