Printing 2D arrays' elements and their respective indices in C programming

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

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 (/TC)

Other info: none

To do: Printing 2D arrays' elements and their respective indices in C programming

To show: How to use the 2D array by requesting inputs from users, store them in 2D array and then prints the inputs

 

 

/* Printing 3x3 array's subscript and their element */

#include <stdio.h>

 

#define m 3

#define n 3

 

int main(void)

{

int i, j;

int x[m][n];

 

printf("\n3x3 arrays' subscripts and\n");

printf("their respective elements\n");

printf("--------------------------\n");

 

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

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

{

printf("Enter int values for ur array lol!: ");

scanf_s("%d", &x[i][j], 3);

}

/* 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++)

printf("x[%d][%d] = %d\n", i, j, x[i][j]);

 

return 0;

}

 

Output example:

 

3x3 arrays' subscripts and

their respective elements

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

Enter int values for ur array lol!: 3

Enter int values for ur array lol!: 6

Enter int values for ur array lol!: 1

Enter int values for ur array lol!: 9

Enter int values for ur array lol!: 20

Enter int values for ur array lol!: 34

Enter int values for ur array lol!: 23

Enter int values for ur array lol!: 45

Enter int values for ur array lol!: 23

x[0][0] = 3

x[0][1] = 6

x[0][2] = 1

x[1][0] = 9

x[1][1] = 20

x[1][2] = 34

x[2][0] = 23

x[2][1] = 45

x[2][2] = 23

Press any key to continue . . .

 

 

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