how can i print two matrices when i call them as two different input file?

-1

I tried to print two matrices, say Matrix A and B. When i just call one text file for matrix A, my program is ok. But then when I call another text file of matrix B, my program failed, leaving a box saying "Unhandled exception at 0x00168a07 in newQr.exe: 0xC00000FD: Stack overflow."

Is it wrong to call two text file like this? Below is my code. I am generating an algorithm for QR Householder method. But since I already failed here, I cant continue my algorithm. hope to know what is 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 is my 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();
}
c++
matrix
segmentation-fault
asked on Stack Overflow Jul 25, 2017 by olive • edited Jul 25, 2017 by olive

1 Answer

0

I suspect it is a problem with the current design of your program : the exception raised is a Stack overflow exception which means your stack is full.

This happens because you both declared matrix A and B on the stack and reserving them a huge space that you don't even need (only for now maybe).

So i think you haev two possibilities : either you decrease the space allocated for your matrix, eg. by decalring A and B like that :

double A[M][N], B[M][N];

Or you can declare A and B on the heap. To do so, i suggest you change the type of your matrix from double[][]to std::vector< std::vector<double> > because vectors automatically store their values on the heap and you won't have to manually use newand delete.

#include <vector>
int main()
{
    std::vector< std::vector<double> > A, B; // The only 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 be enough :) !

answered on Stack Overflow Jul 25, 2017 by Xatyrian

User contributions licensed under CC BY-SA 3.0