473,412 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

please remove these errors

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();
}

Jun 23 '07 #1
7 1637
On 2007-06-23 17:09, 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.


#include <iostream.h>
It's <iostreamwithout the .h at the end.
#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[i][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[i][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::operator+(const Matrix&),
if you don't know how to perform matrix additions then this page will
help you: http://mathworld.wolfram.com/MatrixAddition.html.

--
Erik Wikström
Jun 23 '07 #2
See below.

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.


#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
Jun 23 '07 #3
On 23 Jun, 16:09, 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.
<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-lit...t.html#faq-5.8

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

Gavin Deane

Jun 23 '07 #4
>#include <iostream.h>
#include <iostream>
>#include <stdlib.h>
Remove this line completely
>#include <conio.h>
Change this line to
using namespace std
void main ( )
int main ( )
>getch()
cin.get()

And I think that's it... Did you get this code from an old textbook or
something?

Jun 23 '07 #5
<mo**************@gmail.comwrote:
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();
}
Jun 23 '07 #6
mo**************@gmail.com wrote:
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
Jun 23 '07 #7

<mo**************@gmail.comwrote in message...
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::ostream &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<intconst)
};

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.
--
Bob <GR
POVrookie
Jun 23 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Razvan | last post by:
Hi ! I have a big problem with my web site www.mihaiu.name. Sometimes when I visit my page with IE6 the browser ask me to download the index.html file ! The options are open, save, cancel,...
1
by: Rob Meade | last post by:
Hi all, I found an article on how to write a Windows service here: http://www.dotnetbips.com/displayarticle.aspx?id=178 It was pretty much what I wanted, I want the service to scan a...
5
by: Olly | last post by:
Hello Everyone! Could someone please have a look at my JS Form I posted below....Something wrong there, but I don't understand what's exactly. Many thanks. Olly ...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
5
by: ranishobha21 | last post by:
Dear all, i want to send some unix commands to remote unix machine in france through php.i am using socket communication in php, i have written a socket communication program so that it...
12
by: cpptutor2000 | last post by:
I am new to PHP and I am having a very odd problem. Could some PHP guru please help. I am passing some variables from one page to the next, and in the starting page, I have: <div...
11
by: shror | last post by:
Hi every body, Please I need your help solving my php mail() function problem that the code is appearing in the view source and I dont know whats the problem where I am using another page tto test...
9
by: Debbie | last post by:
I wonder if anyone can help me out, or point me in the right direction, in solving my current problem: I have started seeing an error on one of my ASP pages. Id displays totally blank except...
0
numberwhun
by: numberwhun | last post by:
Please remember the following when posting a question in the Perl Forum. Doing so will get you better assistance. Provide Sample Code - You will find that this is asked for more often than not....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.