Click here to Skip to main content
15,867,870 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi everybody
I write this code for Inverse of matrix in C language .
but there is error in determinant function that say "can not convert 'float' to 'float(*)[20]' for argument '1' to 'float determinant(float(*)[20])' "

does anyone know what is problem , to help me?
thank you


C#
/* a program to calculate inverse of matrix (n*n)*/
//actually one of the way to calculate inverse of matrix is : A^(-1) = 1/|A| * C(t)     that A is matrix and c(t) is taranahade A

#include <stdio.h>;
#include <conio.h>;
#include <string.h>;
#include <stdlib.h>;

const int max=20;
int i , j , n , k , size=0 , row , column ;
float num , det=0 , inverse_matrix[max][max] , matrix[max][max] , new_mat[max][max] , m_minor[max][max] , m_Transpose[max][max];

float determinant(float matrix[max][max]);
float minor(float matrix[max][max],int k);
float Transpose(float matrix[max][max]);

int main()
{
      printf("\n What is degree of your matrix?");
      scanf("%d",&n);
      printf("\n Please entre your matrix's rooms:");

      for(i=1;i<=n;i++)
      {
         for(j=1;j<=n;j++)
         {
             printf("\n\t[ %d , %d ] = ",i,j);
             scanf("%d",&matrix[i][j]);
         }
         printf("\n");
      }
      size=n;
      det=determinant(matrix);

      if(det==0)
      {
          printf("\n\t* * * * * * * * * * * * * * * * * * * * * * * *\n\tINVERSE DOESN'T EXSIT");
          getch();
          return 0;
      }
      else
      {
          num=1/det;
          m_Transpose[n][n]=Transpose(matrix);
          printf("\n\t* * * * * * * * * * * * * * * * * * * * * * * *\n\tMATRIX REVERSE IS:\n\n\t");
          /*complex of determinant with Transpose*/
          for(i=1;i<=n;i++)
          {
               for(j=1;j<=n;j++)
               {
                    inverse_matrix[i][j]=num*m_Transpose[i][j];
                    if(inverse_matrix[i][j]&gt;=0)
                        printf("     %d",inverse_matrix[i][j]);
                    else
                        printf("     %d",inverse_matrix[i][j]);
               }
               printf("\n\t");
          }
      }


      getch();
      return 0;
}

/* calculate determinate of matrix */
float determinant(float matrix[][max])
{
      if(n==1)
             return matrix[1][1];
      else if(n==2)
           return (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]);
      else
      {
          for(k=1;k<=n;k++)
          {
              det+=((-1)^(1+k))*matrix[1][k]*determinant(minor(matrix,k)); //error
              n=size;
          }
          return det;
      }
}

/*calculate minor of matrix and return to determinant's function*/
float minor(float matrix[][max],int k)
{
      int m=1 , p , r , c , row=1 , column;
      column=k;
      for(r=2;r<=n;r++)
      {
          p=1;
          for(c=1;c<=n;c++)
          {
              if(r!=row &amp;&amp; c!=column)
              {
                  new_mat[m][p]=matrix[r][c];
                  p++;
              }
          }
          if(r!=row)
             m++;
      }
      n--;
      return new_mat[m][p];

}

/*calculate Transpose*/
float Transpose(float matrix[][max])
{
    for(int i=1;i<=n;i++)
       for(j=1;j<=n;j++)
          m_Transpose[i][j]=matrix[j][i];
    return m_Transpose[n][n];
}
Posted
Updated 20-Sep-19 6:38am
Comments
Sergey Alexandrovich Kryukov 3-Apr-14 13:24pm    
Apparently you cannot :-). At least add a comment on the line where you do it. The compiler tells you that.
—SA

Look here:
C++
float determinant(float matrix[][max])
...
det+=((-1)^(1+k))*matrix[1][k]*determinant(minor(matrix,k)); //error
...
float minor(float matrix[][max],int k)
minor returns a float. determinant does not accept a float as it's parameter - you will either have to change what minor returns, or re-package the result into something determinant can accept.
 
Share this answer
 
Comments
Diba.S 3-Apr-14 13:34pm    
thank you .
but what do you mean?
minor return float new_mat[][] and determinant's input is float matrix[][]
OriginalGriff 3-Apr-14 13:50pm    
Are you sure?
Look at the code again... :laugh:
And we all do it: we read what we meant to write...
Diba.S 3-Apr-14 14:00pm    
:|ok,i see what you say but can't understand well.
so,what about this algorithm?:
am i understand problem (and fix it) by this way?!

float det1=((-1)^(1+k))*matrix[1][k];
new_mat[n][n]=minor(matrix,k);
float det2=determinant(new_mat);
det+=det1*det2;

