ifstream file

Jul 24, 2017 at 7:41am
let say i have to call a matrix, say matrix A and then i input it as test1.in file. the i have to call another matrix, say matrix B and input it as test2.in file. what is my concern here, when i just call a matrix A and print it, it is ok. but then when i try to call another matrix B, the program eventually break, leaving a box saying this "Unhandled exception at 0x00ab8a07 in newQr.exe: 0xC00000FD: Stack overflow".

i dont know where's the problem.

this is my coding.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<math.h>

#define M 6
#define N 2
#define x 6
#define y 4
#define MAX 100
using namespace std;

void 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=1;i<=M;i++)
{
for(j=1;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=1;i<=x;i++)
{
for(j=1;j<=y;j++)
{
ifp >> B[i][j];
cout << setw(20) << B[i][j];
}
cout << endl;
}
ifp.close();

cin.get();
}

let say matrix A:
[1 3
4 2
4 5
5 7
6 4
6 8]

matrix B:
[3 4 5 7
1 4 6 8
3 5 5 7
1 4 5 6]
Last edited on Jul 24, 2017 at 7:42am
Jul 24, 2017 at 7:59am
An array in c/c++ starts with 0, hence a loop would look like this:

for(i = 0; i < M; i++)

Because of the 0 the index within an array must always be < size of the array [and >= 0], otherwise it will be out of bounds.
Jul 24, 2017 at 8:46am
@coder777
i dont think that would be a problem. it is actually the same whether i use for(i=0; i<M; i++) or for(i=1; i<=M; i++).
Jul 24, 2017 at 9:12am
i dont think that would be a problem.
Actually it is the problem. All other loops must be changed accordingly.

for(j=1;j<=N;j++) -> for(j=0;j<N;j++)

for(i=1;i<=x;i++) -> for(i=0;i<x;i++)

for(j=1;j<=y;j++) -> for(j=0;j<y;j++)

etc.

The problem is that if you have an out of bounds access it will lead to undefined behavior. Means it may causes a crash (usually it does) but it doesn't need to.
Jul 24, 2017 at 10:29am
I've already tried to change all for loop as you said. but still it doesn't work.
Jul 24, 2017 at 11:04am
It is probably not the only problem. What does 'still it doesn't work' mean.

This line:
double A[MAX][MAX], B[MAX][MAX];
requires that the stack provides more than 160k of memory space. This shouldn't be a problem on standard systems.

You may test it with #define MAX 10 . Currently you don't need more.
Last edited on Jul 24, 2017 at 11:12am
Jul 24, 2017 at 12:11pm
It might be a good idea to store the matrix dimensions inside the input file.
Example, the first line is the number of columns followed by number of rows:

test1.txt
2 6
1 3
4 2
4 5
5 7
6 4
6 8


test2.txt
4 4
3 4 5 7
1 4 6 8
3 5 5 7
1 4 5 6

Also, use separate functions for reading from the file, and displaying the matrix.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

const int MAX = 10;

using namespace std;

void readMatrix(double array[][MAX], int & cols, int & rows, string filename);
void showMatrix(double array[][MAX], int cols, int rows);

int main()
{
    double A[MAX][MAX], B[MAX][MAX];

    int ax, ay;
    readMatrix(A, ax, ay, "test1.txt");
    
    int bx, by;
    readMatrix(B, bx, by, "test2.txt");

    cout << setw(26) << "Matrix A" << endl;
    showMatrix(A, ax, ay);
    
    cout << "\n\n";
    
    cout << setw(26) << "Matrix B" << endl;    
    showMatrix(B, bx, by);

    cin.get();    
}


void readMatrix(double array[][MAX], int & cols, int & rows, string filename)
{
    cols = rows = 0; // set default value.
    ifstream fin(filename);
    if (!fin)
    {
        cout << "could not open input file: " << filename << endl;
        return;
    }
    
    fin >> cols >> rows;
    for (int i=0; i<rows; i++)
    {
        for (int j=0; j<cols; j++)
        {
            fin >> array[i][j];
        }
    }
}


void showMatrix(double array[][MAX], int cols, int rows)
{
    for (int i=0; i<rows; i++)
    {
        for (int j=0; j<cols; j++)
        {
            cout << setw(16) << array[i][j];
        }
        cout << endl;
    }    
}

Last edited on Jul 24, 2017 at 1:03pm
Jul 25, 2017 at 5:02am
@coder777
Now my program works. TQ.

[Matrix A
1 3
4 2
4 5
5 7
6 4
6 8
Matrix B
3 4 5 7
1 4 6 8
3 5 5 7
1 4 5 6
]
Jul 25, 2017 at 5:04am
@chervil Thanks for your reply but I am still not familiar with the class function and all as I am still new to this.
Jul 25, 2017 at 7:19am
@sna3 Ok. Well save this program, you can look at it later when you learned about functions - which you should be considering as something you will be needing for everything you do in the future.

But no need to rush. However there's a tutorial here, Take it slowly and make sure you follow what is being explained as you go through it:
http://www.cplusplus.com/doc/tutorial/functions/


Jul 25, 2017 at 7:23am
@chervil Really appreciate your help. I am actually looking at your program. Before my co-sv show me how to create functions, so I am still learning.
Topic archived. No new replies allowed.