Check my code, please!

Somewhere there is an error in the result constantly 2 is deduced. I can't find her.
Or offer your vision for solving this task:
determine whether a given square matrix is symmetric with respect to its lateral diagonal.


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
const int ROWS{ 2 };
const int COLS{ 5 };
srand((unsigned)time(NULL));
int col = 0, row = 0;
int E[ROWS][COLS];

cout << "Matz - " << ROWS << " x " << COLS << "\n\n";

for (int row{ }; row < ROWS; row++)
{
for (int col{ }; col < COLS; col++)
{
E[row][col] = rand() % 10;

cout << E[row][col] << ' ';
}
cout << '\n';
}
cout << '\n';
int tmp;
{
for (int row = 0; row < COLS; row++)
for (int col = 0; col < ROWS; col++)
{
tmp = E[row][col];
E[row][col] = E[col][row];
E[col][row] = tmp;
}
}
cout << tmp << endl;

}
Last edited on
Time you figured out the posting rules.
https://www.cplusplus.com/articles/jEywvCM9/
@chebyrek, read your own question.
SQUARE means ROWS==COLS
Checking for symmetric doesn't mean transposing it.

It looks like you've copied code from elsewhere and it doesn't do anything like your intention.
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
#include <iostream>
#include <random>

int main()
{
	const size_t SIZE {2};

	auto rand {std::mt19937 {std::random_device {}()}};
	auto randNo {std::uniform_int_distribution<size_t> {0, 9}};

	int E[SIZE][SIZE] {};

	for (auto& r : E) {
		for (auto& e : r)
			std::cout << (e = randNo(rand)) << ' ';

		std::cout << '\n';
	}

	bool is_symm {true};

	for (size_t row = 0; is_symm && row < SIZE; ++row)
		for (size_t col = 0; is_symm && col < SIZE; ++col)
			if (E[row][col] != E[col][row])
				is_symm = false;

	if (is_symm)
		std::cout << "\nIs a Symmetric Matrix\n";
	else
		std::cout << "\nIs not a Symmetric Matrix\n";
}

seepluc, thank you for your help. You have helped to solve many problems.
Topic archived. No new replies allowed.