Page 1 of 1

## 8 Replies - 652 Views - Last Post: 27 September 2012 - 11:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293446&amp;s=3955e3828afafa4de1706d9c2cb67dff&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jgar

Reputation: 0
• Posts: 16
• Joined: 31-August 12

Posted 27 September 2012 - 07:07 PM

Hello,

I am having difficulties in implementing a function to add two matrixs and return the value of the two
into one new matrix..
So far I have this......
I am using a class and I am using a double pointer, which points to an array of arrays.

```class matrix
{
private:
double ** entries; // pointer to a pointer
double rows;
double columns;

public:
void parametizeConst (double r, double c)
{
rows = r;
columns= c;

// to  create a 2D array
entries = new double* [rows];

for (double i = 0; i < r; i++)
{
entries[i] = new double [columns];
}
}

void display()
{
for (int i =0; i < rows; i++)
{
for (int j =0; j< columns; j++)
{
cout<< entries[i][j];
}
}
cout<< endl;
}
}

void set(double r, double c , double v)
{
for (int i = 0; i < r; i++)
{
for (int j =0; j< c; j++)
{
entries[i][j] = v;
}
}
}
{

entries = new double *[rows];

if (rows = matrix.rows && columns == matrix.columns)

for (int i =0; i < rows; i++)
{
for( int j =0; j< columns; j++)
{
entries[rows][columns] = entries[rows][columns] + matrix[rows][columns];
}

return entries[rows][columns];
};

```

This post has been edited by Skydiver: 27 September 2012 - 07:30 PM
Reason for edit:: Added code tags. Learn to use them yourself.

Is This A Good Question/Topic? 0

### #2 jgar

Reputation: 0
• Posts: 16
• Joined: 31-August 12

Posted 27 September 2012 - 07:14 PM

PLEASE CAN ANYONE GIVE SOME TIPS???
OR TELL ME IF I AM IN THE RIGHT WAY??

### #3 CTphpnwb

• D.I.C Lover

Reputation: 3463
• Posts: 12,317
• Joined: 08-August 08

Posted 27 September 2012 - 07:15 PM

Here's two tips:
• Learn to be patient. Seven minutes isn't a long time!
• Learn to use code tags.

### #4 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

Posted 27 September 2012 - 07:16 PM

What are your difficulties, exactly? Without line numbers and the syntax highlighter it's pretty hard to point things out to you.

This post has been edited by DimitriV: 27 September 2012 - 07:18 PM

### #5 jgar

Reputation: 0
• Posts: 16
• Joined: 31-August 12

Posted 27 September 2012 - 07:27 PM

What I do not know how to do is how can I implement a function to add two matrices
and return the result as a pointer to a new matrix, which would be the sum of the original 2 matrices.

I am very confuse....

```double add(class matrix)
{

entries = new double *[rows];

if (rows = matrix.rows && columns == matrix.columns)

for (int i =0; i < rows; i++)
{
for( int j =0; j< columns; j++)
{
entries[rows][columns] = entries[rows][columns] + matrix[rows][columns];
}

return entries[rows][columns];
};

```

This post has been edited by Skydiver: 27 September 2012 - 07:31 PM
Reason for edit:: Added code tags, again. Please use them. It's right there on screen on how to use them!

### #6 #define

• Duke of Err

Reputation: 1737
• Posts: 6,175
• Joined: 19-February 09

Posted 27 September 2012 - 08:45 PM

Hi, you want to return an instance of matrix. The right hand side can be const since it is not changed.

usage :

```  result_mat = mat1.add(mat2);

// result_mat = mat1 + mat2;

```

```matrix add(const matrix &rhs)
{
// create an object of matrix to return (result say)

// set values of result matrix to *this values + rhs values

// return result matrix

}

```

Your class could probably use a constructor, destructor, copy constructor and a copy assignment operator.
Rule of three

### #7 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

Posted 27 September 2012 - 10:06 PM

CTphpnwb, on 28 September 2012 - 07:45 AM, said:

Here's two tips:
• Learn to be patient. Seven minutes isn't a long time!
• Learn to use code tags.

I would like to add a 3rd point to that.

3.Please do not use CAPS LOCK

regards,
Raghav

### #8 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,997
• Joined: 30-May 10

Posted 27 September 2012 - 10:18 PM

09 void parametizeConst (double r, double c)
...
17 for (double i = 0; i < r; i++)
Don't use double as a type for defining the size of arrays, or indexing into them.
You might have an array of doubles (or any other type), but the subscripts and dimensions are always integers.

> 06 if (rows = matrix.rows && columns == matrix.columns)
Refer to your book on the difference between = and ==

### #9 snoopy11

• Engineering ● Software

Reputation: 1117
• Posts: 3,338
• Joined: 20-March 10

Posted 27 September 2012 - 11:31 PM

Hmm,

Your code has a lot of errors there are a lot of misplaced } and just plain missing }

Your ideas too leave a lot to be desired in terms of Matrix math

You also allocate memory and never free it for your entries array this is quite bad planning..

That said perhaps a very simple example that compiles runs and actually works might be in order

dont get too excited this example only works for matrices of the same size not different sizes, which you will have to implement yourself...

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

template <typename T>
T **setArray( int x, int y)
{
T **dynamicArray;

dynamicArray = new T*[x];
for( int i = 0 ; i < x ; i++ )
dynamicArray[i] = new T [y];

return dynamicArray;
}

template <typename T>
void FreeArray(T** dArray)
{
delete [] *dArray;
delete [] dArray;
}

class matrix
{
private:
double ** entries; // pointer to a pointer
int rows;
int columns;

public:
void initialise (int r, int c)
{
rows = r;
columns= c;

// to  create a 2D array
entries = setArray<double>(r,c);

}

void display()
{
for (int i =0; i < rows; i++)
{
for (int j =0; j< columns; j++)
{
cout<< entries[i][j]<<",";

}
cout << endl;
}
cout<< endl;
}

void set(int r, int c , double v)
{
for (int i = 0; i < r; i++)
{
for (int j =0; j< c; j++)
{
entries[i][j] = v;
}
}
}

{
double **first = this ->entries;
double **second = mat2.entries;

for (int i =0; i < rows; i++)
{
for( int j =0; j< columns; j++)
{
entries[i][j]= first[i][j]+second[i][j];
}
}
return entries;
}
void erase()
{
FreeArray<double>(entries);
}
};//end of class

int main()

{
matrix matrix1;
matrix matrix2;
matrix1.initialise(2,2);
matrix2.initialise(2,2);
matrix1.set(2,2,21);
matrix2.set(2,2,42);

matrix1.display();
matrix1.erase();//free memory
matrix2.erase();//free memory

return 0;
}

```

First of I use a template to set a dynamic 2d array and allocate memory for it its very simple
dynamicArray[i] = new T [y]; does all the work for you.

next I use a template to Free the Array all very self explanatory.

set up a matrix class similar to yours

change paramitizeConst to initialise as its like a bit of a mouthful and im not sure what it means,

very simply use my Template to set up entries

entries is a pointer to a pointer so the return value from add should also be a pointer to a pointer

set up two variable arrays to hold matrix1 and matrix2

loop around setting entries to matrix1+matrix2

the answer is now in Matrix1