The inverse matrix of matrices and the recursive algorithm of determinant values.

Source: Internet
Author: User

It's been a long time since I wrote C, and today I wrote a matrix algorithm.

The complete code is as follows:

#include <atlstr.h>#include<iostream>#include<string>using namespacestd;//Create a matrixfloat**creat (intN) {    float**array=New float*[n];  for(intI=0; i<n;i++) {Array[i]=New float[n]; } printf ("Please enter the matrix: \ n");  for(intI=0; i<n;i++)    {         for(intj=0; j<n;j++) {cin>>Array[i][j]; }    }    returnArray;}//to find the determinant value:floatValue (float**array,intN) {    floatresult=0; if(n==1)returnarray[0][0]; float**temp=New float*[n-1];  for(intI=0; i<n-1; i++) {Temp[i]=New float[N-1]; }     for(intI=0; i<n;i++)    {         for(intj=0; j<n-1; j + +)        {             for(intk=0; k<n-1; k++)            {                intFlag; if(j<i) flag=0; Elseflag=1; TEMP[J][K]=array[j+flag][k+1]; }        }        intflag2=-1; if(i%2==0) flag2=1; Result+=flag2*array[i][0]*value (temp,n-1); }    returnResult;}//Inverse matrix: The value of the determinant of the adjoint matrix divided by the determinantfloat**getmarin (float**array,intN) {    float**resultmarin=Array; floatresultsum=Value (array,n); float**temp=New float*[n-1];  for(intI=0; i<n-1; i++) {Temp[i]=New float[N-1]; }     for(intI=0; i<n;i++)    {         for(intj=0; j<n;j++)        {             for(intk=0; k<n-1; k++)            {                 for(intm=0; m<n-1; m++)                {                    intflag1=0; intFlag2=0; if(k<i) flag1=0; Elseflag1=1; if(M&LT;J) flag2=0; ElseFlag2=1; TEMP[K][M]=array[k+flag1][m+Flag2]; }            }            intflag3=-1; if((i+j)%2==0) flag3=1; Resultmarin[j][i]=(float) Flag3*value (temp,n-1)/resultsum; }    }    returnResultmarin;}voidOutPut (float**array,intN) {printf ("inverse matrix: \ n");  for(intI=0; i<n;i++)    {         for(intj=0; j<n;j++) {printf ("%f", Array[i][j]); } printf ("\ n"); }}voidTest () {printf ("Please enter the order of the Matrix you want to enter:"); intN; CIN>>N; float**array=creat (n); floatresult=Value (array,n); printf ("determinant Value =%f\n", Result); if(result==0) printf ("The matrix has no inverse matrix \ n"); Else    {      float**resultmarin=Getmarin (array,n);    OutPut (Resultmarin,n); } Test ();}voidMain () {Test ();}

Operation Result:

The inverse matrix of matrices and the recursive algorithm of determinant values.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.