2

I am writing a program to calculate determinant of a matrix using pointers with size up to 3x3 and I've started with writing the formula for 2x2 matrix.

I got an error and it displayed "expression must have arithmetic type" in places of the parentheses at the beginnings of multiplied expressions.

It seems that the program recognize the values as pointers instead of just multiplying values, but I'm not sure either. How do I fix it?

void determinant(int size, int matrix[][MAXSIZE])
{
    int d;
    if(size == 2)
    {
        d = matrix * ((matrix + 1) + 1) - ((matrix + 1) + 0) * ((matrix + 0) + 1);
        printf("Determinant of your matrix: %d\n", d);
    }
}
Sin Halloween
  • 63
  • 1
  • 8
jula
  • 25
  • 5
  • The problem starts with `d = matrix * ...`, where you meant to access the first element of the matrix, like: `d = (**matrix) * (*(*(matrix+1) + 1)) ...` – uv_ Dec 15 '18 at 15:34

2 Answers2

2

Why not just matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1]?

You should probably pass a more type-safe argument than int matrix[][] to the function, though (e.g. create some kind of a struct MATRIX_2_2).

For the sake of exercise, if you really want to use pointer arithmetics, you'd probably want to write something like:

d = (**matrix) * (*(*(matrix + 1) + 1)) - (*(*(matrix + 0) + 1)) * (*(*(matrix + 1) + 0));

Here, every first de-reference (that's what an asterisk is) gets a 1-D array of the matrix, and the second one gets a specific value.

uv_
  • 746
  • 2
  • 13
  • 25
  • Can I ask you one more question? Because this formula is not working right, it doesn't return right values. I thought I could access elements of this matrix like instead of matrix[1][1] I wrote (*(matrix + 1) +1), but it's not reaching this element. Do I have to access them with a nested loop and use expressions like (*(*matrix + i) + j)? Because if yes, then I have no idea how to change that formula. – jula Dec 15 '18 at 16:24
  • You need to see two (2) asterisks in every of the four expressions, like this: `(*(*(matrix + 1) + 1))`. Are you sure you wrote that right? It should work... – uv_ Dec 15 '18 at 16:25
  • d = (**matrix) * (*(*(matrix + 1) +1 )) - (*(*(matrix + 1) +0)) * (*(*(matrix + 0) + 1)); – jula Dec 15 '18 at 16:40
  • And for example for a matrix 2 4 1 2 it returns that the determinant is -2 – jula Dec 15 '18 at 16:42
1

The question of computing the determinant is a basic math question (totally unrelated to C) and most linear algebra courses would explain how to do that. You actually need to understand how you would compute that "by hand".

A different question is how to represent matrixes (as an abstract data type) in a C program. I would suggest using flexible array members (and pointers to struct-s containing them), and I detailed more how to do that in this answer (which of course you need to complete for your needs).

By combining both approaches (the math side and the programming side) you can complete your homework.

Don't forget to enable all warnings and debug info when compiling (e.g. compile using gcc -Wall -Wextra -g with GCC; see also the advice given here), and read How to debug small programs.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547