please remove these errors

M

mohammaditraders

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.




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

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

};
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';
}







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

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.




#include <iostream.h>

It's said:
#include <stdlib.h>

Do you use this one?
#include <conio.h>
Non-standard

class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;

I'm quite sure that it said this one should be called Array.
public :
Matrix( int rows , int cols ) ;
void getMatrix ( ) ;
void displayMatrix ( ) ;

void displayMatrix() const;
Matrix();
Matrix::Matrix operator + (Matrix);

Matrix operator+(const Matrix&);
};
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 ;
}
}
}

You really should try to indent your code, it will be much more readable
then.
void Matrix :: getMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
cin >> elements [ i ] [ j ] ;

std::cint >> elements[j];
//cout<<"Enter the 1st Matrix";
}

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

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

std::cout << elements[j] << " ";
}
cout << "|" << endl ;

std::cout << "|" << std::endl;
}

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

std::cout << '\n';
}


void main ( )

int main() or int main(int argc, char* argv[]) but never void main().
{
Matrix matrix1(2, 2),matrix2(2,2) ;
matrix1.getMatrix ( ) ;
matrix2.getMatrix();
//Matrix = matrix1 + matrix2 ;

Matrix matrix3 = matrix1 + matrix2;
matrix1.displayMatrix ( ) ;
matrix2.displayMatrix ( ) ;
matrix3.displayMatrix();

system ( "PAUSE" ) ;
getch();
Non-standard.

}

All that's left is to implement Matrix Matrix::eek:perator+(const Matrix&),
if you don't know how to perform matrix additions then this page will
help you: http://mathworld.wolfram.com/MatrixAddition.html.
 
J

John Harrison

See below.

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.




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

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

Matrix operator + (Matrix);

That's the only error that will stop it compiling. You have other errors
as well, but you've already been told about some of them at least.

john
 
G

Gavin Deane

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.

<snip code substantially unchanged from earlier post today>

You posted this question with similar (but not quite identical) code
earlier today. You got several responses, all containing good advice.
Why have you not taken that advice on board. Just about every error
that was pointed out in your first thread is still present in you
second.

Having so obviously demonstrated that you aren't prepared to listen to
advice, why are you still bothering to ask for help and why on earth
do you expect anyone else to bother giving that help when you ignored
everything suggested to you first time you asked?

Because I am feeling generous, I will repeat the most important piece
of advice from your previous thread:

*** YOU ARE TRYING TO WRITE TOO MUCH CODE AT ONCE ***

Your code is full of functions - constructors, reading a matrix,
writing a matrix, overloaded operators. Here is what you need to do:

1. Throw away everything you've written so far.
2. Pick *ONE OF* the functions you want to write (the constructor
might be a good one to start with).
3. Write that one function *AND NOTHING ELSE*.
4. Compile it. There will probably be errors.
5. Fix the compile errors.
6. Go back to step 4. *DO NOT* proceed to step 7 until *ALL* compile
errors are fixed.
7. Run it and test it. It probably won't work first time.
8. Fix the bugs that cause it to do the wrong thing.
9. Go back to step 4. *DO NOT* proceed to step 10 until *ALL* bugs are
fixed.
10. Pick *ONE* function to write next. Go back to step 3.

If you have problems at any stage, this FAQ explains how to get help
here:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

This FAQ explains the groups policy on helping with homework:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2

Gavin Deane
 
O

osmium

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.

It wasn't immediately evident to me if you had followed any of the advise
you were given. I will give you the benefit of the doubt and assume you
changed *something*. Your compiler is old and the code had to be modified to
meet current standards. It also used (unnecessarily) one non standard
header. The following compiles but you will have to find what I changed and
undo to make it work on your compiler. I changed perhaps 20-30 things.
Then, when you are have recovered, start adding stuff back in. SLOWLY!!!

You don't seem to be big on following advise but I will give you some
anyhow. The DevC compiler is easily installed and has a nice GUI and you
can recover your current state fairly easily. I suggest you download it and
use it from now on.

http://www.bloodshed.net/devcpp.html

If you use tabs instead of spaces they get lost between you and my display.
The resulting code is the following plug ugly format. I suspect "plug ugly"
is an American idiom. It means "really, really ugly".

<snip and replace>

// following John Harrison's advise
// strip the junk!!!!
// read what John Harrison said for *understanding*.

#include <iostream>
#include <cstdlib>
//#include <conio.h> // not standard

using namespace std;

class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;

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

};
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 ] ;

}

}

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

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


}

cout<<'\n';

}







int main ( )
{
Matrix matrix1(2, 2) ;
cout << "Enter data\n";
matrix1.getMatrix ( ) ;

matrix1.displayMatrix ( ) ;

system ( "PAUSE" ) ;
//getch();
}
 
D

Default User

Write a program which overloads a binary Minus (+) operator,


You ignored all the advice given in response to your previous message.
Why should we waste time with you?





Brian
 
B

BobR

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.

Since what we say goes in one eye and out the other:

#include <iostream> // > #include <iostream.h> // does not exist
#include <vector>

class Matrix{
std::vector< std::vector<int> > elements;
public:
Matrix() : elements( 5, 5 ){}
Matrix( size_t rows, size_t cols ) : elements( rows, cols ){}
void Print( std::eek:stream &out ){
for( size_t x(0); x < elements.size(); ++x ){
for( size_t y(0); y < elements.at(x).size(); ++y ){
out<<" at(x"<<x<<").at(y"<<y<<")=";
out<< elements.at(x).at(y)<<std::endl;
} // for(y)
} // for(x)
} // Print(ostream&)
std::vector<std::vector<int> >& Elements(){ return elements;}

void binaryMinus(){ return;}
void binaryMinus( std::vector<std::vector<int> > const &V2){
for( size_t x(0); x < V2.size(); ++x ){
elements.push_back( V2.at( x ) );
} // for(x)
} // Minus(vector<vector<int> >,vector<vector<int> >const)
};

int main(){
Matrix matrix1( 2, 2 ), matrix2( 3, 2 ) ;
matrix1.Print( std::cout ) ;
matrix2.Print( std::cout ) ;
std::cout<<"binaryMinus"<<std::endl;
matrix1.binaryMinus( matrix2.Elements() );
matrix1.Print( std::cout );
retrun 0;
}

What's the difference anyway, you probably won't read this.

Any resemblance to any of my other code, either living or dead,
is solely coincidental, only the names have been changed to
protect the innocent compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top