Code:
#include <iostream>
using namespace std;
int main( int argc, char **argv ) {
/* 2-dimensional dynamic matrix */
int **dynamicMatrix = NULL;
/* variables to hold the number of rows and columns in the matrix */
int numRows = 0;
int numColumns = 0;
/* loop counters */
int row = 0;
int column = 0;
/* prompt for rows and columns */
cout << "Enter number of rows: ";
cin >> numRows;
cout << "Enter number of columns: ";
cin >> numColumns;
/* create the rows of the matrix */
dynamicMatrix = new int*[ numRows ];
/* assume each row will have the same number of columns
* note: each row could have a different number of columns */
for( row = 0; row < numRows; row++ ) {
dynamicMatrix[ row ] = new int[ numColumns ];
}
/* set all the values to 5 */
for( row = 0; row < numRows; row++ ) {
for( column = 0; column < numColumns; column++ ) {
dynamicMatrix[ row ][ column ] = 5;
}
}
/* display the matrix */
for( row = 0; row < numRows; row++ ) {
for( column = 0; column < numColumns; column++ ) {
cout << dynamicMatrix[ row ][ column ];
}
cout << endl;
}
return 0;
}