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
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int ** create_array(int rows, int cols);
void fillArray(int ** a, int rows, int cols);
void showArray(int ** a, int rows, int cols);
int main(int argc, char * argv[])
{
for (int i=0; i<argc; i++)
cout << i << " " << argv[i] << endl;
// Should do proper validation, but for now,
// just assume values are where we want them
int rows = atoi(argv[2]);
int cols = atoi(argv[4]);
cout << "rows = " << rows << endl;
cout << "cols = " << cols << endl;
int ** array = create_array(rows, cols); // Allocate the array
fillArray(array, rows, cols); // Put in some values
showArray(array, rows, cols); // Display the contents
return 0;
}
int ** create_array(int rows, int cols)
{
int ** arr = new int * [rows];
for (int i=0; i<rows; i++)
arr[i] = new int [cols];
return arr;
}
void fillArray(int ** a, int rows, int cols)
{
int k = 0;
for (int i=0; i< rows; i++)
for (int j=0; j<cols; j++)
a[i][j] = k++;
}
void showArray(int ** a, int rows, int cols)
{
for (int i=0; i< rows; i++)
{
for (int j=0; j<cols; j++)
cout << setw(4) << a[i][j] ;
cout << endl;
}
}
|