三九宝宝网宝宝教育教学论文

求矩阵的逆用C语言编程

12月25日 编辑 39baobao.com

[国家二级C语言编程]计算机考试历年二级C语言上机编程题分析 ============================================== 1、请编一个函数fun,它的功能是:根据以下公式求π的值(要求满足精度0.0005,即,某项小...+阅读

算法的大致思想是通过行列式初等变换来求。 代码如下: private double[,] ReverseMatrix( double[,] dMatrix, int Level ) { double dMatrixValue = MatrixValue( dMatrix, Level ); if( dMatrixValue == 0 ) return null; double[,] dReverseMatrix = new double[Level,2*Level]; double x, c; // Init Reverse matrix for( int i = 0; i < Level; i++ ) { for( int j = 0; j < 2 * Level; j++ ) { if( j < Level ) dReverseMatrix[i,j] = dMatrix[i,j]; else dReverseMatrix[i,j] = 0; } dReverseMatrix[i,Level + i ] = 1; } for( int i = 0, j = 0; i < Level && j < Level; i++, j++ ) { if( dReverseMatrix[i,j] == 0 ) { int m = i; for( ; dMatrix[m,j] == 0; m++ ); if( m == Level ) return null; else { // Add i-row with m-row for( int n = j; n < 2 * Level; n++ ) dReverseMatrix[i,n] += dReverseMatrix[m,n]; } } // Format the i-row with "1" start x = dReverseMatrix[i,j]; if( x != 1 ) { for( int n = j; n < 2 * Level; n++ ) if( dReverseMatrix[i,n] != 0 ) dReverseMatrix[i,n] /= x; } // Set 0 to the current column in the rows after current row for( int s = Level - 1; s > i;s-- ) { x = dReverseMatrix[s,j]; for( int t = j; t < 2 * Level; t++ ) dReverseMatrix[s,t] -= ( dReverseMatrix[i,t]* x ); } } // Format the first matrix into unit-matrix for( int i = Level - 2; i >= 0; i-- ) { for( int j = i + 1 ; j < Level; j++ ) if( dReverseMatrix[i,j] != 0 ) { c = dReverseMatrix[i,j]; for( int n = j; n < 2*Level; n++ ) dReverseMatrix[i,n] -= ( c * dReverseMatrix[j,n] ); } } double[,] dReturn = new double[Level, Level]; for( int i = 0; i < Level; i++ ) for( int j = 0; j < Level; j++ ) dReturn[i,j] = dReverseMatrix[i,j+Level]; return dReturn; } private double MatrixValue( double[,] MatrixList, int Level ) { double[,] dMatrix = new double[Level, Level]; for( int i = 0; i < Level; i++ ) for( int j = 0; j < Level; j++ ) dMatrix[i,j] = MatrixList[i,j]; double c, x; int k = 1; for( int i = 0, j = 0; i < Level && j < Level; i++, j++ ) { if( dMatrix[i,j] == 0 ) { int m = i; for( ; dMatrix[m,j] == 0; m++ ); if( m == Level ) return 0; else { // Row change between i-row and m-row for( int n = j; n < Level; n++ ) { c = dMatrix[i,n]; dMatrix[i,n] = dMatrix[m,n]; dMatrix[m,n] = c; } // Change value pre-value k *= (-1); } } // Set 0 to the current column in the rows after current row for( int s = Level - 1; s > i;s-- ) { x = dMatrix[s,j]; for( int t = j; t < Level; t++ ) dMatrix[s,t] -= dMatrix[i,t]* ( x/dMatrix[i,j] ); } } double sn = 1; for( int i = 0; i < Level; i++ ) { if( dMatrix[i,i] != 0 ) sn *= dMatrix[i,i]; else return 0; } return k*sn; } 调用如下: double[,] dMatrix = new double[3,3]{{0,1,2},{1,0,1},{4,2,1}}; double[,] dReturn = ReverseMatrix( dMatrix, 3 ); if( dReturn != null ) { for( int i=0; i < 3; i++ ) Debug.WriteLine( string.Format( "{0} {1} {2}", dReturn[i,0], dReturn[i,1],dReturn[i,2] ) ); }

以下为关联文档:

C语言编程如何自动生成一个二维数组程序主要通过malloc函数动态生成数组,srand和rand函数配合生成随机数据,代码如下, //程序功能,实现自定义m*n二维数组,随机生成给定范围max-min的数组元素 #include#include #inc...

二维数组指针 C语言编程 #include int main() { int a[2][5] = {{1,2,6,3,0},{10,20,40,60,80}}; int i,flag,order,value; while(scanf("%d",&order) == 1) { flag = 1; for(i = 0; i if(order == a...

C语言编程求两个3 3二维数组的和展开全部 int ArraySun(char* arr, int N) { int sum = 0; for(int i = 0 ; i < N; i++) { for(int j= 0; j< N;j++) { sum += arr[i*N +j]; } } return sum; } 大致这个样...

求C语言编程:调用函数计算二维数组所有元素的平均值两个for循环 记录二维数组的总值 count+=array[i][j] key记录数组元素个数 key++; 最后 return count/key 或: double avg(int x[10][10]) { int i,j,sum; for(i=0;i<10;i++)...

c语言编程将二维数组a23中的元素按顺序放入一维数组b6中二维 #include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, ch...

C语言求矩阵的逆去文库,查看完整内容> 内容来自用户:zhangbincehui #include #include void jiafa() { int m,n; float a[20][20],b[20][20],c[20][20]; int i,j; printf("请输入矩阵行数:"); scanf...

矩阵求逆c语言Gauss Jordan Elimination Algorithm (高斯消除法) int InverseMatrix_GaussianJordan(const float** &fMat, float **&invMat) { int k, l, m, n; int iTemp; float dTemp; f...

c语言求逆矩阵#include <vector>#include <iomanip>#include <cmath>#include <iostream>using namespace std;double det(int N,vector<double> A){ double D=0; vector<double> B((N-1)...

用C语言编写矩阵求逆的程序1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 int a[4][4]; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ a[i][j] = i*4+j+1; } } fo...

推荐阅读
图文推荐