// 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];

Dani AI

Generated

A few focused points that complete the missing operations and avoid common pitfalls when using a single 1-D buffer for matrices.

Use a consistent index mapping: element at row r, column c is at Term[r * cols + c]. That single rule is the key to correct Transpose, Add, Sub, Multiply and Display. Below are compact, drop‑in implementations you can paste into the .cpp (they access other Matrix instances’ Term directly because member functions can access private members of the same class).

Matrix Matrix::Transpose() {
    Matrix b(cols, rows);
    for (int r = 0; r < rows; ++r)
        for (int c = 0; c < cols; ++c)
            b.Term[c * rows + r] = Term[r * cols + c];
    return b;
}

Matrix Matrix::Multiply(Matrix b) {
    if (cols != b.rows) { cout << "Incompatible matrices" << endl; return Matrix(0,0); }
    Matrix d(rows, b.cols);
    for (int i = 0; i < rows; ++i)
        for (int j = 0; j < b.cols; ++j) {
            int sum = 0;
            for (int k = 0; k < cols; ++k)
                sum += Term[i * cols + k] * b.Term[k * b.cols + j];
            d.Term[i * b.cols + j] = sum;
        }
    return d;
}

Matrix Matrix::Multiply2(Matrix b) {
    if (cols != b.rows) { cout << "Incompatible matrices" << endl; return Matrix(0,0); }
    Matrix bX = b.Transpose();
    Matrix d(rows, b.cols);
    for (int i = 0; i < rows; ++i)
        for (int j = 0; j < b.cols; ++j) {
            int sum = 0;
            for (int k = 0; k < cols; ++k)
                sum += Term[i * cols + k] * bX.Term[j * b.rows + k];
            d.Term[i * b.cols + j] = sum;
        }
    return d;
}

Matrix Matrix::Add(Matrix b) {
    if (rows != b.rows || cols != b.cols) { cout << "Incompatible matrices" << endl; return Matrix(0,0); }
    Matrix d(rows, cols);
    int n = rows * cols;
    for (int i = 0; i < n; ++i) d.Term[i] = Term[i] + b.Term[i];
    return d;
}

Matrix Matrix::Sub(Matrix b) {
    if (rows != b.rows || cols != b.cols) { cout << "Incompatible matrices" << endl; return Matrix(0,0); }
    Matrix d(rows, cols);
    int n = rows * cols;
    for (int i = 0; i < n; ++i) d.Term[i] = Term[i] - b.Term[i];
    return d;
}

int Matrix::Display() {
    for (int r = 0; r < rows; ++r) {
        for (int c = 0; c < cols; ++c)
            cout << Term[r * cols + c] << " ";
        cout << endl;
    }
    cout << endl;
    return 0;
}

Two important maintenance tips: implement the destructor, copy constructor and assignment operator (rule of three) or switch Term to std::vector<int> to avoid shallow-copy bugs and double deletes. Add checks around CompareRowCol so the program handles re-sizing or aborts cleanly. This builds on ’s indexing idea and addresses ’s “one‑dimensional” suggestion; ’s point about the specification is also valid — decide how you want to behave on incompatible sizes (error, exception, or return a zero-sized Matrix) and make that consistent.

Recommended Answers

All 3 Replies

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.