Matrix

Hello , I need help with a certain problem.The task is to check if a nxn matrix has a diagonal,whose numbers are all different.The program should return 1 if it exists such diagonal and 0 if not.Using functions is recommended.I know how to do it only for the main diagonal but I should check every other one and I dont know how.
Example:
5 5 5
5 5 1
5 5 5 should return 1

I know how to do it only for the main diagonal
Can you show how you did it? We can extend that to the other diagonal, it's just a matter of calculating the indexes a little differently.
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
int check_diagonals(int matrix[100][100], int size) {
	int i = matrix[0][0];
	int j = 1;
	while (j < size) {
		if (matrix[j][j] != i) {
			++j;
			i = matrix[j][j];
			return 1;
		}
		else return 0;
	}
}




int main() {
	int matrix[100][100];
	int rows, columns;
	cin >> rows >> columns;
	if (rows != columns) {
		return 0;
	}
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < columns; j++) {
			cin>>matrix[i][j];
		}

	}
	
	bool statement_diagonals = check_diagonals(matrix, columns);
	cout << statement_diagonals;


	system("pause");
	return 0;
}
The teacher said that the solution is with 4 nested loops inthe function.
[ Edit: I misread the problem thinking that it returned 1 when all elements of a diagonal are the same. So much of this post is irrelevant. ]

I know how to do it only for the main diagonal
I'm sorry to say that your program doesn't work for the main diagonal either. With this input, it returns 1 instead of 0:
3 3
1 0 0
1 2 0
1 1 2

One problem is that the while loop in check_diagonals() only executes once. That's because there's a return inside the if at line 8, and a return inside the else at line 10. So regardless of whether the if is true, the function will return.

Let's get check_diagonals working first. Then we can modify the code to check the other diagonal. As kbw said, checking the other diagonal is easy once you know how to check the first one.

Here is pseudo-code for checking the major diagonal. See if you can rewrite check_diagonals to do this:
1
2
3
4
5
6
7
// Check each item on the major diagonal against all items below and to the right of it.
for i=0 to size-1 {
    for j = i+1 to size-1 {
        if matrix[i][i] != matrix[j][j] then they aren't the same so return 0
    }
}
// If you get here then they were all the same. Return 1. 
Last edited on
Well,if the diagonal has different digits it must return 1, and even if two are the same it shoud return 0 . Secondary diagonals must also be checked.
Think about the coordinates for the first few elements. They're:
(0, 0)
(1, 1)
(2, 2)
(3, 3) ...

Maybe there's a pattern there ... somewhere. If you spot it, write the code to check those coordinates.

Do the same for other angles, write the coordinates out on paper, then try to code it.
if the diagonal has different digits it must return 1
My mistake. I misread the problem. I thought it returned 1 if they were all the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
bool check_diagonals(int a[100][100] , int size) {
	int result = 1;
	for (int i = 0; i < size; i++) {
		for (int j = i + 1; j < size -1; j++) {
			if (a[i][i] == a[j][j]) result = 0; break;
			
		}
		if (result == 0) break;
	}
		

	return result;
}
this one is for the main diagonal , now i have to check every other and the reverse diagonals too
One set of diagonals (parallel to the main diagonal) are the lines i-j=u, constant. Loop through u and either i or j.

The other set of diagonals (orthogonal to the main diagonal) are the lines i+j=v, constant. Loop through v and either i or j.

You can cut it down to 2 nested loops if, on any diagonal, you try and insert into a set<int>, since a set won't permit repeats.


5	5	5	
5	5	1	
5	5	5	
Found distinct diagonal i - j = -1
result = 1



Edit:
You may need to revise your statement of the problem, @gabrinka. It occurs to me that if you allow ALL the diagonals of the matrix then the diagonals that just include corner elements (and hence have only one element) automatically and trivially satisfy your criterion. Are you sure that you don't want just the main diagonal and the full diagonal at right angles to it?
Last edited on
Topic archived. No new replies allowed.