Solved

need help

Posted on 2004-04-24
4
239 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
  • 2
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Your whole code seems to be messed up. The vector is declared as 'int', not 'double', thus the error.
0
 

Author Comment

by:edelossantos
Comment Utility
// 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now