
Patrick B. answered 03/11/20
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <stdlib.h>
int ** Matrix_Init( int nRows, int nCols)
{
int ** matrix = (int **) malloc( nRows * sizeof(int *));
if (matrix != NULL)
{
for (int iLoop=0; iLoop<nCols; iLoop++)
{
matrix[iLoop] = (int *) malloc(nCols*sizeof(int));
}
for (int iLoop=0; iLoop<nRows; iLoop++)
{
for (int jLoop=0; jLoop<nCols; jLoop++)
{
matrix[iLoop][jLoop]=0;
}
}
return(matrix);
}
}
void Matrix_Destroy(int ** matrix, int nRows)
{
for (int iLoop=0; iLoop<nRows; iLoop++)
{
if (matrix[iLoop]!=NULL)
{
free(matrix[iLoop]) ;
}
}
free(matrix);
}
void Matrix_Input ( int ** matrix, int nRows, int nCols)
{
for (int iLoop=0; iLoop<nRows; iLoop++)
{
for (int jLoop=0; jLoop<nCols; jLoop++)
{
cout << "Please input the value at row = " << (iLoop+1) << " : column " << (jLoop+1) << " :>";
cin >> matrix[iLoop][jLoop];
}
}
}
void Matrix_Dump( int ** matrix, int nRows, int nCols)
{
for (int iLoop=0; iLoop<nRows; iLoop++)
{
for (int jLoop=0; jLoop<nCols; jLoop++)
{
cout << matrix[iLoop][jLoop] << " ";
}
cout << endl;
}
}
int main()
{
int ** matrixA = Matrix_Init(5,5);
int ** matrixB = Matrix_Init(3,3);
Matrix_Input(matrixA, 5,5);
Matrix_Input(matrixB,3,3);
Matrix_Dump(matrixA,5,5);
Matrix_Dump(matrixB,3,3);
Matrix_Destroy(matrixA,5);
Matrix_Destroy(matrixB,3);
}