erm im trying to create a function to test if an array/matrix is symmetrical
that is for example a[i,j] = a[j,i]
i think i got the symmetrical test function correct....just that i dont know where to put that if-else statement. it just doesnt seem to fit anywhere in my codes.

#include <iostream>
using namespace std;

const int MAXROW = 10;
const int MAXCOL = 10;

int main()
{
srand(time(NULL));

int a [MAXROW][MAXCOL];

for (int row = 0; row < MAXROW; row++)
{
for (int col = 0; col < MAXCOL; col++)
{
a[row][col] = rand () % 10;
cout << a[row][col] << "\t";
}
cout << endl;

if (a[row][col] == a[col][row])
cout << "Symmetric" << endl;
else
cout << "Not symmetric" << endl;
}
return 0;
}

## All 4 Replies

Populating the matrix with random numbers will almost never give you a semmatric matrix. In order to test your program you will have to hard-code the matrix with known values like this

You have to create two nested loops to test each value, like this:

for(int i = 0; i < MAXROW; ++i)
{
for(int j = 0; i < MAXCOL; ++j)
{
// put test here
}
}

hmm i tried your suggestion but it gives me some strange results...i got something like this

4 symmetric
9 non symmetric
1 non symmetric
0 symmetric
etc
etc

this is what i got after ur suggestion

#include <iostream>
using namespace std;

const int MAXROW = 10;
const int MAXCOL = 10;

int main()
{
srand(time(NULL));

int a [MAXROW][MAXCOL];

for (int row = 0; row < MAXROW; row++)
{
for (int col = 0; col < MAXCOL; col++)
{
a[row][col] = rand () % 10;
cout << a[row][col] << "\t";

if (a[row][col] == a[col][row])
cout << "Symmetric" << endl;
else
cout << "Not symmetric" << endl;
}
cout << endl;

}
return 0;
}

You can't make the test until after the matrix has been filled in. So move lines 20-23 down after line 25 and start a new set of nested loops.

And as I said before you can't make this test using rand() to populate the array as you are doing on line 17 because you will almost never get a symmetric matrix that way.

ohh..i get what u mean now:icon_idea:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.