anyway this way has problem too! for all matrix det==0 and show inverse doesn't exist !
C++
#include<stdio.h>
#include<math.h>
#include<conio.h>

float determinant(float[25][25],float);
void cofactor(float[25][25],float);
void transpose(float[25][25],float[25][25],float);

int main()
{
    float matrix[25][25],size,d;
    int i,j;

    printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
    printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
    printf("\n* * * * * *                                                       * * * * * * *");
    printf("\n* * * * * * THIS PROGRAM CALCULATE THE REVERSE OF SQUARE MATRIX ! * * * * * * *");
    printf("\n* * * * * *                                                       * * * * * * *");
    printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
    printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
    printf("\n\n  1 - Enter size of Matrix : ");
    scanf("%f",&size);
    printf("\n  2 - Enter the elements of  Matrix : \n");
    for (i=0;i<size;i++)>
    {
        for (j=0;j<size;j++)>
        {
             printf("\n\t[ %d , %d ] = ",i,j);
             scanf("%f",&matrix[i][j]);
        }
        printf("\n");
    }
    d=determinant(matrix,size);
    printf("\n\n    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n\n\tDeterminant of the Matrix = %6.2f",d);
    if (d==0)
    {
        printf("\n\n    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n\n\tInverse not exsist\n\n");
        printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        printf("\n* * * * * * * * * * * * * * * * * THE END * * * * * * * * * * * * * * * * * * *");
        printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
    }
    else
        cofactor(matrix,size);
    getch();
}
 
/*For calculating Determinant of the Matrix . this function is recursive*/
float determinant(float matrix[25][25],float size)
{
    float s=1,det=0,m_minor[25][25];
    int i,j,m,n,c;
    if (size==1)
    {
        return (matrix[0][0]);
    }
    else
    {
        det=0;
        for (c=0;c<size;c++)>
        {
            m=0;
            n=0;
            for (i=0;i<size;i++)>
            {
                for (j=0;j<size;j++)>
                {
                    m_minor[i][j]=0;
                    if (i != 0 && j != c)
                    {
                       m_minor[m][n]=matrix[i][j];
                       if (n<(size-2))
                          n++;
                       else
                       {
                           n=0;
                           m++;
                       }
                    }
                }
            }
            det=det + s * (matrix[0][c] * determinant(m_minor,size-1));
            s=-1 * s;
        }
    }
 
    return (det);
}
 
 /*calculate cofactor of matrix*/
void cofactor(float matrix[25][25],float size)
{
     float m_cofactor[25][25],matrix_cofactor[25][25];
     int p,q,m,n,i,j;
     for (q=0;q<size;q++)>
     {
         for (p=0;p<size;p++)>
         {
             m=0;
             n=0;
             for (i=0;i<size;i++)>
             {
                 for (j=0;j<size;j++)>
                 {
                     if (i != q && j != p)
                     {
                        m_cofactor[m][n]=matrix[i][j];
                        if (n<(size-2))
                           n++;
                        else
                        {
                            n=0;
                            m++;
                        }
                     }
                 }
             }
             matrix_cofactor[q][p]=pow(-1,q + p) * determinant(m_cofactor,size-1);
         }
     }
     transpose(matrix,matrix_cofactor,size);
}

/*Finding transpose of cofactor of matrix*/ 
void transpose(float matrix[25][25],float matrix_cofactor[25][25],float size)
{
     int i,j;
     float m_transpose[25][25],m_inverse[25][25],d;
 
     for (i=0;i<size;i++)>
     {
         for (j=0;j<size;j++)>
         {
             m_transpose[i][j]=matrix_cofactor[j][i];
         }
     }
     d=determinant(matrix,size);
     for (i=0;i<size;i++)>
     {
         for (j=0;j<size;j++)>
         {
             m_inverse[i][j]=m_transpose[i][j] / d;
         }
     }
     printf("\n\n\t* * * * * * * * * * * * * * * * * * * * * * * \n\n\tThe inverse of matrix is : \n\n");
 
     for (i=0;i<size;i++)>
     {
         for (j=0;j<size;j++)>
         {
             printf("\t%3.2f",m_inverse[i][j]);
         }
         printf("\n\n");
     }
     printf("\n\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
     printf("\n* * * * * * * * * * * * * * * * * THE END * * * * * * * * * * * * * * * * * * *");
     printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
}
 
Share this answer
 
v2
Comments
Member 12480890 9-Jun-16 4:07am    
i am making this program in c++.
and i made a class named matrix but there is some problem with the return of determinant and i can't figure out what this is my code.
Member 12480890 9-Jun-16 4:11am    
my code is too big to be posted here.Can someone please send me there email id.
for contacting me my email id is:
junirobert21@gmail.com

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900