Error in the code on line 29:

Access violation when reading at ...

#include<iostream> using namespace std; void print(double **matr, int size) { cout << endl << endl; for (int i = 0;i < size;i++) { for (int j = 0; j < size; j++) { cout << matr[i][j] << " | "; } cout << endl; } } double Determinant(double **a,int size) { double det = 0; if (size == 1) { det = a[0][0]; } if (size == 2) { det = a[0][0] * a[1][1] - a[0][1] * a[1][0]; } if (size > 2) { double **c = new double*[size]; for (int k = size - 1;k <= size;k++) { for (int i = k + 1;i <= size;i++) { c[i][k] = a[i][k] / a[k][k]; for (int j = k + 1;j <= size;j++) { a[i][j] = a[i][j] - (c[i][k] * a[k][j]); } } } } for (int i = 0;i < size;i++) det *= a[i][i]; return det; } int main() { setlocale(LC_ALL, "Russian"); int size; cout << "Введите количество неизвестных: "; cin >> size; double **Matx = new double *[size]; for (int i = 0;i < size;i++) { Matx[i] = new double[size]; } for (int i = 0;i < size;i++) { for (int j = 0;j < size;j++) { cin >> Matx[i][j]; } } print(Matx, size); cout << "Детерминант: " << Determinant(Matx, size) << endl; //Удаление for (int i = 0; i < size; i++) { delete[] Matx[i]; // Удаляем каждый элемент } delete[] Matx; system("pause"); return 0; } 

What is the cause of the error?

  • Your code is not collected at all: In function 'int main()': error: 'size' was not declared in this scope: double **Matx = new double *[size]; - PinkTux
  • I missed when publishing the code, I already fixed it. - Suhoy571 4:34 pm
  • what will be logical. You have there k <= size conditions and automatically go beyond the array. - KoVadim 4:39 pm
  • How can you fix, if you put k < size , it will not go into the cycle at all. - Suhoy571
  • can open the formulas and write correctly? there obviously the cycle should be from scratch. - KoVadim 5:06

1 answer 1

  1. Memory for the rows of the matrix c in the function Determinant not allocated anywhere. Only selection of an array of pointers is made.

     double **c = new double*[size]; 

    and the memory for the actual elements of the matrix is ​​not allocated. It is clear that any attempt to access c[i][j] will cause a crash.

  2. In the Determinant function, the outer loop in a pair

     for (int k = size - 1;k <= size;k++) { for (int i = k + 1;i <= size;i++) { 

    will perform only two iterations (for k == size - 1 and k == size ), and the inner one - only one iteration (for i == size ). So conceived?

    At the same time, access by size indexes is access outside of the arrays used.

  3. Separately, it should be noted that the matrix c in the function Determinant goes into a memory leak.