This error occurs during program execution. The task itself sounds like this: enter image description here Please tell me how to fix? I guess I went beyond the array, but I don’t understand where and how to fix = /

#include <iostream> #include <math.h> using namespace std; int main() { int n, m; double r = 0; cin >> m >> n; double **a = new double*[m]; for (int i = 0; i < m; i++) a[i] = new double[n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; for (int k = 0; k < m; k++) { for (int j = k + 1; j < m + 2; j++) { r = a[j][k]/a[k][k]; for (int i = k; i < m + 1; i++) { a[j][i] = a[j][i] - r*a[k][i]; } } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) cout << a[i][j] << " "; cout << endl; } for (int i = 0; i < m; i++) delete []a[i]; system("pause"); return 0; } 

enter image description here

  • one
    in the code, the error here is `for (int j = k + 1; j <n + 2; j ++) {o = a [k] [j] / a [k] [k];` You explicitly address beyond the second dimension - KoVadim
  • and how to fix not tell? - Roman
  • Find the algorithm, how to bring the matrix to a triangular form and rewrite the algorithm correctly - KoVadim
  • for (int i = 0; i <m- 1; i ++) {for (int j = i + 1; j <n; j ++) {o = a [i] [j] / a [j] [j]; a [i] - = a [j] * o; }} I fixed it like, but now it gives an error error C2296: *: invalid, the left operand is of type "double *" in the string a [i] - = a [j] * o; How to fix it? - Roman

2 answers 2

It is logical that the error occurs due to memory access outside the created arrays. For example in this cycle

 for (int k = 0; k < m - 1; k++) { for (int j = k + 1; j < n+2; j++) { o = a[k][j]/a[k][k]; for (int i = k; i < m + 1; i++) { a[j][i] = a[j][i] - o * a[k][i]; } } } 

when j equal to n+1 already this sentence in the body of cycles

  o = a[k][j]/a[k][k] 

there is a reference to the supporting element with the index a[k][n+1]

So check the correctness of the cycle records and the indices used when referring to the elements of the arrays.

  • for (int i = 0; i <m- 1; i ++) {for (int j = i + 1; j <n; j ++) {o = a [i] [j] / a [j] [j]; a [i] - = a [j] * o; }} I fixed it like, but now it gives an error error C2296: *: invalid, the left operand is of type "double *" in the string a [i] - = a [j] * o; How to fix it? - Roman
  • @Roman a [j] is a pointer. Therefore, this operation a [j] * o is obviously not correct ,. - Vlad from Moscow
  • I changed the code here again, but there was another mistake, would you please help? I haven't felt so stupid for a long time = / - Roman

Thank you all for help) I sort of figured out everything)

 #include <iostream> #include <math.h> using namespace std; int main() { int n, m; double r = 0; cin >> m >> n; double **a = new double*[m]; for (int i = 0; i < m; i++) a[i] = new double[n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; for (int k = 0; k < n; k++) { for (int i = k + 1; i < m; i++) { r = a[i][k]/a[k][k]; for (int j = 0; j < n; j++) { a[i][j] = a[i][j] - r*a[k][j]; } } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) cout << a[i][j] << " "; cout << endl; } for (int i = 0; i < m; i++) delete []a[i]; system("pause"); return 0; }