و این هم برنامه دوم و استفاده از حافظه پویا و اشاره گر ها . دقت کنید که در تابع makeMat شناسه array آرایه از اشاره گر ها هست و خونه های اون با آدرس آرایه های دیگه پر میشن . دقت کنید که array یک آرایه تک بعدی هست اما ما با دو اندیس ([5][2]array) به خونه های اون دسترسی پیدا می کنیم . در واقع در این مثال ([5][2]array) ما داریم به خونه 6 آرایه ای اشاره می کنیم که آدرسش در خونه 3 آرایه array قرار داره .

این مثال هم در 2008 ++VC کاملا تست شده و کار میکنه .

// M2Mat.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

// function prototype
int getMat(int &, int &);
int **makeMat(int, int);
void fillMat(int **, int, int);
int **multiMat(int **, int, int,
int **, int, int);
void showMat(int **, int, int);
void delMat(int **, int, int);

int _tmain(int argc, _TCHAR* argv[])
{
int **aMat, **bMat, **rMat,
aRow, bRow, rRow,
aCol, bCol, rCol;
system("cls");
// Get Parameters, Make and Fill Matrix 'A'
cout<<"Matrix 'A'\n-----------\n";
if (getMat(aRow, aCol))
{
cout<<"\nError : Numbers must be greater than 0 !";
_getch();
return 1;
}
aMat=makeMat(aRow, aCol);
fillMat(aMat, aRow, aCol);

// Get Parameters, Make and Fill Matrix 'B'
cout<<"\n\nMatrix 'B'\n-----------\n";
if (getMat(bRow, bCol))
{
cout<<"\nError : Numbers must be greater than 0 !";
_getch();
return 1;
}
bMat=makeMat(bRow, bCol);
fillMat(bMat, bRow, bCol);

// Clear Screan And Go To Show Result
system("cls");
cout<<"Matrix 'A'\n---------------\n";
showMat(aMat, aRow, aCol);
cout<<"\n\nMatrix 'B'\n---------------\n";
showMat(bMat, bRow, bCol);

// Multiply Two Matrix If Can Do It
if (aCol != bRow)
{
cout<<"\n\nCan not multiply two Matrixes !\n";
}
else
{
rRow = aRow;
rCol = bCol;
cout<<"\n\nResult Matrix\n---------------\n";
rMat = multiMat(aMat, aRow, aCol,
bMat, bRow, bCol);
showMat(rMat, rRow, rCol);
delMat(rMat, rRow, rCol);
}
delMat(aMat, aRow, aCol);
delMat(bMat, bRow, bCol);
cout<<"\n\nPress Any Key ...";
_getch();
return 0;
}

// Get Matrix : This function get matrix parameters
int getMat(int &row, int &col)
{
cout<<"Rows : ";
cin>>row;
cout<<"Columns : ";
cin>>col;
if (row<=0 || col<=0)
return 1;
return 0;
}



// Make Matrix : This function make a matrix and return **
int **makeMat(int row, int col)
{
int **array = new int * [row];
for (int i=0; i<row; i++)
{
array[i] = new int [col];
}
return array;
}

// Fill Matrix : This function fill matrix with input numbers .
void fillMat(int **mat, int row, int col)
{
cout<<endl;
for (int i=0; i<row; i++)
{
cout<<"Row "<<i+1<<" :\n";
for (int j=0; j<col; j++)
{
cout<<" Column "<<j+1<<" : ";
cin>>mat[i][j];
}
}
}

// Multiply Two Matrix : This function multiply two matrix
int **multiMat(int **aMat, int aRow, int aCol,
int **bMat, int bRow, int bCol)
{
int sum;
// Make Result Matrix
int **array = new int * [aRow];
for (int i=0; i<aRow; i++)
{
array[i] = new int [bCol];
}
// Start Multiply
for (int i=0; i<aRow; i++)
for (int j=0; j<bCol; j++)
{
sum=0;
for (int k=0; k<aCol; k++)
sum += aMat[i][k] * bMat[k][j];
array[i][j] = sum;
}
return array;
}

// Show Matrix : This function show content of Matrix
void showMat(int **mat, int row, int col)
{
for (int i=0; i<row; i++)
{
cout<<endl;
for (int j=0; j<col; j++)
{
cout.width(4);
cout<<mat[i][j];
}
}
}

// Delete Matrix : This function delete Matrix and free memory
void delMat(int **mat, int row, int col)
{
for (int i=0; i<row; i++)
{
delete [] mat[i];
}
delete [] mat;
}