C程序计算矩阵的逆(n * n) [英] C Program to calculate inverse of matrix (n*n)
本文介绍了C程序计算矩阵的逆(n * n)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。
我用C语言编写矩阵反转的代码。
但是在行列式函数中有错误说无法转换'浮动'到'float(*)[20]'为参数'1''浮动行列式(浮点数(*)[20])'
有谁知道什么是问题,帮帮我?
谢谢
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
/* 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]>=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 && 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];
}
推荐答案
看这里:
Look here:
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
返回 float
。 determinant
不接受 float
作为参数 - 你要么必须更改 minor
返回,或者将结果重新打包成 determinant
可以接受的内容。
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.
#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* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
}
这篇关于C程序计算矩阵的逆(n * n)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文