Help with arrays.

Hello, I have asked a similar question earlier but I was seeing if this code is right or not. Seeing this makes a bit sense to me from my research however, for some reason it always says it is symmetric, even when I do play with the matrix a bit. Am I going at a right path here? any feedback would be much appreciated.

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
#include <iostream>
using namespace std;
int main(void) {

	int pause;

	double matrix[][4] = { { 1, 2, 3, 4 },
	{ 2, 2, 3, 1},
	{ 3, 3, 3, 0 },
	{ 4, 1, 2, 4 } };

	int side = sizeof(matrix[0]) / sizeof(matrix[0][0]);
	bool issymmetric = true;
	

		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 4; j++) {
				if (matrix[i][j] != matrix[j][i])
					return false;
				
		                 if (issymmetric)
			                cout << "The matrix is symmetric" << endl;
		                 else
			              cout << "The matrix is not symmetric" << endl;
  
				cin >> pause;
				return 0;

			}
}

Why are you returning ... from main() ... on line 19? You might conceivably do this from a function, but that is not what you have here. It looks like you have copied this code from somewhere that had it in a separate function.

Why are you stating whether the matrix is symmetric or not within the same loops that you are checking it? You have to complete (or break out of) the loops before you can state whether the matrix is symmetric or not.

The variable issymmetric is perfectly legitimate, but I don't see anywhere that you give it a value other than true. So that is, obviously, what your program reports.

Having calculated side ... what - if anything - are you doing with it? This looks like another bit of code ripped from somewhere else.


Lilil wrote:
Seeing this makes a bit sense to me from my research

I would strongly suggest that you stop "researching" (ie taking random bits of code from elsewhere in the vain hope that they will magically assemble themselves into what you want) and start thinking how you would do this for yourself. How would you determine on paper whether a matrix is symmetric? Then code that. "Researching" means reading and understanding, not "cut-and-pasting".


Please make your indentation consistent. Then you can see more accurately the structure of your code and you won't be tempted to give an answer about symmetricity and return 0 to end main when you have only looked at one element.
Last edited on
Topic archived. No new replies allowed.