Solved

need help

Posted on 2004-04-24
4
275 Views
Last Modified: 2010-04-01
// matrix.h 2x2

#ifndef MATRIX_H
#define MATRIX_H

class Matrix {

public:

   Matrix(int=0, int=1, int=1); // constructor    
   Matrix add(const Matrix &);
   Matrix sub(const Matrix &);
   Matrix mul(const Matrix &);
   Matrix div(const Matrix &);

   bool equal(const Matrix &) const;
   bool notEqual(const Matrix &) const;
   void matrixPrint() const;        // print Matrix 2x2.
   int RationalNumerator();
   void setElement(double, int);   // access private data members
   double getElement();             // function to access private data members.
   void Clear();                    // clears Matrix[2][2] array.
   void ValidSize(const Matrix &); // to check Matrix[2][2] array size.
   
private:

   int numerator;
   int denominator;
   int *set;
   int size;
   
};

#endif
// matrix.cpp

#include <iostream>

using namespace std;

#include "matrix.h"

Matrix::Matrix(int n, int d, int s)
{

   numerator = n;
   denominator = d;
   size = s;
   set = new int[size];

}

Matrix Matrix::add(const Matrix &a)
{

   Matrix t;

   t.numerator = a.numerator * denominator;
   t.numerator += a.denominator * numerator;
   t.denominator = a.denominator * denominator;

   return t;

}

Matrix Matrix::sub(const Matrix &s)
{

   Matrix t;

   t.numerator = s.denominator * numerator;
   t.numerator -= denominator * s.numerator;
   t.denominator = s.denominator * denominator;

   return t;

}

Matrix Matrix::mul(const Matrix &m)
{

   Matrix t;

   t.numerator = m.numerator * numerator;
   t.denominator = m.denominator * denominator;
     
   return t;

}

Matrix Matrix::div(const Matrix &v)
{

   Matrix t;

   t.numerator = v.denominator * numerator;
   t.denominator = denominator * v.numerator;

   return t;

}

void Matrix::matrixPrint() const
{

   if(denominator == 0)
     cout << "\nDivide by zero error!!!\n";

   else
     if(numerator == 0)
        cout << 0;
     else
        cout << numerator << '/' << denominator;

}

bool Matrix::equal(const Matrix &e) const
{

     Matrix temp ( *this ) ;  // this replaced by *this
       temp.sub ( e ) ;
       if ( temp.RationalNumerator () == 0 ) {
            return true ;    // Indicates equal
       }
       else {
            return false ;   // Indicates unEqual
       }            


}

int Matrix::RationalNumerator  ( ) {
         return numerator ;
}



bool Matrix::notEqual(const Matrix &n) const
{

     Matrix temp ( *this ) ;  // this replaced by *this
       temp.sub ( n ) ;
       if ( temp.RationalNumerator () == 0 ) {
            return true ;    // Indicates equal
       }
       else {
            return false ;   // Indicates unEqual
       }            



}

void Matrix::setElement(double, int i)
{
   if(i > 0)
     for(int i = 0; i < size; i++)
      getElement = set[i];

}

double Matrix::getElement()
{
   
   return getElement;

}

void Matrix::Clear()
{
   delete[] set;

}

void Matrix::ValidSize(const Matrix &init)
{
   size = init.size;
   set = new int[size];

   for(int i = 0; i < size; i++)
      set[i] = init.set[i];

} // end matrix.cpp


// matrix_driver.cpp [2][2]

#include <iostream>

using namespace std;

#include "matrix.h"

int main()
{

   Matrix c(5,3,4);
   Matrix d(3,2,6);
   Matrix x;
     
   c.matrixPrint();
   cout << endl << " addition ";
   d.matrixPrint();
   x = c.add(d);
   cout << " = ";
   x.matrixPrint();
   cout << '\n';

   c.matrixPrint();
   cout << " subtraction ";
   d.matrixPrint();
   x = c.sub(d);
   cout << " = ";
   x.matrixPrint();
   cout << '\n';

   c.matrixPrint();
   cout << " multiplication ";
   d.matrixPrint();
   x = c.mul(d);
   cout << " = ";
   x.matrixPrint();
   cout << '\n';

   c.matrixPrint();
   cout << " division ";
   d.matrixPrint();
   x = c.div(d);
   cout << " = ";
   x.matrixPrint();
   cout << '\n';
   cout << endl;

   cout << "5/3 is: " << '\n'
        << '\t' << "!= 3/2" << '\n'
        << '\t' << "!= 3/2" << endl;            

   return 0;

} // end driver


output:

