Hi all,

I have a .cpp code which reads a matrix from a .dat file and compute their eigenvalues. But I would like to read more matrices from the file not only one, and get the eigenvalues in the similar way.
Please help to solve this, I stuck at this point (i.e. how to read more than one matrix from the file).

The meaning of the content of the matrix.dat file is the following:

2 <-- this is the size of the NxN matrix, N=2
1 0 0 0 <-- (1 0) means the real and imaginary part of a matrix element 1,1;
                 (0 0) means the real and imaginary part of a matrix element 1,2;
0 0 1 0 <-- (0 0) means the real and imaginary part of a matrix element 2,1;
                 (1 0) means the real and imaginary part of a matrix element 2,2;

Would it not just mean adding some form of a break in the text file?

For example enter an 'x' after the first matrix, then the program knowns it finished and can call again for the next.

Maybe i missed something?

You don't even need that, since you have a number before your matrix that tells you how big it is.
Simply add second number after that, do some sort of while/for/do-while/goto loop (ok, maybe only while or for :) ) and it should work.
If you want more help, post your attempt (that is, attempt to read more matrices, not just one)

Could you post what you are thinkig about? I tried this thing but does not work.

Please, post here the code.

//algorhithm to read file containing matrix data where size of matrix (which will always be a square matrix) precedes matrix data. There may be data for 1 or more matrixes in the file.

int sizeofMatrix, i , j;
ifstream fin("myfile.dat");

//check if file open and continue only if it is

while(fin >> sizeofMatrix)
{
   //declare memory for matrix here

   //read in matrix here

   //calculate eigenvalue of matrix here
}
//fin is now in failed state either because it read in the entire file or because of an error in the file.  Find out why.  If file read successfully completed then move on to next part of program, else indicate unable to read file.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.