#Help with matrix on recursion problem

1 messages · Page 1 of 1 (latest)

shy mirage
#

Write a recursive function that verifies if the sum of the elements of the main diagonal is equal to that of the secondary diagonal.

plain pewterBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

shy mirage
#
#define N 100
int diagonalcmp(int matrix[N][N], int n) {
    if(n == 1 || n == 0) return 0;

    int sum = matrix[0][0] + matrix[n - 1][n - 1] - matrix[0][n - 1] - matrix[n - 1][0];
    return sum + diagonalcmp(&matrix[1][1], n - 2);
}
vapid pollen
#

seems wrong for n==1

shy mirage
#

I think I know why it currently does not work, what I think is happening is that I am calling the function with &matrix[1][1], which is the address of the cell at (1,1) but this does not modify the matrix by itself, meaning that iirc (since 2d arrays are unidimensional arrays) I would also have some other elements to handle

vapid pollen
#

(and arguably for n==0 too but not sure what it's returning exacly, seems ot should return a bool ?)

shy mirage
#

it returns 0 if equal, positive if the diagonal is bigger and negative if the secondary diagonal is bigger

#

but what I don't quite understand is the pointer arithmetic involved in the recursive call

vapid pollen
#

you can't indeed call a function that expect compile time array size with & of 1 element in the middle

#

use -Wall -Wextra -Wpedantic -Werror when compiling, it'll probably help flagging the most basic issues

shy mirage
#

can I not make it think it is an NxN matrix?

#

it actually is

vapid pollen
#

I actually don't remember, I would just have int *matrix and assume it's packed ie a 3x3 is (0,0), (0,1), (0,2), (1,0).... (2,2) consectutively

that has the small downside you'd use a function to get i,j

#

though that may be the same as int m[3][3] does already, I can never remember, juggling too many languages

#

clankers confirm m[3][3] is same memory layout as m[9] so you can still use that 2dimension access but you can't change N / has to be constant so it knows that m[i][j] = m[i*N+j]

shy mirage
#

clankers is wild btw

#

so ok, they are they same layout

#

but I cannot use them as the same layout

#

because of a type mismatch

#

if I have a 3x3 matrix my function would check the corners and then call itself with the middle element of the 3x3 matrix, but I cannot even cast &matrix[1][1] type as a int (*)[N] because I would take more elements than I can actually

#

let's say the 3x3 matrix is ((1,2,3),(4,5,6),(7,8,9))
if I have the address of matrix[1][1] which points to the element 5
I would basically have an array like this
[5,6,7,8,9]

vapid pollen
#

using [1][1] is wrong in all scenarios

#

you pass matrix or nothing

#

and yes it decays into pointer to array of lines

#

you have to pass always the full matrix, and the i,j or whatever you care about