Write a program to find inverse of a matrix


Default
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Write a program to find inverse of a nxn matrix
// Author: AimForJob.com
 
#include
#include
int determinant(int m, int a[][]);
int main(){
     int n,i,j;
     printf("Enter order of matrix:");
     scanf("%d",&n);                 // matrix row size input from user
     int matrix[n][n];
    
     // user input for matrix elements
     printf("Enter Matrix Elements\n");
     for(i=0;i<n;i++){
             for(j=0;j<n;j++){
                     scanf("%d",&matrix[i][j]);
             }
     }
     for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                     printf("%d ",matrix[i][j]);
            }
            printf("\n");
     }
     double det = determinant(n,matrix);
     printf("Determinant :%.2f\n",det);               // calling determinant function
       // calculating transpose of given matrix
     printf("Transpose Matrix:\n");
     int transpose_matrix[n][n];
     for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                  transpose_matrix[i][j] = matrix[j][i];
                  printf("%d ",transpose_matrix[i][j]);
            }
            printf("\n");
     }
    
     // calculating adjacent matrix
     printf("Adjacent Matrix:\n");
     int adjacent_matrix[n][n];
     int s=0,t=0,z,k;
     for (z=0;z<n;z++){
            for (i=0;i<n;i++){
                int b[n-1][n-1];
                for(j=0;j<n;j++){
                    if(j!=z){
                        for(k=0;k<n;k++){
                            if(k!=i){
                                b[s][t] = transpose_matrix[j][k];  
                                t++;
                            }
                        }
                        s++;
                   }
                    t = 0;
                }
                s = 0;
                adjacent_matrix[z][i] = (int)pow(-1,i+z)*determinant(n-1,b);
                printf("%d ",adjacent_matrix[z][i]);
            }
            printf("\n");
     }
     // calculating inverse matrix
     double inverse_matrix[n][n];
     printf("Inverse Matrix:\n");
     for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                  inverse_matrix[i][j] = adjacent_matrix[i][j]/det;
                  printf("%.2f ",inverse_matrix[i][j]);
            }
            printf("\n");
     }
     getch();
}
 
// defining det function for any nxn matrix
 
int determinant(int m, int a[m][m]){
     if(m==1){
  return a[0][0];
     }
     else if(m==2){
             return a[0][0]*a[1][1]-a[0][1]*a[1][0];
     }
     else{
             int det = 0,s=0,t=0,i,j,k,x,y;
             for (i=0;i<m;i++){
                 int b[m-1][m-1];
                 for(j=0;j<m;j++){
                     if(j!=0){
                         for(k=0;k<m;k++){
                             if(k!=i){
                                 //printf("%d %d %d %d %d\n",i,j,k,s,t);
                                 b[s][t] = a[j][k];    
                                 t++;
                             }
                         }
                     s++;
                     }
                    t = 0;
                }
                s = 0;
                det = det+(int)pow(-1,i)*a[0][i]*determinant(m-1,b);
            }
            return det;  
     }

No comments:

Post a Comment