Breaking nested loops and an issue with constant number

this code check whatever a matrix is symmetric or not however i got two issues the first when i print N which suppose to be constant i get two values of it :\
the second is i get two value as if the break didn't work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<string>
using namespace std;


int main()
{
    int t ;
    cin >>t;
    int caseDone;
    while(t)
    {
        caseDone = 0;
        char Number[3];
        for(int i = 0; i < 3 ; i++)
            cin>>Number[i];
        int N = (int(Number[2]) - int('0'));
        int Mat[N][N]; 
        int Last_Element = N-1;
        for(int i  = 0; i < N ; i++)
        {
            for(int j = 0 ; j < N ; j++)
            {
                cin>>Mat[i][j];
            }
        }
        for(int i = 0 ; i < int(N/2) ; i++)
        {
            Last_Element = N-1;
            if(caseDone)
                break;
            for(int j = 0 ; j < N ; i++)
            {
                if(N-i-1 == i)
                {
                    if(Mat[i][j] == Mat[i][Last_Element])cout<<"Test #"<<t<<": Symmetric."<<endl;
                    else cout<<"Test #"<<t<<": Non-symmetric."<<j<<Last_Element<<endl;
                    if(j==Last_Element)break;
                }
                else if(Mat[i][j] != Mat[N-i-1][Last_Element--])
                {
                    cout<<"Test #"<<t<<": Non-symmetric."<<i<<N<<endl;
                    caseDone =1;
                    break;
                }
            }
        }
        t--;
    }
}



sample input
1
2
3
4
5
1
N = 3
5 1 3
2 0 2
3 1 5
On line 32: for(int j = 0 ; j < N ; i++) // j will never be increased
Hello, @zeroblank

Given that the input is from the console, is there any good reason why you can't just input N as a number, rather than trying to deduce it from a sequence of characters?

You are also declaring an array size
int Mat[N][N];
at run time, which is a bit naughty.

As far as your symmetry tests go, I'm struggling to see what you are intending to do, but it looks like you might be testing whether your matrix is left-right and up-down symmetric. That might be aesthetically pleasing, but the mathematical description of a symmetric matrix is one which is symmetric about the leading diagonal; i.e.
Mat[i][j] = Mat[j][i]

for all i and j. To test that, your outer (i) loop will have to run from top to bottom (0 to N-1) and your inner (j) loop from 0 to i-1 (at least); the remaining elements will be covered by redundancy.
Last edited on
Topic archived. No new replies allowed.