I'm using the header file below. I'm a little confused about how to store any matrices I create...

If I do:

int main()
{
    Matrix A(2, 3);
    Matrix B(3);

    return 0;
}

Matrix::Matrix(int mdim_, int ndim_)
{
    data_.resize (mdim_*ndim_);

    for (int i = 0; i < mdim_*ndim_; i++)
    {
		data_[i] = 0;
	}
}

Matrix::Matrix(int ndim_)
{
    data_.resize (ndim_*ndim_);

    for (int i = 0; i < ndim_*ndim_; i++)
    {
		data_[i] = 0;
	}
}

Then does this create 2 separate matrices stored in data_? Or will Matrix B just overide the data in Matrix A? And if this is the case how would I get around this? (the header needs to remain unchanged).

Thanks in advance!

Header file:

#include<string>
#include<vector>

class Matrix {
 public:

  Matrix(int mdim, int ndim);
  // Create a zero matrix with mdim rows and ndim columns

  Matrix(int ndim);
  // Create an zero ndim x ndim matrix

  void Add(Matrix b);
  // Add Matrix b to this matrix

  void Subtract(Matrix b);
  // Subtract b from this matrix

 private:
  int mdim_; // Number of rows of matrix
  int ndim_; // Number of columns of matrix
  std::vector<double> data_; // Vector storing the matrix data
};
int main()
{
    Matrix A(2, 3);
    Matrix B(3);

    return 0;
}

>> Then does this create 2 separate matrices stored in data_?
Yes it does. You have two distinct objects of type Matrix, they both withhold their own vector, so there is no interference between the two objects.

If you'd have a static vector there, then each instance of a Matrix would operate on the very same vector, so

class Matrix {
 public:
 <snip>

 private:
  static std::vector<double> data_; // Vector storing the matrix data
};

Then you'd be better of passing in the Matrix objects by reference and even by const reference, just to avoid temporary Matrix from being created upon method calls, so rather

// if 'b' is not supposed to change -> make it const
// and pass it in by reference, to be more efficient
void Subtract(const Matrix & b);

I'm having a little more trouble...I know this is a stupid question but how do I refer to each individual matrix since both are stored in data_?

For example for the Add function,

data_ = data_ + data_;

obviously won't work, but I cant see how to specify them seperately, can anyone please help?

Thanks in advance!

how do I refer to each individual matrix since both are stored in data_?

As per the above class declaration ...

// Add Matrix b to this matrix
void Matrix::Add(Matrix b)
{
    data_[ <index here> ] += b.data_[ <same index here> ];
}

That's how you access the other object's (b) data_ member. So the next step would be to put that inside a loop to process all the data_.

As per the above class declaration ...

// Add Matrix b to this matrix
void Matrix::Add(Matrix b)
{
    data_[ <index here> ] += b.data_[ <same index here> ];
}

That's how you access the other object's (b) data_ member. So the next step would be to put that inside a loop to process all the data_.

I'm sorry, but I don't see this working, mitrmkar.

Based on what I see, there are 2 separate matrices, each with a private "data_" member, which means it's only accessible internally to b. There would have to be some sort of getElement method that allows you to request a specific element from data_.

i.e.:

Matrix::getElement(int elementId) {
  return data_[elementId];
}

In addition, if data_ were static, the matrices would be stored in the same memory. Wouldn't the net result be 2 identical matrices that modify each other? Also that would cause Add to generate the result 2*data_[i] for every "i" every time Matrix::Add() was called...

I'm sorry, but I don't see this working, mitrmkar.

Hmm, it really should work, that's a method were both of the objects are of same class, hence it does not matter whether or not there is access to a private member variable.

Here is a little test program ...

class access_test
{
private:
  int answer;

public:
  access_test(int i) : answer(i) {}

  int plus(const access_test & other)
  {
    // increment this instance's 'answer' by other's
    answer += other.answer;
    // and return the current value ...
    return answer;
  }
};

#include <iostream>
int main()
{
  access_test fourty(40);
  access_test two(2);

  std::cout << fourty.plus(two) << std::endl;

  std::cin.get();
}

that should output 42 (which is the correct answer)

[EDIT]
Oh, I didn't at first notice what you were saying ...
>> In addition, if data_ were static, the matrices would be stored in the same memory

Yes, that would be quite unsuitable, I sort of tried to point that out there in post #2.

Interesting, looks like I have some experimenting to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.