I am writing a 2d matrix program.
Requirements for the assignment:
Implement the following functions:
float *allocate(int rows, int cols);
void readm(float *matrix, int rows, int cols);
void writem(float *matrix, int rows, int cols);
void mulmatrix(float *matrix1, int rows1, int cols1, float *matrix2, int cols2, float *product);
My code (some parts removed in main, just creating and calling allocate)
int main() {
float * matrix1;
float * matrix2;
matrix1 = allocate(rows1,cols1);
matrix2 = allocate(rows2,cols2);
}
float *allocate(int rows, int cols) {
float ** matrix = new float *[rows * cols];
return *matrix;
}//end allocate
void writem(float *matrix, int rows, int cols) {
for (int x = 0; x < rows; x++) {
for (int y = 0; y < cols; y++) {
cout << "enter contents of element at " << (x + 1) << ", " << (y + 1) << " ";
cin >> matrix[x*rows + cols];
}
}
}//end writem
I get an error
Exception thrown at 0x0FECF6B6 (msvcp140d.dll) in lab5.exe: 0xC0000005: Access violation writing location 0xCDCDCDD5. If there is a handler for this exception, the program may be safely continued.
It occurs at the line cin >> matrix[x*rows + cols];