My program consists of two classes. The first creates a 2D array and populates it with user input. The first class works correctly, and when I call it in main it is able to create and print a 2D array. However, I am attempting to pass a pointer to that 2D array to the second function to calculate the determinant of the matrix. However, my program keeps crashing after the determinant function is called. Why am I unable to multiply, add, or subtract these array elements?
Here is the implementation file for the determinant class:
#include <iostream>
#include "det.hpp"
using std::cout;
using std::endl;
Det::Det() {
};
int Det::determinant(int **pointerToArray, int arraySize) {
int determinant;
cout << "Calculating the determinant..." << endl;
if (arraySize == 2) {
determinant = (pointerToArray[0][0] * pointerToArray[1][1]) -
(pointerToArray[1][0] * pointerToArray[1][0]);
} else if (arraySize == 3) {
determinant = (pointerToArray[0][0] * ((pointerToArray[1][1] * pointerToArray[2][2]) -
(pointerToArray[1][2] * pointerToArray[2][1]))) -
(pointerToArray[0][1] * ((pointerToArray[1][0] * pointerToArray[1][1]) -
(pointerToArray[1][2] * pointerToArray[2][0]))) +
(pointerToArray[0][2] * ((pointerToArray[1][0] * (pointerToArray[2][1]) -
(pointerToArray[1][1] * pointerToArray[2][0])));
} else {
return determinant;
};
};
Here is how is the part of main which I am calling the function:
//this is the original object
Matrix* point = new Matrix();
//this is where I retrieve the data from the first function
int * tempPoint = point->readMatrix(newArray, squareSize);
/*this is where I call the determinant with
a pointer to the original array as a parameter*/
calculate.determinant(&tempPoint, squareSize);