By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,777 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,777 IT Pros & Developers. It's quick & easy.

Please help me in program

P: n/a
Write a program which overloads a binary Minus (-) operator,

The program will contain a class Matrix, This class will contain a
private data member Array[][] which store int values. The class will
further contain a Default constructor, get() function which takes
values for array from the user and also contain a Display function
witch display the array on the screen,

In main function create three objects Mat1, Mat2, Mat3 of this class,
first call get() and Display() functions with Mat1 and Mat2 objects
then implement the statement Mat3 = Mat1 - Mat2; and call Display()
function with Mat3.
Note: The matrix should be 2x2.

Jun 20 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
mo**************@gmail.com wrote:
Write a program which overloads a binary Minus (-) operator,

The program will contain a class Matrix, This class will contain a
private data member Array[][] which store int values. The class will
further contain a Default constructor, get() function which takes
values for array from the user and also contain a Display function
witch display the array on the screen,

In main function create three objects Mat1, Mat2, Mat3 of this class,
first call get() and Display() functions with Mat1 and Mat2 objects
then implement the statement Mat3 = Mat1 - Mat2; and call Display()
function with Mat3.
Note: The matrix should be 2x2.
You've got very precise instructions, it almost tells you how to write
the program line by line.

Which bit don't you understand?

class
private data member
binary - operator
default constructor
function

etc. etc.

The way to get help on a homework question is to post the code you have
written so far. That way we can tell what you understand and what you
don't. Even more importantly it shows that you are prepared to make and
effort and don't just expect to be given the answer.

Post your code and you will get help with it.

john
Jun 20 '07 #2

P: n/a
On Jun 20, 3:11 pm, mohammaditrad...@gmail.com wrote:
Write a program which overloads a binary Minus (-) operator,

The program will contain a class Matrix, This class will contain a
private data member Array[][] which store int values. The class will
further contain a Default constructor, get() function which takes
values for array from the user and also contain a Display function
witch display the array on the screen,

In main function create three objects Mat1, Mat2, Mat3 of this class,
first call get() and Display() functions with Mat1 and Mat2 objects
then implement the statement Mat3 = Mat1 - Mat2; and call Display()
function with Mat3.
Note: The matrix should be 2x2.
Better to approach some tutorial sites which has loads of examples.

Regards,
Sarath

Jun 20 '07 #3

P: n/a
On Jun 20, 3:11 am, mohammaditrad...@gmail.com wrote:
Write a program which overloads a binary Minus (-) operator,

The program will contain a class Matrix, This class will contain a
private data member Array[][] which store int values. The class will
further contain a Default constructor, get() function which takes
values for array from the user and also contain a Display function
witch display the array on the screen,

In main function create three objects Mat1, Mat2, Mat3 of this class,
first call get() and Display() functions with Mat1 and Mat2 objects
then implement the statement Mat3 = Mat1 - Mat2; and call Display()
function with Mat3.
Note: The matrix should be 2x2.
pay me and I'll write the program for you

Jun 20 '07 #4

P: n/a
On Jun 20, 11:18 am, John Harrison <john_androni...@hotmail.com>
wrote:
mohammaditrad...@gmail.com wrote:
Write a program which overloads a binary Minus (-) operator,
The program will contain a class Matrix, This class will contain a
private data member Array[][] which store int values. The class will
further contain a Default constructor, get() function which takes
values for array from the user and also contain a Display function
witch display the array on the screen,
In main function create three objects Mat1, Mat2, Mat3 of this class,
first call get() and Display() functions with Mat1 and Mat2 objects
then implement the statement Mat3 = Mat1 - Mat2; and call Display()
function with Mat3.
Note: The matrix should be 2x2.

You've got very precise instructions, it almost tells you how to write
the program line by line.

Which bit don't you understand?

class
private data member
binary - operator
default constructor
function

etc. etc.

The way to get help on a homework question is to post the code you have
written so far. That way we can tell what you understand and what you
don't. Even more importantly it shows that you are prepared to make and
effort and don't just expect to be given the answer.

Post your code and you will get help with it.

john

