Tell me please where I messed up in addressing with matr?

(code corrected)

#include <time.h> #include <stdio.h> #include <stdlib.h> void scan_matr(int ***matr, int *n){ printf("Введите порядок матрицы:"); scanf("%i", n); printf("\nВведите матрицу:\n"); matr = (int**)malloc(*n * sizeof(int*)); for (int i = 0; i < *n; ++i) { matr[i] = (int*)malloc((*n+1) * sizeof(int)); } for (int i = 0; i < *n; ++i) { for (int j = 0; j < *n+1; ++j) { scanf("%i",&(matr[i][j])); } puts(""); } } int* solveGauss(int **matr, int *n) { int *vectors; vectors = malloc(*n * sizeof(int)); for (int k = 1; k < *n; ++k) { for (int j = k; j < *n; ++j) { int a = matr[j][k-1]; int b = matr[k-1][k-1]; int m = a/b; for (int i = 0; i < *n+1; ++i) { matr[j][i] = matr[j][i] - m*matr[k-1][i]; } } } for (int i = *n-1; i >= 0 ; --i) { vectors[i] = matr[i][*n]/matr[i][i]; for (int c = n-1; c > i; --c) { vectors[i] = vectors[i] - matr[i][c] * vectors[c] / matr[i][i]; } } return vectors; } int main() { int **matr, n; scan_matr(matr,&n); int *vectors = solveGauss(&matr,&n); return 0; } 
  • Tip: Do not use arrays of pointers to arrays for matrices. It is much easier to select a one-dimensional array of size w * h , and then instead of mat[x][y] do mat[x + y*w] . - HolyBlackCat
  • In the loop for (int c = 0; c > i; --c) { vectors[i] = vectors[i] - matr[i][c] * vectors[c] / matr[i][i];} most likely the initial number should not be zero. - AlexGlebe
  • The compiler has given you a bunch of diagnostic messages that indicate your errors to you. Did you decide to just ignore them? - AnT

2 answers 2

In scan_matr and solveGauss you pass &matr == int *** ie matrix address. And take as an argument the matrix. int **

 int ** scan_matr( int *n){ printf("Введите порядок матрицы:"); scanf("%i", n); printf("\nВведите матрицу:\n"); int **matr = (int**)malloc(*n * sizeof(int*)); for (int i = 0; i < *n; ++i) { matr[i] = (int*)malloc((*n+1) * sizeof(int)); } for (int i = 0; i < *n; ++i) { for (int j = 0; j < *n+1; ++j) { scanf("%i",&(matr[i][j])); } puts(""); } return matr;} int* solveGauss(int **matr, int *n) { int *vectors; vectors = malloc(*n * sizeof(int)); for (int k = 1; k < *n; ++k) { for (int j = k; j < *n; ++j) { int a = matr[j][k-1]; int b = matr[k-1][k-1]; int m = a/b; for (int i = 0; i < *n+1; ++i) { matr[j][i] = matr[j][i] - m*matr[k-1][i]; } } } for (int i = *n-1; i >= 0 ; --i) { vectors[i] = matr[i][*n]/matr[i][i]; for (int c = 0; c > i; --c) { vectors[i] = vectors[i] - matr[i][c] * vectors[c] / matr[i][i]; } } return vectors;} int main(){ int n; int * * matr = scan_matr(&n); int *vectors = solveGauss(matr,&n); return 0; } 
  • On the contrary, you need an int *** argument in scan_matr . And pass on the address matr . In sloveGauss can be left. - AlexGlebe
  • Well, I kind of fixed it, as you said, but now again int b = matr [k-1] [k-1] crashes; * (( user10229865
  • Ltd., thank you, everything is fine, but you can’t tell me where in the Wikipedia algorithm upload.wikimedia.org/wikipedia/commons/f/fd/Gauss.png error? With these 3 1 -2 1 0 2 2 -1 3 4 -1 1 5 data, I divide by zero in the reverse vectors[i] = matr[i][*n]/matr[i][i]; - user10229865 pm
 for (int i = n-1; i >= 0 ; --i) { vectors[i] = matr[i][*n]/matr[i][i]; 

You already choose one thing, otherwise you have a loop with a counter from n-1 , and the matrix - with *n :)

Well, did you understand? If you already pass the address, then do not forget to dereference ...

And yet - if you allocate memory for N elements, then referring to the N element is still going beyond the array boundaries ...

This is so, offhand. What catches your eye. I didn’t dig more seriously, so it’s hardly a problem ... One int m = a/b; enough for the eyes that did not work ...