IHateEngineering
IHateEngineering

Reputation: 9

Declare two dimensional array of type double?

I was provided a function listed below that I have to create code for, to find the determinant of a 1x1, 2x2, or 3x3 matrix.

double det (int size, double matrix[size][size]) {

It calls for a two dimensional array of type double, as a beginner I am unfamiliar with this and not sure how to declare it. I looked online and couldn't find anything. My ideas were int[][] but nothing is working. Any Ideas? Using C btw, thank you!

For example, if I use

int main() {
double test[2][2] = {{1.0,2.0},{2.0,3.0}};
det(2, test);
return(0);}

I attempted to declare it with

double det (int size, double matrix[size][size]) {
double matrix[size][size];

however I get the error

/tmp/tmpwhrzhcqq.c: In function ‘det’:
/tmp/tmpwhrzhcqq.c:9:10: error: ‘matrix’ redeclared as different kind of symbol
9 |   double matrix[size][size];
  |          ^~~~~~
/tmp/tmpwhrzhcqq.c:8:30: note: previous definition of ‘matrix’ was here
8 | double det (int size, double matrix[size][size]) {
  |                       ~~~~~~~^~~~~~~~~~~~~~~~~~

Upvotes: 0

Views: 777

Answers (1)

chux
chux

Reputation: 153508

Remove unneeded double matrix[size][size]; declaration. matrix in the function parameter list is enough.

If code needs another matrix, use a new name.

Note: Code is using the VLA feature, available in C99 and some later compilers.

double det (int size, double matrix[size][size]) {
  // double matrix[size][size];
  double some_other_matrix[size][size];
  double d = 0.0;
  for (int row = 0; row < size; row++) { 
    for (int vol = 0; col < size; col++) { 
      ; // your det code
    }
    ; // your det code
  }
  return d;
}

Upvotes: 1

Related Questions