Matrix and Files Question.

Hey everyone. I have an assignment where I have to multiply matrices from another file.

The file will look something like this:

4 4
1.2 3.14 4.3 5.3
8.9 2.12 4.45 10
11 8.2 9.39 2.41
0.1 0.9 0.9 1.89

4 1
2 2 2 2

3 3
1.2 3.1 17.2
16.4 1.2 9.0
8.7 8.7 6.3

How can i deal the numbers from the file? Like I know if you have an array[1,2,3] and you type array[2] you will the number from the third location. I was wondering if there was a way to do that with the contents of the file. Please help me out guys, I'm so lost.

PS i changed the thread slightly so thats prolly why the previous comments seem off a little.
Last edited on
Load each one into some matrices in your program, then just multiply them normally.
Will that work if all three of the matrices are in the same file tho? I just dont understand how to load em up and multiply them since there are multiple ones per file.
Last edited on
Just load the first one, then keep advancing through the file until you get the next number, which should be the start of the second one, and repeat. It's not really different from reading single numbers, you just have a bunch of numbers you are reading in a row now.
So if i load the file with the matrices above it will just read the numbers in order? So i'd have to make a program to read the numbers and to understand to put them into matrices if i understand correctly.

Would someone happen to have a link to an example of this? I can sort of understand but it would be great to see it done.
Last edited on
Bump. Please guys I need your help.
Do you know how to read a matrix from file? Do you have code that does it?
If you do, post it and we'll see how we can help.
No i dont. I dont understand how to take the numbers in the file above and input them into a matrix in my program. I know how to use fstream and whatnot to get the data from the file but i have no idea how to use it correctly.
So I've been looking into this more and i think what i need to do is be able to "target" the individual numbers within the file to be able to use them appropriately in the file. I'm not sure however how to get the numbers individually. Is there some sort of operator that can just select numbers at certain locations in the file?
No, all you need is a function that will read an array from any given input stream. It should not care where in the stream you are -- it is sufficient that the stream pointer is somewhere immediately before the complete definition of a matrix.

Try doing this:

1
2
3
4
5
istream& operator >> ( istream& ins, my_matrix_type& m )
  {
  // Stuff that reads a matrix from ins goes here. //
  return ins;
  }

Now you can simply say:

1
2
3
4
5
my_matrix_type a;
my_matrix_type b;

cin >> a;
cin >> b;

Keep in mind that your function should use the ins stream and not cin. That is, everywhere you would write "cin" you should instead write "ins".

Remember, to read a number from a stream (cin or ins or the like), just:

  cin >> my_number;

Hope this helps.
Last edited on
Topic archived. No new replies allowed.