Assigning data from file to matrix C++

Good day everybody,
I'm beginner on C++.
The aim I want to achieve is quiet silly. But I don't see/understand where is the mistake.
I will be very thankfull with any bluecode help.

So... I want to assign the content of the file "REL", which is:

28 28
28 6
28 21
28 30
28 16
22 22
22 33
22 9
39 39
39 32
39 46
39 10
39 24
36 36
36 7
36 43
36 23
11 11
11 26
11 41
15 15
15 17
15 45
15 29
15 40
3 3
3 37
3 42
3 34
3 2
35 35
35 4
35 14
35 44
35 18
13 13
13 12
13 25
13 5
13 1
13 8
13 31
20 20
20 27
20 19
20 38

and my C++ code is:
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
67
68
69
70
71
72
73
74
75
#include <stdlib.h>
#include <malloc.h>
#include <iostream>
#include <fstream>

using namespace std;

void allocateMatrix(int **& A, int row, int col)
{
    int i;
    A = new int*[row];
    for (i=1; i<=col; i++)
        A[i] = new int[col];
}

void ReadData(int **& A, int row, int col) // read data from file
{  
int i,j;
ifstream REL1;
REL1.open ("REL");  // open file for reading                
for(i=1;i<=row;i++)   // row loop
{
	for(j=1;j<=col;j++)  // column loop
	{
		REL1 >> A[i][j]; // read data into matrix
	}
REL1.close(); // close the file                
}
}

void Display(int **& A, int row, int col) // display matrix
{ 
int i,j;
for(i=1;i<=row;i++)
{
	for(j=1;j<=col;j++)
    { 
		cout << A[i][j] << "\t ";  // display numbers
	}
	cout << endl;
}    
}

void Cero(int **& A, int row, int col) // display matrix
{
int i,j;
for(i=1;i<=row;i++)
{
	for(j=1;j<=col;j++)
	{
		A[i][j]=0;
	}
}	
}	

int main()
{
int tm,rowR,colR,rowM,colM,**A,**B; 

ifstream TM;
TM.open("TM");
TM >> tm;
TM.close();

rowR = rowM = colM = tm;
colR = 2;

allocateMatrix(A, rowM, colM);
allocateMatrix(B, rowR, colR);
Cero(A, rowM, colM); 
//Display(A, rowM, colM);
ReadData(B ,rowR, colR);
Display(B, rowR, colR);

}


and... when I run it in bash, the shell prompted to me:
1
2
3
28	 28	 
0	 0	 
Segmentation fault (core dumped)

Thanks in advance!!!
Last edited on
Well there is a laundry list of small, easy to make errors here. Since we know the program actually gets to the method display before segmentation faulting lets start there and work our way backward.

A seg fault has something to do with accessing parts of arrays that dont exist and it looks like you were able to get to the second element in that array before it tried to access the third one and said, "whoa, that doesnt exist buddy" (the code talks to me haha). So that should tell you there was probably a problem in the allocation of the arrays in your code. Looking at this code in allocateMatrix():
1
2
3
4
5
6
7
void allocateMatrix(int **& A, int row, int col)
{
    int i;
    A = new int*[row];
    for (i=1; i<=col; i++)
        A[i] = new int[col];
}

I noticed that on line five the stop condition i<=col, and since i is supposed to be representing the rows that drew a red flag. Another thing to note is since i is initialized to 1 and your array has 46 (0-45) different elements in the row section you would actually initialize element 46 of the rows which hasnt been allocted. That is of course if all you do is replace col on line five with rows. I would just re-write the for statement as follows so you dont waste any space:
 
for (int i = 0; i < rows; i++) //also get rid of the declaration of i at the top 

so go through all your for loops in the code and set the int = 0 and the stop condition to int < (either rows or cols). That should fix most of the mistakes.

If you run the program after that you should notice everything works except it only reads in the first integers in your file and the rest stay at 0 (what you initialized them too). Well if you notice on line 27 of the code, you put the close file in that first for loop. Thus, you close the file and never read anything else in during that entire time. After you push that out of your for loops everything should work fine. Did I explain that well enough cause sometimes i think im explaining myself but I make absolutely no sense. Anyway, if you have any other questions just ask.
Topic archived. No new replies allowed.