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 |
#include<iostream> using namespace std; int main() { int rows, columns, determinant, determMatrix[2][2]; cout << "\nPlease Enter the 2 * 2 Matrix Items\n"; for(rows = 0; rows < 2; rows++) { for(columns = 0; columns < 2; columns++) { cin >> determMatrix[rows][columns]; } } determinant = ((determMatrix[0][0] * determMatrix[1][1]) - (determMatrix[0][1] * determMatrix[1][0])); cout << "\nThe Determinant of 2 * 2 Matrix = " << determinant; return 0; } |
Code 2:
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: