Advertisement
#include<iostream>
using namespace std;
int main()
{
int twoDA[3][3]; // Declaration of a 2-D array, which will hold arrays of int values
}
/* Creating a 2-D array holding 3 int arrays, with each array holding 3 int values. */
#include<stdio.h>
int main()
{
int twoDA[3][3]; /* 2-D array holding 3 int arrays(each array holding 3 int values) */
twoDA[0][0]=10; /* inserting 10 in the first array at its 0th index */
twoDA[0][1]=20; /* inserting 20 in the first array at its 1st index */
twoDA[0][2]=30; /* inserting 30 in the first array at its 2nd index */
twoDA[1][0]=40; /* inserting 40 in the second array at its 0th index */
twoDA[1][1]=50; /* inserting 50 in the second array at its 1st index */
twoDA[1][2]=60; /* inserting 60 in the second array at its 2nd index */
twoDA[2][0]=70; /* inserting 70 in the third array at its 0th index */
twoDA[2][1]=80; /* inserting 80 in the third array at its 1st index */
twoDA[2][2]=90; /* inserting 90 in the third array at its 2nd index */
return 0;
}
twoDA[0][0]=10;
The reference to the first array is specified by zero in the first bracket of 2D array. Zero in the second bracket specifies the
0th index in the first array at which we are putting int value 10;
twoDA[0][1]=20;
We are putting an int value(20) in the first array at its 1st index
twoDA[0][2]=30;
We are putting an int value(20) in the first array at its 2nd index
twoDA[1][0]=40;
The reference to the second array is specified by one in the first bracket of 2D array. Zero in the second bracket specifies the 0th index of the second array at which we are putting int value 40;
twoDA[1][1]=50;
We are putting an int value(50) in the second array at its 1st index
twoDA[1][2]=60;
We are putting an int value(60) in the second array at its 2nd index
Advertisement
printf(twoDA[1][0]); /* Reading the 1st array and its 0th index */
/* Creating a 2-D array holding 3 int arrays, with each array holding 3 int values. */
#include<stdio.h>
int main()
{
int twoDA[3][3]; /* 2-D array holding 3 int arrays(each array holding 3 int values) */
twoDA[0][0]=10; /* inserting 10 in the first array at its 0th index */
twoDA[0][1]=20; /* inserting 20 in the first array at its 1st index */
twoDA[0][2]=30; /* inserting 30 in the first array at its 2nd index */
twoDA[1][0]=40; /* inserting 40 in the second array at its 0th index */
twoDA[1][1]=50; /* inserting 50 in the second array at its 1st index */
twoDA[1][2]=60; /* inserting 60 in the second array at its 2nd index */
twoDA[2][0]=70; /* inserting 70 in the third array at its 0th index */
twoDA[2][1]=80; /* inserting 80 in the third array at its 1st index */
twoDA[2][2]=90; /* inserting 90 in the third array at its 2nd index */
/* Reading an array */
for(int i=0; i<3; i++)
{
for(int j=0; j<3;j++)
{
cout << twoDA[i][j] << "\n";
}
}
return 0;
}
10
20
30
40
50
60
10
20
10
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement