Printing 3x3 array subscripts and their respective elements C++ code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Printing the 3x3 array subscripts and their respective elements in C++ programming

To show: How to access and print the array subscripts and their respective elements in C++ programming

 

// printing the 3x3 array's subscript and their element

#include <iostream>

using namespace std;

 

#define m 3

#define n 3

 

int main(void)

{

int i, j;

int x[m][n]={{10,25,33}, {21,32,43},{20,42,51}};

 

cout<<"\n3x3 arrays' subscripts and\n";

cout<<"their respective elements\n";

cout<<"--------------------------\n";

// outer for loop, reading the row by row...

for(i=0; i<m; i++)

// inner loop, for every row, read every column by column...

for(j=0; j<n; j++)

cout<<"["<<i<<"]"<<"["<<j<<"]"<<"="<<x[i][j]<<"\n";

return 0;

}

 

Output example:

 

3x3 arrays' subscripts and

their respective elements

--------------------------

[0][0]=10

[0][1]=25

[0][2]=33

[1][0]=21

[1][1]=32

[1][2]=43

[2][0]=20

[2][1]=42

[2][2]=51

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index