c++ - how can i print two matrices when i call them as two different input file? -
i tried print 2 matrices, matrix , b. when call 1 text file matrix a, program ok. when call text file of matrix b, program failed, leaving box saying "unhandled exception @ 0x00168a07 in newqr.exe: 0xc00000fd: stack overflow."
is wrong call 2 text file this? below code. generating algorithm qr householder method. since failed here, cant continue algorithm. hope know wrong here. here are:
test1.in matrix a:
1.00 -6.00 34.00 -1644.00 803.00 -42258.00 15524.00 -831864.00 285061.00 -15355806.00 5153062.00 -278046852.00
test2.in matrix b:
-1875.00 17976.00 485714.00 -501810.00 5370.00 409584.00 -973084.00 559740.00 291495.00 9193128.00 -64643018.00 55199850.00 6351060.00 175638624.00 -1430791544.00 1249618200.00 120491745.00 3213894936.00 -27252104806.00 23932788870.00 2200175790.00 58033455312.00 -498213904852.00 438253167540.00
this code:
#include <iostream> #include <fstream> #include <iomanip> #include <cmath> #define m 6 #define n 2 #define x 6 #define y 4 #define max 100 using namespace std; int main() { double a[max][max], b[max][max]; int i, j; ifstream ifp; ifp.open("test1.in", ios::in); cout << "matrix a" << setw(40) << endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { ifp >> a[i][j]; cout << setw(20) << a[i][j]; } cout << endl; } ifp.close(); ifp.open("test2.in", ios::in); cout << "matrix b" << setw(40) << endl; for(i=0;i<x;i++) { for(j=0;j<y;j++) { ifp >> b[i][j]; cout << setw(20) << b[i][j]; } cout << endl; } ifp.close(); cin.get(); }
i suspect problem current design of program : exception raised stack overflow exception means stack full.
this happens because both declared matrix , b on stack , reserving them huge space don't need (only maybe).
so think haev 2 possibilities : either decrease space allocated matrix, eg. decalring , b :
double a[m][n], b[m][n];
or can declare a
, b
on heap. so, suggest change type of matrix double[][]
to std::vector< std::vector<double> >
because vectors automatically store values on heap , won't have manually use new
and delete
.
#include <vector> int main() { std::vector< std::vector<double> > a, b; // modified line !! int i, j; ifstream ifp; ifp.open("test1.in", ios::in); cout << "matrix a" << setw(40) << endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { ifp >> a[i][j]; cout << setw(20) << a[i][j]; } cout << endl; } ifp.close(); ifp.open("test2.in", ios::in); cout << "matrix b" << setw(40) << endl; for(i=0;i<x;i++) { for(j=0;j<y;j++) { ifp >> b[i][j]; cout << setw(20) << b[i][j]; } cout << endl; } ifp.close(); cin.get(); }
this little modification should enough :) !
Comments
Post a Comment