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
|
#include <iostream>
using std::cout;
using std::endl;
int main()
{
const int rows = 3;
const int cols = 3;
int v2d[][cols] = {{5,4,3},{6,7,8},{1,2,9}};
// print the diagonal (0,0) (1,1) (2,2)
cout << "Diagonal One: ";
for(int r = 0, c = 0; r < rows; r++,c++) // increment both r and c index
{
cout << v2d[r][c] << " ";
}
cout << endl;
// print the diagonal (0,2) (1,1) (3,0)
cout << "Diagonal Two: ";
for(int r = 0, c = cols - 1; r < rows; r++,c--) // increment r index, decrement c index
{
cout << v2d[r][c] << " ";
}
cout << endl;
return 0;
}
|