I’m trying to reduce the matrix to triangular form.
On
input
4
10 23 17 44 25
15 35 26 69 40
25 57 42 108 65
30 69 51 133 95
// at the entrance takes the size of the matrix
# define _CRT_SECURE_NO_WARNINGS
#Include & lt; iostream & gt;
Using Namespace STD;
INT MAIN () {
freopen ( "input.txt", "r", stdin);
freopen ( "output.txt", "w", stdout);
int n;
CIN & GT; & GT; n;
int m = n + 1; // Size matrritsy with vektorm b [n] [m]
double a [4] [5];
For (int i = 0; i & lt; n; i ++) {
for (int j = 0; j & lt; m; j ++)
CIN & GT; & GT; a [i] [j];
}
//// check that the first element of the first column is not equal to 0
for (int i = 0; i & lt; n-1; i ++) {
for (int i = 0; i & lt; n - 1; i ++) {
if (a [i] [0] == 0) {
for (int y = 0; y & lt; m; y ++) {
int z = a [i] [y];
a [i] [y] = a [i + 1] [y];
a [i + 1] [y] = z;
}
}
}
}
For (int i = 0; i & lt; n; i ++) {
for (int j = 0; j & lt; m; j ++)
COUT & LT; & LT; A [i] [j] & lt; & lt; "";
COUT & LT; & LT; Endl;
} // derived matrix
// COUT & LT; & LT; Endl;
// give a triangular form
for (int i = 0; i & lt; n - 1; i ++) {
{
if (a [i] [i]! = 0) {
for (int k = i; k & lt; n - 1; k ++) {
float s = a [k + 1] [i] / a [k] [i];
for (int x = i; x & lt; m; x ++) {
float c = a [k] [x] * (-1) * s;
a [k + 1] [x] = a [k + 1] [x] + c;
}
}
}
}
}
COUT & LT; & LT; Endl;
For (int i = 0; i & lt; n; i ++) {
for (int j = 0; j & lt; m; j ++)
COUT & LT; & LT; A [i] [j] & lt; & lt; "";
COUT & LT; & LT; Endl;
} // derived matrix
}
Answer 1, Authority 100%
Because you divide by 0.
Record
assert (a [k] [i] = 0!);
before
float s = a [k + 1] [i] / a [k] [i];
and see for yourself …
This is not surprising – after all the matrix
October 23 17 44
15 35 26 69
25 57 42 108
30 69 51 133
degenerate, its determinant is equal to 0 …
p.s. Something strange you have a nested loop
for (int i = 0; i & lt; n-1; i ++) {
for (int i = 0; i & lt; n - 1; i ++) {
No? i
in the nested loop hides i
from the external, and further doubt … int z = a [i] [y];
is also somehow not comme il faut 🙂 – why double
to int
cut
?
But the main thing – what is the point in the inspections before conversions? It is in the course of their regular support member can be zero! Take a simple matrix-type
January 1
eleven
or manually, make sure that your first check is working, but after the first iteration of the transformation you get zero second line – and no checks such troubles you have no
.