I'm having a problem in running the following code. It is giving me a segmentation fault as a runtime error.
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
float A[n][n], x[n], B[n], L[n][n], U[n][n], m[n][n], Aug[n][n + 1];
//Initializing matrix A,L,U
cout << "Enter A: \n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> A[i][j];
U[i][j] = A[i][j];
Aug[i][j] = A[i][j];
if (i == j) {
L[i][j] = 1;
}
else {
L[i][j] = 0;
}
}
}
//Initialising matrix B
cout << "Enter B: \n";
for (int i = 0; i < n; i++) {
cin >> B[i];
cout << "done" << i;
Aug[i][n] = B[i];
}
// ...
return 0;
}
Input:
n=2, A={2,5,-3,-4}, B={0,0}
The error occurs when I try to input B[1]
so done0 gets printed however done1 doesn't. I just can't figure out what the reason of this error is as I don't see any reason for B[1]
to be inaccessible.
You've misdiagnosed the problem. The segmentation fault occurs after the for
loop finishes, in the code you haven't shown us (the // ...
part). The done1
doesn't get printed because your code faults before it has a chance to flush the output buffer. Your cout << "done" << i;
just puts things in the output buffer, there's nothing there to flush the buffer.