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,
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.
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.
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.