0

I'm trying to build my skills here.
This code compiles and runs successfully if I go out of class.
It crashes when I need to enter numbers for matrices.
Can somebody explain to me why this crashes?

#pragma once
#ifndef matrixType_h
#define matrixType_h
template <class T>


    class matrixType {


public:


    matrixType() { rawMatrix1 = rawMatrix2 = addMatrices = subtractMatrices = multiplyMatrices = 0; };


    void initialize(T row1, T col1);
    void add();
    void subtract();
    void multiply();
private:


        T ** rawMatrix1, **rawMatrix2, ** addMatrices, ** subtractMatrices, ** multiplyMatrices;




    };



template <class T>


    void matrixType<T>::initialize(T row1, T col1) {



    for (int a = 0; a < row1; a++)
    {
        cout << "enter " << col1 << " numbers for row " << a + 1 << ":" << endl;
        for (int b = 0; b < col1; b++)
            cin >> rawMatrix1[a][b];
        cout << endl;
    }


    }



template <class T>
void matrixType<T>::add() 


    {



    cout << "adding" << endl;
    for (int a = 0; a < 3; a++)
    {
        for (int b = 0; b < 3; b++)      
            addMatrices[a][b] = rawMatrix1[a][b] + rawMatrix2[a][b];

    }


    }





    template <class T>
    void matrixType<T>::subtract() {

        cout << "subtracting" << endl;
        for (int a = 0; a < 3; a++)
        {
            for (int b = 0; b < 3; b++)
            {
                subtractMatrices[a][b] = matrix1[a][b] - matrix2[a][b];
            }
        }
    }

    template <class T>
    void matrixType<T>::multiply() {

        cout << "multiplying" << endl;  
        for (int a = 0; a < 3; a++)
        {
            for (int b = 0; b < 3; b++)
            {
                for (int inner = 0; inner < 3; inner++)
                {
                    multiplyMatrices[a][b] = matrix1[a][inner] * matrix2[inner][b];
                }
            }
        }   
    }

    #endif // !matrixType_h

    #include <iostream>
    #include "matrixType.h"
    using namespace std;

    int main() {
        matrixType<int>matrix1;
        int rows1, columns1, rows2, columns2;


            cout << "Enter number of rows for matrix 1" << endl;
            cin >> rows1;
            cout << "Enter number of columns matrix 1" << endl;
            cin >> columns1;

            cout << "Enter number of rows for matrix 2" << endl;
            cin >> rows2;
            cout << "Enter number of columns matrix 2" << endl;
            cin >> columns2;

            matrix1.initialize(rows1, columns1);

        return 0;
    }

Edited by Dani: Formatting fixed

3
Contributors
2
Replies
20
Views
1 Year
Discussion Span
Last Post by tinstaafl
0

I would start fresh and use a C++

typedef vector< vector < double > > Matrix;

then code something, beginning like the following,
to exploit the power built into C++ ...

void takeIn( Matrix& mat );
Matrix& operator += ( Matrix& a, const Matrix& b );
Matrix operator + ( const Matrix& a, const Matrix& b )
{
   Matrix tmp = a;
   return tmp +=  b;
}
// etc... as you need ... 
// make sure you define all the functions 
// before you call them //

Edited by David W

0

One of the problems with your existing code, is you're trying to access the pointers without allocating memory for them:

template <class T>
void matrixType<T>::initialize(T row1, T col1) 
{
    rawMatrix1 = new T*[row1];
    for (int a = 0; a < row1; a++)
    {
        rawMatrix1[a] = new T[col1];
        std::cout << "enter " << col1 << " numbers for row " << a + 1 << ":" << endl;
        for (int b = 0; b < col1; b++)
            std::cin >> rawMatrix1[a][b];
        std::cout << endl;
    }
}

Of course this means that you'll need to implement a destructor to delete all those pointers. You will probable want to consider adding size properties for them, so that you can iterate through each pointer to delete the pointer that it's pointing to.

Also in some of your functions you're using matrix1 and matrix2 instead of rawMatrix1 and rawMatrix2

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.