Need help with adding matrices

Hey guys, new here and wanted someone to help me figure out this program I have to do. I keep getting a error where I have defined ReadDouble and ReadInput, saying that its not defined anywhere. Once this program works, can I just replace the addition sign to multiply to get this program to multiply?

Thanks for your help




#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

class Matrix
{
private:
vector < vector < double > > values;
bool IsValid(int r, int c);

public:
bool Init(int rows, int cols);
void Display();
bool SetValue(int r, int c, double v);
double GetValue(int r, int c);
int Rows();
int Cols();
void Clear();
void Input();

};

bool Matrix::Init(int rows, int cols)
{
bool rv = true;
if (rows > 0 && cols > 0 && rows <= 100 && cols <= 100) {
vector<double> rowtmpl;
for (int i = 0; i < cols; i++)
rowtmpl.push_back(0.0);
for (int i = 0; i < rows; i++)
values.push_back(rowtmpl);
}
else {
cerr << "Bad dimension" << endl;
rv = false;
}
return rv;
}

int Matrix::Rows()
{
return values.size();
}

int Matrix::Cols()
{
int rv = 0;
if (Rows() > 0)
rv = values[0].size();
return rv;
}

bool Matrix::IsValid(int r, int c)
{
bool rv = false;
if (Rows() > 0 && Cols() > 0) {
if (r < Rows() && r >= 0 && c < Cols() && c >= 0)
rv = true;
}
return rv;
}

void Matrix::Display()
{
for (int r = 0; r < values.size(); r++) {
for (int c = 0; c < values.size(); c++)
cout << values[r][c] << "\t";
cout << endl;
}
}

double Matrix::GetValue(int r, int c)
{
double rv = 0.0;
if (IsValid(r, c) == true)
rv = values[r][c];
return rv;
}

bool Matrix::SetValue(int r, int c, double v)
{
bool rv = false;
if (IsValid(r, c) == true) {
values[r][c] = v;
rv = true;
}
return rv;
}

void Matrix::Clear()
{
values.clear();
}

void Matrix::Input()
{
int rsize = 0, csize;
do {
rsize = ReadInt("Rows?");
csize = ReadInt("Cols?");
} while (Init(rsize, csize) == false);
for (int r = 0; r < Rows(); r++){
for (int c = 0; c < Cols(); c++) {
values[r][c] = ReadDouble("Value?");
}
}
}

bool AddMatrix(Matrix &A, Matrix &B, Matrix&C)
{
bool rv = false;
if (A.Rows() == B.Rows() && A.Rows() > 0 && A.Cols() == B.Cols() && A.Cols() > 0) {
if (C.Init(A.Rows(), A.Cols() == true)) {
for (int r = 0; r < C.Rows(); r++) {
for (int c = 0; c < C.Cols(); c++) {
C.SetValue(r, c, A.GetValue(r, c) + B.GetValue(r, c));
}
}
rv = true;
}
else
cerr << "Cannot initalize results" << endl;
}
else {
cerr << "Cannot add matrices" << endl;
}
return rv;
}

int main()
{
Matrix A, B, C;
A.Input();
B.Input();
AddMatrix(A, B, C);
A.Display();
B.Display();
C.Display();

return 0;
}
hiya,
1. use code tags please mate
2.
I keep getting a error where I have defined ReadDouble and ReadInput


I cant see any instance of ReadInput anywhere. Did you mean ReadInt??

You are calling 2 methods, ReadDouble and ReadInt but you haven't told the compiler about them. Unless you haven't pasted in your full code.
The compiler says that because you have neither ReadInt nor ReadDouble functions declared so it doesn't know what to do. You must declare those functions so that the compiler knows what to call for.
Last edited on
Where would I declare such variables? How do I use code tags btw?

Can you guys give me a specific place where I would need to put the functions and what they would look like.

Thanks for the quick reply
i would make 2 member variables (nRow, nColumn).

ReadInt shoudl be made a method of matrix too. Looks like you're asking the user for the number of rows and columns.
Look here to read input and assign to variables.

http://www.cplusplus.com/doc/tutorial/basic_io/

ReadDouble() the same kinda thing BUT..... Are you really expecting your user to enter a value for every element in the matrix? If the user states 10 rows and 10 columns, that's one hundred values you are going to have to ask them for.


Can you show me how to change the AddMatrix of my program to multiply the matrix instead? I am lost on how to write it so it would Multiply.

Thanks
Topic archived. No new replies allowed.