Hi everybody..

I have a homework on 2-Dimentional arrays,
I have to creat a class and pass the parameters int it,
i have to overload operators( +, -, *, << )
how do I Add the matrices?? Subtract?? and Multiply them using operator??
what should I pass and what should I return??

also for the outputing the matrix using the operator?!!

please somebody replly.. I need explenation!!

thanks..Mona

Hi everybody..

I have a homework on 2-Dimentional arrays,
I have to creat a class and pass the parameters int it,
i have to overload operators( +, -, *, << )
how do I Add the matrices?? Subtract?? and Multiply them using operator??
what should I pass and what should I return??

also for the outputing the matrix using the operator?!!

please somebody replly.. I need explenation!!

thanks..Mona

Hey Mona. Seems like you have nothing so far and you're asking people to do your homework.
You should start by at least making a class for your 2D Matrix and declaring the methods you need. There is tons of information online on how to overload operators.
Check out this link for instance:
http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html

Then all you need to do is actually write the code for each of the operations. At least give it a try, then I'm sure people will help.

yes, I write My Programme but its has lots of errors !!
this is my class decleration..

class matrixType
{
	friend ostream & operator << ( ostream& , const matrixType& );


public:
	
	void set( int, int, int, int );
	void get( int&, int&, int&, int& );

	matrixType operator + ( const matrixType& )const; 
	matrixType operator - ( const matrixType& )const; 
	matrixType operator * ( const matrixType& )const; 

	matrixType();

private:
	double A[10][10];
	double B[10][10];
	double R[10][10];

	int row1, col1, row2, col2;

};

and I dont know how to pass the Array in the operator overloading function? and what to return??

this is the operator overloaded function deffinition:

sorry..

this is it:

matrixType matrixType::operator + ( const matrixType& obj )const
{
	matrixType result;

	result.A= this->A + obj.A;
	result.B= this->B + obj.B;
//	result.R= this->R + obj.R;


	//Addition
	if( a==c && b==d){
	
	cout<<"The result of Adding A & B:\n\n";

	for( int i=0; i<a; i++ )
		for( int j=0; j<b; j++ )
			obj.R[i][j] = obj.A[i][j] + obj.B[i][j];
	
	for( i=0; i<a; i++ )
	{
		for( j=0; j<b; j++ )
			cout<<setw(5)<<obj.R[i][j];
		cout<<endl;
	}
	cout<<endl;





	return result;

}

I know I'm mixing things but I need somebody to show me the right way to this!!

ok I complete my programme

but I have error in this implementation:

ostream& operator << ( ostream& osObj, const matrixType& obj )
{
	

	osObj<<obj.A;
	osObj<<obj.B;
	osObj<<obj.R;

	return osObj;

}

A, B, R are matrices defined in the class??
where is the problem ??

Perhaps these questions will help lead you to the answer...

What is obj.A? What is *(obj.A)? How is this different from (obj.A)[0]? What is **(obj.A)? How is this different from (obj.A)[0][0]?

you mean that I should create a pointer??

No, I mean you should think about what the code osObj<<obj.A is doing. In particular, obj.A is an address...

Instead of posting many threads, you could have used EDIT button. Just a tip ;)

Hi.. sorry I was quite busy !

ostream& operator << ( ostream& osObj, const matrixType& obj )
{
	
	osObj<<obj;
	return osObj;

}

this also doesnt work!!

What is obj.A? What is *(obj.A)? How is this different from (obj.A)[0]? What is **(obj.A)? How is this different from (obj.A)[0][0]?

if anyone know the answer of these questions please answer me
I want to know!

In the operator<< function, obj.A is a pointer to the starting location in memory of the 2D array A. Presumably, we don't want to print this out (its an address). You should instead loop through each element in A... Hint:

osObj << (obj.A)[i][j]

should go inside a loop, assuming that you use the variables i and j to loop over the matrix A.

yes, I'm using the loop in the main function
here it says that I cannot access a private member.. Although I'm using a friend function!!

main function:

int main()
{
	int i, j;
	int a, b, c, d;
	matrixType obj;

	matrixType A[10][10];
	matrixType B[10][10];
	matrixType R[10][10];


	//Determinning Arrays size
	cout<<"Enter matrix A size: ";
	cin>>a>>b;

	cout<<"Enter matrix B size: ";
	cin>>c>>d;

	//set() function call
	obj.set(a,b,c,d);

	//using operator << to output matrix A
	cout<<"The matrix A: \n\n";
	
	for( i=0; i<a; i++ )
	{
		for( j=0; j<b; j++ )
			cout<<setw(5)<<A[i][j];
		cout<<endl;
	}
	cout<<endl;


	//using operator << to output matrix B
	cout<<"The matrix B:\n\n";
	
	for( i=0; i<a; i++ )
	{
		for( j=0; j<b; j++ )
			cout<<setw(5)<<B[i][j];
		cout<<endl;
	}
	cout<<endl;
	




	//using operator ( + & - & * )

	//Addition
	if( a==c && b==d )
	{
		cout<<"The result of Adding A & B:\n\n";

		for( i=0; i<a; i++ )
			for( j=0; j<b; j++ )
				R[i][j] = A[i][j] + B[i][j];
	
		for( i=0; i<a; i++ )
		{
			for( j=0; j<b; j++ )
				cout<<setw(5)<<R[i][j];
			cout<<endl;
		}
		cout<<endl;

	}
	else
		cout<<"Matrices A & B should be the same size !\n\n";


	//Subtraction
	if( a==c && b==d )
	{
		cout<<"The result of Subtracting A & B:\n\n";

		for( i=0; i<a; i++ )
			for( j=0; j<b; j++ )
				R[i][j] = A[i][j] + B[i][j];
	
		for( i=0; i<a; i++ )
		{
			for( j=0; j<b; j++ )
				cout<<setw(5)<<R[i][j];
			cout<<endl;
		}
		cout<<endl;

	}
	else
		cout<<"Matrices A & B should be the same size !\n\n";


	//Multiplication
	if( b==c )
	{
	
		cout<<"The result of Multipliying A & B: \n\n";

		for( i=0; i<a; i++ )
		{
			for( j=0; j<b; j++ )
			{
			//	R[i][j]=0;
				for( int k=0; k<a; k++ )
				{
					R[i][j] = R[i][j] + ( A[i][k] * B[k][j] );

				}
				cout<<R[i][j];
			}
			cout<<endl;
		}
	}
	else
		cout<<"In Multiplication of matrices columns of the first\n"
			<<"should equals the rows of the second !\n\n ";


	return 0;


}

ijust want it to print it out!!
also I have three matrices .. what should I return????
iwant to use on simple procedure and use it to print out the three matrices (A, B, R)

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.