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