Page 50 - DCAP407_DATA_STRUCTURE
P. 50
Unit 3: Arrays
In this example:
1. First, the header files are included using #include directive and function
printarr is defined.
2. The function printarr(int b[]) accepts an array as a parameter and using a
for loop, prints the values of the array.
3. The function printdetail(int b[]), prints the values of the array along with
their addresses using for loop.
4. Variable i is declared globally.
5. Inside the main() function, array b is declared.
6. Using a for loop, the value of i is assigned to each element in an array.
7. The functions printarr(b) and printdetail(b) are called. The getch() function
prompts the user to press a key and the program terminates.
Write an algorithm to add all the elements of an array.
3.2.2 Multidimensional Array
Multidimensional arrays are also known as "arrays of arrays." Programming languages often need to
store and manipulate two or more dimensional data structures such as, matrices, tables, and so on.
When programming languages use two subscripts they are known as two-dimensional arrays. One
subscript denotes a row and the other denotes a column.
The declaration of two-dimension array is as follows:
data_type array_name[row_size][column_size];
int m[5][10]
Here, m is declared as a two dimensional array having 5 rows (numbered from 0
to 4) and 10 columns (numbered from 0 to 9). The first element of the array is
m[0][0] and the last row last column is m[4][9]
Now let us discuss a three-dimensional array. A three-dimensional array is considered as an array of
two-dimensional arrays.
A three dimensional array is created as follows:
int bigArray [ ][ ][ ] = new int [10][10][4];
This will create an array named bigArray containing 400 integers. We can
access any element of this array by using 3 indices.
Suppose we want to assign a value 312 to the element at position 3 down, 7
across, and 2 in, then we write it as:
bigArray [2][6][1] = 312;
The general form of an n-dimensional array is as follows:
Consider an n-dimensional m1 x m2 x m3…..mn M containing elements m1, m2, m3,….mn. Each
element is specified by a list of n integers k1, k2, k3….kn known as index.
Where,
1<=k1<=m1, 1<=k2<=m2,……………….. 1<=kn<=mn.
An array M with index k1, k2, k3….kn is denoted by
LOVELY PROFESSIONAL UNIVERSITY 43