8 Replies - 405 Views - Last Post: 27 September 2012 - 11:31 PM Rate Topic: -----

#1 jgar  Icon User is offline

  • New D.I.C Head

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

Adding Matrix

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;
	}
}
}
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:30 PM
Reason for edit:: Added code tags. Learn to use them yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: Adding Matrix

#2 jgar  Icon User is offline

  • New D.I.C Head

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

Re: Adding Matrix

Posted 27 September 2012 - 07:14 PM

PLEASE CAN ANYONE GIVE SOME TIPS???
OR TELL ME IF I AM IN THE RIGHT WAY??
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2992
  • Posts: 10,337
  • Joined: 08-August 08

Re: Adding Matrix

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.

Was This Post Helpful? 1
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Adding Matrix

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

Was This Post Helpful? 0
  • +
  • -

#5 jgar  Icon User is offline

  • New D.I.C Head

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

Re: Adding Matrix

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!

Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,635
  • Joined: 19-February 09

Re: Adding Matrix

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
 
}




C++ Operator Overloading Guidelines

Your class could probably use a constructor, destructor, copy constructor and a copy assignment operator.
Rule of three
Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Adding Matrix

Posted 27 September 2012 - 10:06 PM

View PostCTphpnwb, 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
Was This Post Helpful? 1
  • +
  • -

#8 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Adding Matrix

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 ==
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 778
  • View blog
  • Posts: 2,300
  • Joined: 20-March 10

Re: Adding Matrix

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

perhaps this link will be useful I suggest you read it..

Wiki link

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** add(matrix mat2)
    {
        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.add(matrix2);

    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

skipping to add function

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

display the answer

free memory

thats it...

It should be quite easy to modify this now... for what you need..

Snoopy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1