matrices

T

tommy

Hello everyone,

I'm trying to write a program which will be adding and subtracting
two matrices. I've already written some code but there are a few problems.

1) During compilation I get this warning:
mt.cpp 17: temporary used for parameter 1 in call to: istream: operator >>
(int &)
2) When I'm trying to show results of addition and subtraction two matrices
I get some strange really big unexpected numbers.

The source code is bellow. I use Borland C++ v3.1
----------
def.h
----------
#ifndef _DEF_H_
#define _DEF_H_

#include <iostream.h>
#include <conio.h>
#include "mt.h"

#endif
---------
mt.h
---------
#ifndef _MT_H_
#define _MT_H_

class Mt { /* multidimensional table */
public:
Mt() ;
void fill() ;
friend void operator+(Mt &,Mt &) ;
friend void operator-(Mt &,Mt &) ;
private:
int values[5][5] ;
const int firstcolumn ; /* number of first column in table
*/
const int firstline ; /* number of first line in table */
int column ;
int line ;
const int nc ; /* number of columns */
const int nl ; /* number of lines */
} ;

#endif
--------
main.cpp
--------
#include "def.h"

int main(int , char *[]) /* argument counter, argument vector */
{
clrscr() ;

Mt m1, m2 ;
m1.fill() ;
m2.fill() ;

m1 + m2 ;
m1 - m2 ;

getch() ;
return 0 ;
}
-------
and mt.cpp
-------
#include "def.h"

Mt::Mt() : firstcolumn(0), firstline(0), nc(5), nl(5)
{
; /* do nothing */
}

void Mt::fill()
{
column = firstcolumn ;

while( column < nc )
{
for(line = firstline ; line < nl ; line++)
{
cout << "i= " << column << " j= " ;
cin >> values[column][line] ;
}

column++ ;
}
}

void operator+(Mt &first, Mt &second)
{
int sum[5][5] ;

// Let's make some addition of these two tables

int column = 0 ;
int line = 0 ;

cout << "\nAddition: \n" ;

while( column < 5 )
{
for(line = 0 ; line < 5 ; line++)
sum[column][line] = first.values[column][line] +
second.values[column][line] ;

column++ ;
}

column = 0 ;

while( column < 5 )
{
if ( column <= 5 - 1)
cout << "\n" ;

for(line = 0 ; line < 5 ; line++)
cout << sum[line][column] << " " ;

column++ ;
}
cout << "\n\n" ;
}

void operator-(Mt &first, Mt &second)
{
int sum2[5][5] ;

// Let's make some addition of these two tables

int column = 0 ;
int line = 0 ;

cout << "\nSubtraction :\n" ;

while( column < 5 )
{
for(line = 0 ; line < 5 ; line++)
sum2[column][line] = first.values[column][line] -
second.values[column][line] ;

column++ ;
}

column = 0 ;

while( column < 5 )
{
if ( column <= 5 - 1)
cout << "\n" ;

for(line = 0 ; line < 5 ; line++)
cout << sum2[line][column] << " " ;

column++ ;
}
cout << "\n\n" ;
}
 
V

Victor Bazarov

tommy said:
I'm trying to write a program which will be adding and subtracting
two matrices. I've already written some code but there are a few problems.

1) During compilation I get this warning:
mt.cpp 17: temporary used for parameter 1 in call to: istream: operator >>
(int &)

Really? The only 'cin' I see is in the 'fill' function and it doesn't
look like a use of a temporary to me... Hard to say. Your code is non-
standard and I don't have BC++ v3.1 to verify your claim.
2) When I'm trying to show results of addition and subtraction two matrices
I get some strange really big unexpected numbers.

The source code is bellow. I use Borland C++ v3.1

That's about as old a compiler as one can find still in use. Perhaps you
should consider at least upgrading to the [free] v5.5...

Now, about the code...

Names with leading underscore followed by a capital letter are reserved by
the implementation. There is no need for you to have that fancy leading
underscore here. Be simple.
#include <iostream.h>

#include <conio.h>

#include "mt.h"

#endif
---------
mt.h
---------
#ifndef _MT_H_
#define _MT_H_

class Mt { /* multidimensional table */

Misleading comment. It's really has a _two_ dimensional "table" and of
very specific size.
public:
Mt() ;
void fill() ;
friend void operator+(Mt &,Mt &) ;
friend void operator-(Mt &,Mt &) ;

Seems like two operators. Good. But why do they return 'void'? Do you
not want some result from those operations recorded?

Change those to
friend Mt operator+(Mt const&, Mt const&);
friend Mt operator-(Mt const&, Mt const&);

And add
void print() const; // think of how you'd implement it.
private:
int values[5][5] ;
const int firstcolumn ; /* number of first column in table
*/
const int firstline ; /* number of first line in table */
int column ;
int line ;

These two members above don't have any comment next to them. What are
they for?
const int nc ; /* number of columns */
const int nl ; /* number of lines */
} ;

#endif

This is a rather bad practice if you ask me. It is much better to
explicitly include all headers than do it indirectly through another
header. Doing it indirectly does NOT help improve compilation speed
and only adds confusion when one is looking at the 'main.cpp' module.
int main(int , char *[]) /* argument counter, argument vector */
{
clrscr() ;

Non-standard function. Try to avoid those until your program works.
Mt m1, m2 ;
m1.fill() ;
m2.fill() ;

m1 + m2 ;
m1 - m2 ;

These two statements above are very strange. Imagine I'd write

int i1, i2;
i1 = 1;
i2 = 2;
i1 + i2;

What does the addition do? Nothing. It adds two values and the sum is
simply thrown away. That's what the m1+m2; looks to me.

Read a good book about operator overloading. Find a copy of "Effective
C++" by Scott Meyers, you seem to be in desperate need of reading it.

The code in 'main' should be changed to

Mt msum = m1 + m2;
Mt mdiff = m1 - m2;
.. // somehow output the contents of 'msum' and 'mdiff'
getch() ;
return 0 ;
}

