// complete the following code.
// Do not use 2-D array, such as int a[2][3];
#include <iostream>

#include <stdlib.h>
using namespace std;

// implement the functions : add, sub, mult, transpose
class Matrix {
public:
    Matrix(int row, int col);
    int GetData();
    Matrix Transpose();
    int Display();
    Matrix Multiply(Matrix b);
    Matrix Add(Matrix b);
    Matrix Sub(Matrix b);
    Matrix Multiply2(Matrix b);
    int CompareRowCol(Matrix b);
private:
    int rows, cols;
    int* Term;
};

Matrix::Matrix(int row, int col) : rows(row), cols(col)
{
    Term = new int[rows * cols];
}

int Matrix::GetData() {
    int input_value;
    cout << "rows = " << rows << "  cols = " << cols << endl;
    for (int j = 0; j < rows * cols; j++)
    {
        cout << "term value = ";
        cin >> input_value;
        cout << " " << endl;
        Term[j] = input_value;
    }
    return 0;
}

Matrix Matrix::Transpose() {
    Matrix b(cols, rows);

    int n = rows * cols;
    for (int i = 0; i < cols; i++)
    {
    //To be implemented
    }
    cout << endl;
    return b;
}

Matrix Matrix::Multiply(Matrix b) {
    if (cols != b.rows) cout << "Incompatible matrices" << endl;
    Matrix d(rows, b.cols);
    for (int i = 0; i < rows; i++)

    {
        for (int j = 0; j < b.cols; j++)
        {
            //To be implemented
        }
    }
    return d;
}
Matrix Matrix::Add(Matrix b) {
    if (cols != b.cols) cout << "Incompatible matrices" << endl;
    if (rows != b.rows) cout << "Incompatible matrices" << endl;

    Matrix d(rows, cols);
    for (int i = 0; i < rows; i++)

    {
        //To be implemented
    }
    return d;
}
Matrix Matrix::Sub(Matrix b) {
    if (cols != b.cols) cout << "Incompatible matrices" << endl;
    if (rows != b.rows) cout << "Incompatible matrices" << endl;

    Matrix d(rows, cols);
    for (int i = 0; i < rows; i++)

    {
        for (int j = 0; j < cols; j++)
        {
            //To be implemented
        }
    }
    return d;
}
Matrix Matrix::Multiply2(Matrix b) {
    if (cols != b.rows) cout << "Incompatible matrices" << endl;
    Matrix bXpose = b.Transpose();
    Matrix d(rows, b.cols);
    //  Must be implemented by using bXpose
    return d;
}
int Matrix::CompareRowCol(Matrix b) {
    if (cols != b.rows) return 1;
    else return 0;
}
int Matrix::Display() {

    int n;
    n = rows * cols;
    for (int i = 0; i < rows; i++)
    {
        //To be implemented
    }
    cout << endl;
    return 0;
}

int main()
{
    Matrix a(2, 2);
    Matrix b(2, 2);
    Matrix c(2, 2);

    cout << "Enter first matrix: " << endl;
    a.GetData();
    cout << "Enter second matrix: " << endl;
    b.GetData();

    cout << "Display first matrix: " << endl;
    a.Display();
    cout << "Display second matrix: " << endl;
    b.Display();

    Matrix d(2, 2);
    d = b.Transpose();
    cout << "Transpose() of Matrix b" << endl;
    d.Display();

    /* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
    if (a.CompareRowCol(b))
    {
        cout << "Error! column of first matrix not equal to row of second.";
        cout << "Enter rows and columns for first matrix: ";
    }

    c = a.Multiply(b);//not using transpose()
    c.Display();
    c = a.Sub(b);
    c.Display();
    cout << "Multiply of Matrix a,b" << endl;
    c.Display();
    cout << "using transpose()" << endl;
    c = a.Multiply2(b);//using transpose()
    c.Display();
    system("pause");
    return 0;
}

Complete the following code. And Do not use 2-D array, such as int a[2][3];

What is the code supposed to do?

Remember that I'm asking because reverse engineering the code given could have me arrive at the wrong answer. Also, since 2-D is not allowed, I'd use a 3-D or greater array and hand it in as that was the spec given.

    // Lets start with the easy part: displaying the Matrix
    // please forgive my cout and cin skills, its been a while
    // This also sets the foundation for matrix operations using 
    // one dimensional array  

    int count = 0;

    //int RowPos[rows]; // I forget how to use "new" without referencing help

    cout << endl;

    for (int y = 0; y < rows; y++)
    {
        //is this allowed, or considdered cheating
        //RowPos[y] = count;

        cout << y << "  ";

        for (int x = 0, x < cols; ++x)
        {
            cout << Term[count++] << " ";
        }

        cout << endl << endl;
    }

    cout << endl;

    //future references would look like this for adding matching matrices
    // result = a.Term[a.RowPos[y]+x] + b.Term[b.RowPos[y]+x]
    // or
    // result = a.Term[(y*rows) + x] + b.Term[(y*rows) + x]
    // or more specific
    // result[(y*rows) + x] = a.Term[(y*rows) + x] + b.Term[(y*rows) + x]

    /*
    // Compatible Matix addition

    //again with the "new" crisis 
    //int result[rows*cols];

    for (int y = 0; y < rows; y++)
    {
        for (int x = 0, x < cols; ++x)
        {
            result[(y*rows) + x] = a.Term[(y*rows) + x] + b.Term[(y*rows) + x]
        }
    }

    //finally something like this
    //DisplayMatix(&result, rows, cols);

    */

Try with struct in one dimensional array. So you avoid 2D array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.