matrix.cpp: In method `void Matrix::setElement(double, int)':
matrix.cpp:124: invalid use of member (did you forget the `&' ?)
matrix.cpp: In method `double Matrix::getElement()':
matrix.cpp:131: aggregate value used where a float was expected

0
Comment
Question by:edelossantos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 10909122
That should be

void Matrix::setElement(double d, int i)
{
  if(i > 0)
    for(int i = 0; i < size; i++)
     set[i] = d;

}

double Matrix::getElement() // shouldn't that be 'getElement(int i)'?
{

   return 0.0; // if so, replace this statement with 'return set[i];'

}

Apart from that: Could you please *stop* to flood this TA with high-point questions and *finish* these problems in the question that you have *open* and that deal with the *same* problem?
0
 

Author Comment

by:edelossantos
ID: 10909195
Apart from that: Could you please *stop* to flood this TA with high-point questions and *finish* these problems in the question that you have *open* and that deal with the *same* problem?

Understood.  Will do.

matrix.cpp: In method `void Matrix::setElement(double, int)':
matrix.cpp:124: warning: assignment to `int' from `double'
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 10909233
Your whole code seems to be messed up. The vector is declared as 'int', not 'double', thus the error.
0
 

Author Comment

by:edelossantos
ID: 10965881
// matrix1.h 2x2

#ifndef MATRIX1_H
#define MATRIX1_H

class Matrix {

public:

   Matrix(int=1, int=0, int=1);           // constructor    
   Matrix add(const Matrix &);
   Matrix sub(const Matrix &);
   Matrix mul(const Matrix &);
   Matrix div(const Matrix &);

   void matrixPrint() const;            // print Matrix 2x2
   void setElement(int, int);        // access private data members
   double getElement(int);              // access private data members
   void Clear();                        // clears Matrix[2][2] array
   void ValidSize(const Matrix &);      // check Matrix[2][2] array
   
private:

   int M[2][2];
   int col;
   int row;
   int *set;
   int size;
   
};

#endif

// matrix1.cpp

#include <iostream>

using namespace std;

#include "matrix1.h"

Matrix::Matrix(int c, int r, int s)
{
   col = c;
   row = r;
   size = s;
   set = new int[size];

}  

Matrix Matrix::add(const Matrix &a)
{

   Matrix t;

   t.M[0][0] = M[0][0]+a.M[0][0];
   t.M[0][1] = M[0][1]+a.M[0][1];

   return t;

}

Matrix Matrix::sub(const Matrix &s)
{

   Matrix t;

   t.M[0][0] = M[0][0]+s.M[0][0];
   t.M[0][1] = M[0][1]+s.M[0][1];

   return t;

}

Matrix Matrix::mul(const Matrix &m)
{

   Matrix t;

   t.M[0][0] = M[0][0]*m.M[0][0];
   t.M[0][1] = M[0][1]*m.M[0][1];
   
   return t;

}

Matrix Matrix::div(const Matrix &v)
{

   Matrix t;

   t.M[0][0] = M[0][0]/v.M[0][0];
   t.M[0][1] = M[0][1]/v.M[0][1];

   return t;

}

void Matrix::matrixPrint() const
{

        cout << col  << '\t' << row;

}

void Matrix::setElement(int d, int i)
{
 
  if(i > 0)
    for(int i = 0; i < size; i++)
     set[i] = d;

}

double Matrix::getElement(int i)
{
   
   return set[i];

}

void Matrix::Clear()
{
   delete[] set;

}

void Matrix::ValidSize(const Matrix &init)
{
   size = init.size;
   set = new int[size];
   for(int i = 0; i < size; i++)
      set[i] = init.set[i];

} // end matrix.cpp

// matrix_driver_1.cpp [2][2]

#include <iostream>

using namespace std;

#include "matrix1.h"

int main()
{

   Matrix c(5,3);
   Matrix d(3,2);
   Matrix x;
     
   c.matrixPrint();
   cout << " addition ";
   d.matrixPrint();
   x = c.add(d);
   cout << " = ";
   x.matrixPrint();
   cout << '\n';

   c.matrixPrint();
   cout << " subtraction ";
   d.matrixPrint();
   x = c.sub(d);
   cout << " = ";
   x.matrixPrint();
   cout << '\n';

   c.matrixPrint();
   cout << " multiplication ";
   d.matrixPrint();
   x = c.mul(d);
   cout << " = ";
   x.matrixPrint();
   cout << '\n';

   c.matrixPrint();
   cout << " division ";
   d.matrixPrint();
   x = c.div(d);
   cout << " = ";
   x.matrixPrint();
   cout << '\n';
   cout << endl;

   cout << "Matrix2x2 Class" << endl;    

   return 0;

} // end driver

I don't think this is the correct output.  Need ideas on how to modify.  Please advise.

5       3 addition 3    2 = 1   0
5       3 subtraction 3 2 = 1   0
5       3 multiplication 3      2 = 1   0
5       3 division 3    2 = 1   0

Matrix2x2 Class



0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Would like to move button in a function 3 91
c++ getting the first 10 characters of a char* string 11 110
best sources to up-to-date in C++? 8 114
Unresolved External Symbols 3 151
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question