Again, avoid using those "collection" headers. Be explicit.
Mt::Mt() : firstcolumn(0), firstline(0), nc(5), nl(5)

Kudos! That's a very rare thing to see a novice who uses initialiser
lists instead of assignments. Good job!
{
; /* do nothing */

You don't need a semicolon here to do nothing. You may leave the comment
in if you want but many would understand that

{
}

does nothing.
}

void Mt::fill()
{
column = firstcolumn ;

while( column < nc )
{
for(line = firstline ; line < nl ; line++)

Rather strange choice of loop controlling statements. Why not
both 'while' or both 'for'?
{
cout << "i= " << column << " j= " ;

Prints

i= <number> j=

Did you forget to output 'line'?
cin >> values[column][line] ;
}

column++ ;
}
}

void operator+(Mt &first, Mt &second)

Well, if you read the "Effective C++", you'll know how you should declare
the addition operator:

Mt operator +(Mt const &first, Mt const &second)
{
int sum[5][5] ;

This should probably be

Mt sum;
// Let's make some addition of these two tables

int column = 0 ;
int line = 0 ;

cout << "\nAddition: \n" ;

You might want to leave this in for debugging purposes, but really your
addition operator shouldn't be telling everybody it's doing its job.
while( column < 5 )
{
for(line = 0 ; line < 5 ; line++)
sum[column][line] = first.values[column][line] +
second.values[column][line] ;

Since I recommend changing 'sum' to be of type Mt, this should become

sum.values[column][line] = ...
column++ ;
}

column = 0 ;

while( column < 5 )
{
if ( column <= 5 - 1)
cout << "\n" ;

for(line = 0 ; line < 5 ; line++)
cout << sum[line][column] << " " ;

column++ ;
}
cout << "\n\n" ;

You should consider creating a special _separate_ function for outputting
the result (an Mt object)

And, of course, since we changed the return value type, you need

return sum;

here.
}

void operator-(Mt &first, Mt &second)
[...]

Same notes as with the operator +.

All in all, a very good effort! Now, get a copy of "Effective C++" and
consume it.

Victor
 
R

Rade

tommy said:
Hello everyone,

I'm trying to write a program which will be adding and subtracting
two matrices. I've already written some code but there are a few problems.

1) During compilation I get this warning:
mt.cpp 17: temporary used for parameter 1 in call to: istream: operator >>
(int &)
2) When I'm trying to show results of addition and subtraction two
matrices
I get some strange really big unexpected numbers.

The source code is bellow. I use Borland C++ v3.1

[cut]

First of all, I have used this compiler (and the accompanying IDE, debugger
and so on) many years ago. The debugger was a decent one for these years...
So I expect you will be able to go step by step through your code until you
find the bug.

IIRC, if BC++ 3.1 reports such a warning, it actually means what is stated
in the warning, i.e. that the integer is read to a temporary, which is then
assigned to your 'values' array. Honestly, I don't see why this happens, but
I can see no harm if this happens. However, please check this by setting a
breakpoint at this line, going just one step and checking if the 'values'
array actually contains what you have typed.

Having this sorted out, the problem (2) may be a consequence of mixing rows
and columns. Generally, your first index is a column, and the second one is
a row, but it is opposite when you print. More counterintuitively, you print
horizontal columns and vertical lines (?!).

Regards,
Rade

P.S. A general comment... actually many people are posting questions like
"Why my program doesn't work ?" (often with lots of code). What I can't
understand is why it is so hard to learn a couple of basic debugging steps -
step into a function, step out of the function, watch the variable, set a
breakpoint. Not a rocket science. Even if they work on a platform where they
don't have debugger, it is easy to add some additional printouts at critical
places in the code, and later look at the results. This way, beginner finds
his/her own bug, instead of posting a question and hoping that somebody else
will find it.
 
V

Victor Bazarov

Rade said:
[...]
P.S. A general comment... actually many people are posting questions like
"Why my program doesn't work ?" (often with lots of code). What I can't
understand is why it is so hard to learn a couple of basic debugging steps -
step into a function, step out of the function, watch the variable, set a
breakpoint. Not a rocket science. Even if they work on a platform where they
don't have debugger, it is easy to add some additional printouts at critical
places in the code, and later look at the results. This way, beginner finds
his/her own bug, instead of posting a question and hoping that somebody else
will find it.

Couldn't you answer your own questions here? It's so much easier when the
entire planet is working for you instead of sitting there typing cout and
printf or stepping through the code that you are already sick of because
you have been mucking in it for the past several hours...

A while back in the old country when I learned to program, there was no
Usenet. We had to walk to school barefoot in the snow twenty miles all
uphill. We ate tin cans for breakfast and smoked cow manure to keep us
warm... And there were no debuggers and the computers were so old we had
to chop wood before we could boot them up, so printouts were out of the
question because most of the paper we usually spent on kindling the wood
or making cow manure joints...

Nowadays, paper is cheap here, besides, the debuggers are visual. And,
with Internet developing and spreading around the globe, there is no need
for the beginners to learn debugging or writing programs on a chalk- or
white-board first. Forgeddaboudid!

V
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top