Inverse matrix

Hi everyone,
I wrote the code to inverse matrix. However, the result have some problems.
How to solute the problems?
Thank u too much

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<fstream>
#include<ostream>

using namespace std;

int nhapmatran(float, int);
int xuatmatran(float, int);
float dinhthuc(float, int);
float nghichdao(float, int);

int nhapmatran(float a[100][100], int n)
{
	int i,j;
	for (i=0; i<n; i++){
	    for (j=0; j<n; j++){
		cin>>a[i][j];
	    }
	}
}

int xuatmatran(float a[100][100], int n)
{	
	int i,j;
	
	for (i=0; i<n; i++){
		cout<<endl;
	    for (j=0; j<n; j++){
		cout<<a[i][j]<<"\t";
	    }
	}
}

float dinhthuc(float a[100][100], int n)
{	
	float b[100];
	float c[100];
	int i,j,k,kt, dem=0;
	float h;
	float dt=1;

		for (i=0; i<n-1; i++){
		if(a[i][i]==0){
			kt =0;
		for (j=0;j<n;j++){
			if(a[i][j]!=0){
				for(k=0;k<n;k++){
					c[k] = a[k][i];
					a[k][i]=a[k][j];
					a[k][j]=c[k];
				}
			dem++;
			kt++;
			break;
			}
		}
		}
		if(kt==0) return 0;
		b[i]=a[i][i];
		
	for (j=0; j<n; j++) a[i][j]/=b[i];
	for (j=i+1; j<n; j++){
		h = a[j][i];
	for (k=0;k<n;k++) a[j][k]=a[j][k]-h*a[i][k];
	}
	}

	b[n-1]=a[n-1][n-1];
		for(i=0; i<n; i++)
			dt*=b[i];
			//cout<<kq<<endl;
	if (dem%2==0) return dt;
	else return -dt;
	//xuatmatran(a,n);
}

float nghichdao(float a[100][100], int n)
{
	
	float ratio,t,k;
	int i,j,l;

	k = dinhthuc(a,n);
	

    for(i = 0; i < n; i++){
        for(j = n; j < 2*n; j++){
            if(j==(i+n))
                a[i][j] = 1.0;
            else
                a[i][j] = 0.0;
        }
    }

    for (i=0; i<n; i++){
	if (a[i-1][1]<a[i][1])
	for (j=1; j<=2*n; j++){
		ratio=a[i][j];
		a[i][j]=a[i][j];
		a[i][j]=ratio;
	}
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(i!=j){
                ratio = a[j][i]/a[i][i];
                for(l = 0; l < 2*n; l++){
                    a[j][l] -= a[i][l]*ratio;
                }
            }
        }
    }
    for(i = 0; i < n; i++){
        ratio = a[i][i];
        for(j = 0; j < 2*n; j++){
            a[i][j] /=ratio;
        }
    }
  
    for(i = 0; i < n; i++){
	  // cout<<endl;
        for(j = n; j < 2*n; j++){
            cout<< a[i][j];
            cout<<"\t";
        }
	cout<<endl;
    }
	return 0;
	//else cout<<"ERROR!"<<endl;
}
	
int main()
{	
	float a[100][100];
	float b[100][100];
	int n;
	
	cout<<"Ma tran loai: "<<endl;
	cin>>n;	
	cout<<"Nhap phan tu ma tran"<<endl;
	nhapmatran(a,n);
	
	cout<<"Ket qua"<<endl;
	xuatmatran(a,n);

	cout<<"\nDinh thuc"<<endl;
	cout<<dinhthuc(a,n);
	cout<<endl;

	cout<<"\n Ma tran nghich dao"<<endl;
	nghichdao(a,n);
	cout<<endl;

	
}
Hi,

There were some warnings when I compiled with cpp.sh (with all 3 warnings on):

In function 'int nhapmatran(float (*)[100], int)': 
23:1: warning: no return statement in function returning non-void [-Wreturn-type] 
In function 'int xuatmatran(float (*)[100], int)': 
35:1: warning: no return statement in function returning non-void [-Wreturn-type] 
In function 'float nghichdao(float (*)[100], int)': 
83:14: warning: unused variable 't' [-Wunused-variable] 
83:16: warning: variable 'k' set but not used [-Wunused-but-set-variable] 
In function 'int main()': 138:8: warning: unused variable 'b' [-Wunused-variable]


However they don't look as they would cause too many problems. I would fix them though: if there are warnings - one isn't finished :+) always set the warning level as high as you can, warnings are your friend. On g++ or clang use at least -Wall -Wextra -pedantic-errors

I use these ones as well:
http://www.cplusplus.com/forum/general/183731/#msg899203


If it turns out to be a runtime problem, then try to use a debugger, hopefully there is a GUI one in your IDE. This is by far the easiest thing to do, it will save you days of staring at code :+)

Good Luck !!
Lots of good stuff here, written by Bjarne Stroustrup & Herb Sutter:

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html#main
Topic archived. No new replies allowed.