Hey, could someone help me out by telling me how I can read in 2 matrices from a single input file?

4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by Dimitar

What have you tried so far? It will be helpful to know the layout of the input file also.

I'm new to posting online, but I guess I should have given more info :)
Here is the code I managed to write and the input file.

Attachments
``````# include <iostream>
# include <iomanip>
# include <fstream>
using namespace std;
void multiply(double A[][], double B[][],int Nrows, int Ncols);
int main ()
{

ifstream fin("Program1input.txt");
ofstream fout("Output.txt");

if(fin.fail())
{
cerr << "Unable to open file";
exit(2);
}

fin.close();
fout.close();
return 0;
}
void multiply(double A[][], double B[][],int Nrows, int Ncols)
{
int r,c,size;
for(int r=0; r<Nrows; r++)
{
for(int c=0; c<Ncols; c++)
{
result[r][c]=0;
for(int k; k<size; k++)
result[r][c] +=A[r][c]*B[k][c]
}
}
}``````
``````0 1 2 3
3 0 1 2
2 3 0 1
1 2 3 0

1.5	2.5   	3.5 	4
-1.5	-2.5	-3.5	0.4
.15	.25	.35	.004
1	2	3	4``````

I'm new to posting online, but I guess I should have given more info :)
Here is the code I managed to write and the input file.

try something like this, i fill a vector of vectors with the values find in the file.. is not finished but i could help

``````#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <string>
#include <sstream>

using namespace std;

#define ROW vector<int>

int main ()
{
ifstream fin("Input.txt", ifstream::in);
ofstream fout("Output.txt");

if(!fin.is_open())
{
cerr << "Unable to open file";
exit(2);
}

int number;

vector<ROW> matrix;
while(!fin.eof())
{
string fila;
getline(fin, fila);
vector<int> row;

// convert to number
istringstream str(fila);
while(!str.eof())
{
str >> number;

// ok?
if (str)
{
cout << " " << number;
row.push_back(number);
}
}

// end of row
cout << endl;
matrix.push_back(row);
}

fin.close();
fout.close();
return 0;
}``````

Thank you for the help, sorry I didn't write back earlier, but I after a few changes (major in some places) I ended up with 5 functions and everything worked out nicely :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.