dear sir i wirte this code but give error can u correct it please help
me

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;
public :
Matrix(){
cout<<"paratmerized constructor";
}
Matrix( int rows , int cols ) ;
void getMatrix ( ) ;
void displayMatrix ( ) ;
Matrix :: Matrix operator+();

};
Matrix :: Matrix ( int rows = 0 , int cols = 0)
{
numCols = cols ;
numRows = rows ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
elements [ i ] [ j ] = 0 ;
}
}
}
void Matrix :: getMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
cin >elements [ i ] [ j ] ;
//cout<<"Enter the 1st Matrix";
}

}
//cout<<"Enter the 2nd Matrix"<<endl;
}
void Matrix :: displayMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
cout << "| " ;
for ( int j = 0 ; j < numCols ; j ++ )
{
cout << elements [ i ] [ j ] << " " ;
}
cout << "|" << endl ;
}

cout<<'\n';
cout<<'\n';
}

Matrix :: Matrix operator + (Matrix add)
{
Matrix temp;
temp.numCols=numCols + add.numCols;
temp.numRows=numRows + add.numRows;
return temp;
}

void main ( )
{
Matrix matrix1(2, 2),matrix2(2,2) ;
matrix1.getMatrix ( ) ;
matrix2.getMatrix();
//Matrix = matrix1 + matrix2 ;
matrix1.displayMatrix ( ) ;
matrix2.displayMatrix ( ) ;
//
system ( "PAUSE" ) ;
getch();
}

Jun 23 '07 #5

P: n/a
On Jun 22, 6:57 pm, mohammaditrad...@gmail.com wrote:
On Jun 20, 11:18 am, John Harrison <john_androni...@hotmail.com>
wrote:


mohammaditrad...@gmail.com wrote:
Write a program which overloads a binary Minus (-) operator,
The program will contain a class Matrix, This class will contain a
private data member Array[][] which store int values. The class will
further contain a Default constructor, get() function which takes
values for array from the user and also contain a Display function
witch display the array on the screen,
In main function create three objects Mat1, Mat2, Mat3 of this class,
first call get() and Display() functions with Mat1 and Mat2 objects
then implement the statement Mat3 = Mat1 - Mat2; and call Display()
function with Mat3.
Note: The matrix should be 2x2.
You've got very precise instructions, it almost tells you how to write
the program line by line.
Which bit don't you understand?
class
private data member
binary - operator
default constructor
function
etc. etc.
The way to get help on a homework question is to post the code you have
written so far. That way we can tell what you understand and what you
don't. Even more importantly it shows that you are prepared to make and
effort and don't just expect to be given the answer.
Post your code and you will get help with it.
john

dear sir i wirte this code but give error can u correct it please help
me

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;

public :
Matrix(){
cout<<"paratmerized constructor";}

Matrix( int rows , int cols ) ;
void getMatrix ( ) ;
void displayMatrix ( ) ;
Matrix :: Matrix operator+();

};

Matrix :: Matrix ( int rows = 0 , int cols = 0)
{
numCols = cols ;
numRows = rows ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
elements [ i ] [ j ] = 0 ;}
}
}

void Matrix :: getMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
cin >elements [ i ] [ j ] ;
//cout<<"Enter the 1st Matrix";

}
}

//cout<<"Enter the 2nd Matrix"<<endl;}

void Matrix :: displayMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )

{
cout << "| " ;
for ( int j = 0 ; j < numCols ; j ++ )
{
cout << elements [ i ] [ j ] << " " ;}

cout << "|" << endl ;

}

cout<<'\n';
cout<<'\n';

}

Matrix :: Matrix operator + (Matrix add)
{
Matrix temp;
temp.numCols=numCols + add.numCols;
temp.numRows=numRows + add.numRows;
return temp;

}

void main ( )
{
Matrix matrix1(2, 2),matrix2(2,2) ;
matrix1.getMatrix ( ) ;
matrix2.getMatrix();
//Matrix = matrix1 + matrix2 ;
matrix1.displayMatrix ( ) ;
matrix2.displayMatrix ( ) ;
//

system ( "PAUSE" ) ;
getch();

}
What's the error? PS. main should return int, not void.
(use "return(0)" at the end of main()). In addition,
your Addition operator does NOT add the matrices. You
are adding the _sizes_ of the matrices together, you
need to add the _elements_ together!

Jun 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.