Multiplying two matrices from two text files with unknown dimensions

Posted by wes schwertner on Stack Overflow See other posts from Stack Overflow or by wes schwertner
Published on 2012-09-08T20:10:55Z Indexed on 2012/09/15 15:38 UTC
Read the original article Hit count: 213

Filed under:
|
|
|

This is my first post here. I've been working on this c++ question for a while now and have gotten nowhere. Maybe you guys can give me some hints to get me started.

My program has to read two .txt files each containing one matrix. Then it has to multiply them and output it to another .txt file. My confusion here though is how the .txt files are setup and how to get the dimensions. Here is an example of matrix 1.txt.

#ivalue      #jvalue      value
   1            1           1.0
   2            2           1

The dimension of the matrix is 2x2.

1.0     0
0       1

Before I can start multiplying these matrices I need to get the i and j value from the text file. The only way I have found out to do this is

int main()
{
  ifstream file("a.txt");
  int numcol;
  float col;
  for(int x=0; x<3;x++)
  {
    file>>col;
    cout<<col;
    if(x==1)     //grabs the number of columns
    numcol=col;
  }
  cout<<numcol;
}

The problem is I don't know how to get to the second line to read the number of rows. And on top of that I don't think this will give me accurate results for other matrices files.

Let me know if anything is unclear.

UPDATE Thanks! I got getline to work correctly. But now I am running into another problem. In matrix B it is setup like:

#ivalue      #jvalue         Value
    1         1              0.1
    1         2              0.2
    2         1              0.3
    2         2              0.4

I need to let the program know that it needs to go down 4 lines, maybe even more (The matrices dimensions are unknown. My matrix B example is a 2x2, but it could be a 20x20). I have a while(!file.eof()) loop my program to let it loop until the end of file. I know I need a dynamic array for multiplying, but would I need one here also?

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream file("a.txt");      //reads matrix A
    while(!file.eof())
    {
        int temp;
        int numcol;
        string numrow;
        float row;

        float col;
        for(int x=0; x<3;x++)
        {
            file>>col;
            if(x==1)
            {
                numcol=col;      //number of columns
            }
        }

        string test;
        getline(file, test);     //next line to get rows
        for(int x=0; x<3; x++)
        {
            file>>test;
            if(x==1)
            {
                numrow=test;     //sets number of rows
            }
        }
        cout<<numrow;
        cout<<numcol<<endl;
    }


    ifstream file1("b.txt");     //reads matrix 2
    while(!file1.eof())
    {
        int temp1;
        int numcol1;
        string numrow1;
        float row1;

        float col1;
        for(int x=0; x<2;x++)
        {
            file1>>col1;
            if(x==1)
                numcol1=col1;    //sets number of columns
        }

        string test1;
        getline(file1, test1);   //In matrix B there are four rows.
        getline(file1, test1);   //These getlines go down a row.
        getline(file1, test1);   //Need help here.
        for(int x=0; x<2; x++)
        {
            file1>>test1;
            if(x==1)
                numrow1=test1;
        }
        cout<<numrow1;
        cout<<numcol1<<endl;
    }
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about matrix