In this example I’ll show you how to calculate determinant 2×2 ย of a matrix calculator in C++.
C++ Code: Calculate Determinant of Matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include<stdio.h> #include<stdlib.h> #define d 2 double det (double A[d][d], int N) { double c, r=1; for(int i = 0; i < N; i++) { for(int k = i+1; k < N; k++) { c = A[k][i] / A[i][i]; for(int j = i; j < N; j++) A[k][j]= A[k][j] - c*A[i][j]; } } for (int i = 0; i < N; i++) r *=A[i][i]; return r; } int main() { double M[d][d]; M[0][0]=9; M[0][1]=5; M[1][0]=3; M[1][1]=7; printf("Det(M) = %f\n",det(M,d)); return 0; } |
Output: