查找矩阵基础和维度的C ++程序
这是一个C++程序,用于查找矩阵的基础和维度。
算法
Begin
Function determinant() :
It calculates determinant of the matrix.
/*
Arguments:
n = number of elements.
matrix[10][10] = input matrix.
*/
declare the submatrix submatrix[10][10].
//函数主体:
if (n == 2)
return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]))
else
Make a for loop c = 0 to n-1
Declare and initialize submati = 0, submatj.
Make a For loop i = 1 to n-1
initialize subj = 0
Make a For loop i = 1 to n-1
if (j == c)
continue
submatrix[submati][submatj] = matrix[i][j].
Increment subj.
increment submati.
Compute d = d + (pow(-1, c) * matrix[0][c] * determinant(n- 1, submatrix)).
End示例
#include<iostream>
#include<math.h>
using namespace std;
double d = 0;
double determinant(int n, double matrix[10][10]) {
double submatrix[10][10];
if (n == 2)
return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] *
matrix[0][1]));
else {
for (int c = 0; c < n; c++) {
int submati = 0,submatj;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == c)
continue;
submatrix[submati][submatj] = matrix[i][j];
subj++;
}
submati++;
}
d = d + (pow(-1, c) * matrix[0][c] * determinant(n -
1, submatrix));
}
}
return d;
}
int main(int argc, char **argv) {
cout << "Enter the number of elements:\n";
int n;
cin >> n;
double matrix[10][10];
cout << "Enter elements one by one:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[j][i];
}
}
d = determinant(n, matrix); //call the function
if (d != 0)
cout << "The elements form the basis of R" << n << " as the determinant is non-zero";
else
cout << "The elements don't form the basis of R" << n << " as the determinant is zero";
}输出-1
Enter the number of elements: 3 Enter elements one by one: 7 6 1 2 3 4 5 8 9 The elements form the basis of R3 as the determinant is non-zero
输出-2
Enter the number of elements: 4 Enter the elements one by one: 7 6 1 4 2 3 5 4 9 8 2 3 2 1 3 0 The elements don't form the basis of R4 as the determinant is zero
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短