#include <iostream> #include <iomanip> using namespace std;
const int Rows = 3; const int Cols = 4;
void populate(int(*)[Cols]); void print(const int (*) [Cols]); void print1Row(const int* row); void printVersion2(const int (*) [Cols]); void print1Element(int);
void print1Element(int i);
int main() { int array[Rows][Cols]; populate(array); print(array); print1Row(*array); // print 1st row print1Row(*(array+1)); // print 2nd row print1Row(*(array+Rows-1)); // print last row cout << endl; printVersion2(array);
print1Element(**array);
// first row first column print1Element(*(*(array)+2)); // first row third column print1Element(*(*(array+1)+1)); // second row second column print1Element(*(*(array+2))); // third row first column print1Element(*(*(array+2)+3)); // third row fourth column }
void populate(int (*a)[Cols]) // same as (int a[][Cols]) { for (int r = 0; r < Rows; r++) { for (int c = 0; c < Cols; c++) { *(*(a+r)+c) = 4*r+c+1; } } }
void print(const int (*a)[Cols]) { for (int r = 0; r < Rows; r++) { for (int c = 0; c < Cols; c++) { cout << setw(4) << *(*(a+r)+c) ; } cout << endl; } cout << endl; }
void print1Row(const int* a) { for (int r = 0; r < Cols; r++) cout << setw(4) << *(a+r); cout << endl; }
void printVersion2(const int (*a)[Cols]) { for (int r = 0; r < Rows; r++) { print1Row(*(a+r)); } cout << endl; }
void print1Element(int element) { cout << element << endl; }