0% found this document useful (0 votes)
16 views6 pages

Arrays Part 3

Two-dimensional arrays can be thought of as lists of one-dimensional arrays. They are declared with two sets of brackets (type arrayName[x][y]). Each element is accessed via two subscripts, the row and column index. For example, to print the element in row 2 column 3 of array a, code would say printf("%d", a[2][3]). Multi-dimensional arrays allow arrays with more than one subscript, effectively creating arrays of arrays. They are declared with data_type and sizes for each dimension like char cube[50][60][30].

Uploaded by

Sidhu Worldwide
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Arrays Part 3

Two-dimensional arrays can be thought of as lists of one-dimensional arrays. They are declared with two sets of brackets (type arrayName[x][y]). Each element is accessed via two subscripts, the row and column index. For example, to print the element in row 2 column 3 of array a, code would say printf("%d", a[2][3]). Multi-dimensional arrays allow arrays with more than one subscript, effectively creating arrays of arrays. They are declared with data_type and sizes for each dimension like char cube[50][60][30].

Uploaded by

Sidhu Worldwide
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

11/13/2022

Two-dimensionalArrays: Thus, every element in the array a is identified


by an element name of the form a[i][j].
The simplest form of multidimensional array is
the two- dimensional array. Atwo-dimensional array is, in where ‘a’ is the name of the array, and ‘i’ and ‘j’
essence, a list of one-dimensional arrays. are the subscriptsthat uniquely identify each element in ‘a’.

To declare a two- dimensional integer array of


size[x][y], you would write something as follows: Initializing Two-Dimensional Arrays:
typearrayName[x][y]; Multidimensional arrays may be initialized by
specifying bracketed values for each row.
Where type can be any valid C data type and
arrayName will be a valid C identifier.

1
11/13/2022

Accessing Two-Dimensional ArrayElements:


Following is an array with 3 rows and each row
has 4 columns.
An element in a two-dimensional array is
int a[3][4] ={
accessed by using the subscripts. i.e., row index and
{0,1,2,3},
column index of the array.
{4,5,6,7},
For Example:
{8,9,10,11}
int val = a[2][3];
};
The nested braces, which indicate the intended
The above statement will take the 4 th element
row, are optional. The following initialization is equivalent
from the 3 rdrow of the array.
to the previous example
int a[3][4] ={0,1,2,3,4,5,6,7,8,9,10,11};

2
11/13/2022

Two-Dimensional Arrays program: Output:


#include<stdio.h>
int main()
{ a[0][0]: 0
int a[5][2]={{0,0},{1,2},{2,4},{3,6},{4,8}}; a[0][1]: 0
int i,j; a[1][0]: 1
for(i=0;i<5;i++) a[1][1]: 2
{ a[2][0]: 2
for(j=0;j<2;j++) a[2][1]: 4
{
a[3][0]: 3
printf(“a[%d][%d] = %d\n”,i,j,a[i][j]);
} a[3][1]: 6
} a[4][0]: 4
return 0; a[4][1]: 8
}

3
11/13/2022

Multi-Dimensional Arrays: Declaration of Multidimensional Array


A multidimensional array is declared using the
C programming language supports following syntax
multidimensional Arrays. Syntax:
data_type array_name[d1][d2][d3][d4]....[dn];
• Multi dimensional arrays have more than one Above statement will declare an array on N
subscript variables. dimensions of name array_name, where each element of
• Multi dimensional array is also called as matrix. array is of type data_type.
• Multi dimensional arrays are array o arrays. The maximum number of elements that can be
stored in a multi dimensional array array_name is size1 X
size2 X size3....sizeN.
For example:
char cube[50][60][30];

4
11/13/2022

5
11/13/2022

You might also like