//this is my Matrix Header File
#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
class Vector;
class Matrix {
public:
Matrix (double a[], int n_row, int n_col);//Constructor
Matrix ();//Default Constructor
~Matrix();//Deconstructor
double value(int i, int j);//Gets a Value in a matrix in a certain spot
Vector row(int i);//Gets a Single Row of the Matrix and returns a Vector
int row();
int col();
private:
int r;//Rows
int c;//Columns
double * coms;//Components
};
#endif // MATRIX_H_INCLUDED
//this is my Matrix Class
#include "Matrix.h"
#include "Vector.h"
Matrix::Matrix (double a[], int n_row, int n_col)//Constructor
{
r = n_row;
c = n_col;
coms = newdouble[r*c];
for (int i=0; i<r*c; ++i)
coms[i] = a[i];
}
Matrix::Matrix ()
{
r = 2;
c = 3;
coms = newdouble[r*c];
for (int i=0; i<r*c; ++i)
coms[i] = 0;
}
Matrix::~Matrix() {delete [] coms;}//Deconstructor
double Matrix::value(int i, int j)
{
return coms[(i-1)*c+j-1];
}
Vector Matrix::row(int i)
{
double a[c];
for (int j=0, t=(i-1)*c; j < c; ++j, ++t)
a[j]=coms[t];
return Vector(a,c);
}
int Matrix::row()
{
return r;
}
int Matrix::col()
{
return c;
}
//This is what i have so far:
Matrix matrixFromFile(std::ifstream infile)
{
//load the text file and put it into a single string:
std::ifstream in("m1.txt");
std::stringstream buffer;
buffer << in.rdbuf();
std::string test = buffer.str();
std::cout << test << std::endl << std::endl;
Vector c = extractVectorFromString(test);
Matrix returnthis;
return returnthis;
}
//this is what extractVectorFromString is doing:
Vector extractVectorFromString(std::string line)
{
std::stringstream ss(line);
char ch;
int dim;
ss >> dim >> ch;// d [
int i;
double * vec = newdouble[dim];
for (i=0; i<dim; ++i) // ...,dn]
ss >> vec[i] >> ch;
if (ch != ']') // dim under
throw"Dimension error!";
if (!ss.good()) // dim over
throw"Dimension error!";
Vector v(vec,dim);
delete [] vec;
return v;
}