1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <iomanip>
using namespace std;
template<typename T>
void printArray(int l, int m, int n, T arg)
{
for(int i = l; i < m; ++i)
{
for(int j = 0; j < n; ++j)
cout << setw(8) << arg[i][j];
cout << '\n';
}
}
int main()
{
int row, col;
cout << "Enter how many rows: ";
cin >> row;
cout << "Enter how many columns: ";
cin >> col;
// dynamic memory allocation:
// you can use any type instead of int
int **myArray = new int *[row];
for(int i = 0; i < row; ++i)
myArray[i] = new int[col];
cout << "\nEnter the elements of the matrix\n\n";
for (int i=0; i < row; i++)
{
cout << "Row " << i + 1 << '\n';
for (int j=0; j < col; j++)
{
cout << " Col " << j + 1 << ": ";
cin >> myArray[i][j];
}
// your check code
// ...
// for example print out the actual array:
cout << "\nCheck row " << i + 1 << " : \n";
printArray(i, i + 1, col, myArray);
cout << "Plus the result(s) of your check code...\n";
// end of the check code and continue with
// the next input line
cout <<'\n';
}
cout << "Entered matrix: \n";
printArray(0, row, col, myArray);
//----- the rest of your code ----
//free the allocated memory
for(int i = 0 ; i < row ; ++i )
delete [] myArray[i] ;
delete [] myArray ;
return 0;
}
|
Enter how many rows: 3
Enter how many columns: 4
Enter the elements of the matrix
Row 1
Col 1: 1
Col 2: 2
Col 3: 3
Col 4: 4
Check row 1 :
1 2 3 4
Plus the result(s) of your check code...
Row 2
Col 1: 5
Col 2: 6
Col 3: 7
Col 4: 8
Check row 2 :
5 6 7 8
Plus the result(s) of your check code...
Row 3
Col 1: 9
Col 2: 10
Col 3: 11
Col 4: 12
Check row 3 :
9 10 11 12
Plus the result(s) of your check code...
Entered matrix:
1 2 3 4
5 6 7 8
9 10 11 12 |