Page 47 - DCAP407_DATA_STRUCTURE
P. 47
Data Structure
3.2 Types of Arrays
The elements in an array are referred either by a single subscript or by two or more subscripts. Hence,
the arrays are of two types namely, one-dimensional array and multidimensional array, based on the
subscript referred. A two-dimensional array is also a type of multidimensional array. When the array is
referred by a single subscript, then it is known as one-dimensional array or linear array. When the array
is referred by two subscripts, it is known as a two-dimensional array. Some programming languages
allow more than two or three subscripts and these arrays are known as multidimensional arrays.
3.2.1 Linear Array
A linear or one-dimensional array is a structured collection of elements (often called array elements). It
can be accessed individually by specifying the position of each element by an index value.
A linear array can be anything from a row of trees or a street full of lampposts. Any
sequence with repeated objects or shapes forms a linear array.
Now let us see how individual elements of linear array are accessed. The syntax for accessing an array
component is:
ArrayName[IndexExpression]
The IndexExpression must be an integer value. The integer value can be of char, short int, long int, or
Boolean value because these are integral data types. The simplest form of index expression is a constant.
If we consider an array number[25], then,
number[0] specifies the 1 component of the array
st
number[1] specifies the 2 component of the array
nd
number[2] specifies the 3 component of the array
rd
number[3] specifies the 4 component of the array
th
number[4] specifies the 5 component of the array
th
.
.
.
number[23] specifies the 2 last component of the array
nd
number[24] specifies the last component of the array
To store and print values from the number array, we can perform the following:
for(int i=0; i < 25; i++)
{
number[i]=i; // Storing a number in each array element
printf("%d", number[i]); //Printing the value
}
To store values in a number array we use a for loop. For every iteration of the for loop, the value of i is
assigned to each element of the array and then the values are printed using a printf statement.
Each element of an array is treated as a simple variable. Each array element is declared to hold a value
of integer data type.
for(int i=0; i < 25; i++)
{ //Iterations
/* Double the value in each array element
and store it in the array element*/
number[i]=2*number[i];
printf(“The output of Linear array is = %d”, number[i]);
}
40 LOVELY PROFESSIONAL UNIVERSITY