• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

need help

// 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
edelossantos
Asked:
edelossantos
  • 2
  • 2
2 Solutions
 
jkrCommented:
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
 
edelossantosAuthor Commented:
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
 
jkrCommented:
Your whole code seems to be messed up. The vector is declared as 'int', not 'double', thus the error.
0
 
edelossantosAuthor Commented:
// 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now