4 Replies - 672 Views - Last Post: 14 September 2010 - 03:46 AM Rate Topic: -----

#1 akwarthan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 09

Help with Determinants

Posted 13 September 2010 - 11:13 PM

I'm writing a code for my Computational Linear Algebra course that, among other things, computes the determinant of a matrix.

double det(double matrix[][], int bound)
{
    double ans = 0;
    if(bound == 2)
    {
        return matrix[1][1] * matrix[2][2] -
                matrix[1][2] * matrix[2][1];
    }

    double mminor[bound][bound];
    int k = 0;
    for(int i = 1; i < bound + 1; i++){
        for(int j = 0; j < bound; j++, k++){
            if(j!=i)
            mminor[i][k] = matrix[i][j];
        }
        ans += (-1 ^ (i+1)) * matrix[1][i]
                * det(mminor, bound - 1);
    }

    return ans;
}


When I compile, I get this error message:

Quote

C:\Users\Andrew\Documents\MATH365\hw3.cpp|32|error: cannot convert `double (*)[((unsigned int)((int)bound))]' to `double (*)[8]' for argument `1' to `double det(double (*)[8], int)'|


So, is there any (easy) way I can remedy this problem? Preferably without having to go back and rewrite my whole program using a Matrix class?

edit:
I should mention that I know all matrices will be 8x8.

This post has been edited by akwarthan: 13 September 2010 - 11:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Determinants

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Help with Determinants

Posted 13 September 2010 - 11:51 PM

This double matrix[][] parameter may be your problem. You can not leave columns section empty there. You have to give a constant value to that part of the matrix. And if you know all matrices are of 8x8 then why not give parameter as double matrix[8][8]? or if you still want to use it without those row column values there, you can simply give a double pointer in there and treat it like a 2D array.

This post has been edited by AmitTheInfinity: 13 September 2010 - 11:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 akwarthan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 09

Re: Help with Determinants

Posted 14 September 2010 - 12:21 AM

I tried both
double det(double matrix[8][8], int bound)
and
double det(double *matrix, int bound)
.

Both still result in the same error message mentioned previously, pointing to
det(mminor, bound - 1)
:(
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Help with Determinants

Posted 14 September 2010 - 02:21 AM

The problem with double mminor[bound][bound]; is that you are using a variable to define the bounds of an array. You have to define the indexes using the constant value only, you can not use variable value to define an array (columns part in case of 2D array specially).
Was This Post Helpful? 1
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Help with Determinants

Posted 14 September 2010 - 03:46 AM

You have some problems. Some big problems actually. First of all, ^ operator does NOT stand for power, in C/C++. That means, (-1 ^ (i+1)) is not equivalent with -1i + 1. ^ is bitwise XOR, and does a completely different job. Use pow() from cmath instead.
Secondly, don't just guess code. double det(double *matrix, int bound) - wait a moment, matrix is supposed to be a 2D array, yet you try passing it as a single pointer. Now think about this recursive call: det(mminor, bound - 1); - so you recursively pass mminor to your function, but yet you always re-declare mminor: double mminor[bound][bound];, before the recursive call - is that right? And there aren't many Compilers that support VLAs.

This post has been edited by sarmanu: 14 September 2010 - 03:47 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1