?
Solved

need help

Posted on 2004-04-24
4
Medium Priority
?
286 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 2000 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 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