Print out multiple matrices from a text file

Mar 24, 2020 at 8:42pm
Hello, I am trying to print out several matrices from a text file called data.txt and contains the matrix elements. The matrices will be of 4X4 dimension. The file format is given and explained below,


1 0 1 1.2 3.5
1 0 2 4.1 -2.4
1 0 3 0.9 1.03
1 1 2 2.5 3.0
1 1 3 -2.5 8.5
1 2 3 -1.2 0.09
2 0 1 0.7 -0.98
2 0 2 1.6 2.3
2 0 3 2.09 7.5
2 1 2 1.09 -0.89
2 1 3 4.5 5.9
2 2 3 1.0 -0.9


The first column is ID of the matrix, second column contains the row index of the matrix and third column contains the column index. The fourth and fifth columns are the matrix elements. The diagonal elements of the matrices are 0 (zero). I want to print one matrix for ID 1, one matrix for ID 2 and so on. This is the code I have written to print them. However, it prints only the last matrix. I could not figure out how to print all the matrices one after another.

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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main() {
    double mat[4][4]={0};
    int a, b, c;
    double d, e;
    ifstream infile;
    infile.open("data.txt");

	while(!infile.eof())  {  

	infile >> a >> b >> c >> d >> e;
        mat[b][c]=d;
        mat[c][b]=e;
	}
	
	for (int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            cout<< mat[i][j] << "\t";
        }
        cout<<"\n";
    }
    infile.close();
}

I will highly appreciate any help provided. Thank you.
Last edited on Mar 24, 2020 at 8:55pm
Mar 24, 2020 at 9:02pm
Notice that you read the first column's value into a but then never use it. As a result, nothing distinguishes the first matrix from the second; there is only one matrix. So in the lines
1
2
mat[b][c]=d;
mat[c][b]=e;

these values are overwritten every time you try to make a new matrix. You could use the matrix ID to fix this, or you could modify the placement of your loops to accommodate having only one matrix stored at a time.
Mar 25, 2020 at 6:54am
Thank you for your reply. I understand that I need to use the matrix ID but I could not figure out how can I effectively do it.
Last edited on Mar 25, 2020 at 6:54am
Mar 25, 2020 at 10:21pm
If there's a reason you need to store multiple matrices at once, then you could use the matrix ID in conjunction with another container (maybe an std::vector). But if you're only printing the matrices then you don't really need the matrix ID. Instead, think about how you might rearrange your current code to print the current matrix before reading the next.
Mar 26, 2020 at 9:46am
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <array>
#include <map>
using namespace std;

using MAT = array<array<double,4>,4>;


void print( const MAT &M )
{
   for ( const auto &row : M )
   {
      for ( auto e : row ) cout << e << '\t';
      cout << '\n';
   }
}


int main()
{
   map<int,MAT> matrices;
   int ID, i, j;
   double x, y;

// ifstream in( "data.txt" )
   istringstream in( "1 0 1 1.2 3.5    \n"
                     "1 0 2 4.1 -2.4   \n"
                     "1 0 3 0.9 1.03   \n"
                     "1 1 2 2.5 3.0    \n"
                     "1 1 3 -2.5 8.5   \n"
                     "1 2 3 -1.2 0.09  \n"
                     "2 0 1 0.7 -0.98  \n"
                     "2 0 2 1.6 2.3    \n"
                     "2 0 3 2.09 7.5   \n"
                     "2 1 2 1.09 -0.89 \n"
                     "2 1 3 4.5 5.9    \n"
                     "2 2 3 1.0 -0.9   \n" );

   while ( in >> ID >> i >> j >> x >> y )
   {
      matrices[ID][i][j] = x;
      matrices[ID][j][i] = y;
   }

   for ( const auto &M : matrices )
   {
      cout << "Matrix " << M.first << '\n';
      print( M.second );
      cout << '\n';
   }
}


Matrix 1
0	1.2	4.1	0.9	
3.5	0	2.5	-2.5	
-2.4	3	0	-1.2	
1.03	8.5	0.09	0	

Matrix 2
0	0.7	1.6	2.09	
-0.98	0	1.09	4.5	
2.3	-0.89	0	1	
7.5	5.9	-0.9	0
Last edited on Mar 26, 2020 at 9:54am
Topic archived. No new replies allowed.