-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.0.c
81 lines (67 loc) · 2.1 KB
/
lab2.0.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
grgtg
*/
*
// Function to create a 3x3 identity matrix
void create_identity_matrix(float matrix[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = (i == j) ? 1.0 : 0.0;
}
}
}
// Function to calculate the determinant of a 3x3 matrix
float determinant(float matrix[3][3]) {
float det = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) -
matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) +
matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
return det;
}
// Function to create Dxyz matrices
void create_dxyz_mat(float matrix[3][3], float dxyz_mat[3][3], int col, float value) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
dxyz_mat[i][j] = (j == col) ? value : matrix[i][j];
}
}
}
// Function to find the inverse of a 3x3 matrix using Cramer's Rule
void inverse_cramers_rule(float matrix[3][3], float inverse[3][3]) {
float det = determinant(matrix);
if (det == 0) {
printf("The matrix is singular and has no inverse.\n");
return;
}
float dxyz_mat[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
create_dxyz_mat(matrix, dxyz_mat, j, matrix[i][j]);
inverse[j][i] = determinant(dxyz_mat) / det;
}
}
}
int main() {
float matrix[3][3];
float inverse[3][3];
// Ask for the elements of matrix A
printf("Enter the elements of the 3x3 matrix A:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%f", &matrix[i][j]);
}
}
// Calculate the inverse using Cramer's Rule
inverse_cramers_rule(matrix, inverse);
// Print the inverse matrix
printf("The inverse of the matrix A is:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%.2f ", inverse[i][j]);
}
printf("\n");
}
return 0;
}