473,473 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

matrices

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" ;
}
--------
Any help will be appreciate,
Regards
tommy
Jul 22 '05 #1
3 2159
tommy wrote:
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...
----------
def.h
----------
#ifndef _DEF_H_
#define _DEF_H_
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>
There is no standard header <iostream.h>. Use <iostream> and make sure to
change your code appropriately.
#include <conio.h>
There is no standard header <conio.h>. Whatever may arise from using it
is not guaranteed by the language.
#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
--------
main.cpp
--------
#include "def.h"
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 ;
}
-------
and mt.cpp
-------
#include "def.h"
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
Jul 22 '05 #2
"tommy" <i-*****@gazeta.pl> wrote in message
news:a7****************************@40tude.net...
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.

Jul 22 '05 #3
Rade wrote:
[...]
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
Jul 22 '05 #4

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

Similar topics

1
by: Nils Wagner | last post by:
Hi all, Has someone written a C binding to transfer matrices from C to Python and vice versa ? Any pointer would be appreciated. Nils
8
by: Noesis Strategy | last post by:
I am attempting to build a database that profiles about 50 companies in a single industries. In a standard MS Word format, each profile would be about 20 pages of dense text and tables. In order...
3
by: Prototipo | last post by:
Hi! I need to dynamically create X vectors of matrices (with a known size of 5x5). It's like a three-dimensional matrix with 2 known dimensions and the third unknown. I have something like: ...
5
by: Frederic Soustra | last post by:
Hi, I am trying to speed up some work I have to do with a 3D Matrix. If I am not mistaken, C is row major, hence the matrix A of size 2 x 2 x 2 would be stored like this in memory:...
1
emaghero
by: emaghero | last post by:
Does anybody know an algorithm for the fast multiplication of three n-x-n symmetric matrices? I have an algorithm, which I'm not too pleased with, that does the job. Does anybody know a faster...
9
by: tomamil | last post by:
imagine that you have different matrices with different names and you want to perform the same action with each of them. is it possible to put their names into some array and to create a loop that...
5
by: td0g03 | last post by:
This program adds two square matrices - Algorithm 1-3, Page 35. Change it to implement the generalized algorithm to add two m x n matrices. Matrices, in general, are not square or n x n...
2
by: debiasri | last post by:
I have to devide a matrix in variable no of matrices with each having variable no of rows. Like I have to devide a matrix with dim.9X19 into say 4 matrices with row nos 1,6,6,6 respectively, and I...
5
by: adinda | last post by:
So what i need is this; (I'm very new at this,, programming in C I mean...) In matlab I had a while loop, and after each loop was done I added my resulting matrix to an object. Seeing the loop...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.