O'Reilly logo

Express Learning - Computer Fundamentals and Programming by ITL ESL, Ashok Kamthane

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Array
1. What is an array?
Ans:
1. An array is a linear data structure.
2. It is a finite collection of data elements.
3. An array permits homogeneous data. It means its each data element is of same type.
4. Similar data type elements are stored contiguously in the memory and that too under one variable name.
2. How elements of an array are stored?
Ans: Elements of array are stored in contiguous memory locations. One-dimensional or more
than one-dimensional elements are stored contiguously. Figure 7.1 shows storage of elements of a
one-dimensional array Num[5] contiguously.
Num[0]
1
65516
Num[1]
2
65518
Num[2]
3
65520
Num[3]
2
65522
Num[4]
5
65524
Figure 7.1 Storage of One-Dimensional Array
Where 
Num[0] = 1 and its Address : 65516
Num[1] = 2 and its Address : 65518
Num[2] = 3 and its Address : 65520
Num[3] = 2 and its Address : 65522
Num[4] = 5 and its Address : 65524
7
M07_ITL-ESL4791_02_SE_C07.indd 130 12/22/2012 5:02:59 PM
Array II-131
3. Explain the syntax of a one-dimensional array.
Ans: Declaration of a one-dimensional array can be done with data type first, followed by it is
variable name and, at last, the size is enclosed in square bracket.
The syntax of declaration of one-dimensional array is as follows:
Data-type var_name[Size];
Example:
int a[5];
It tells to the compiler that a is an integer type of array and can store five integers. The compiler
reserves 2 bytes of memory for each integer array element, i.e., 10 bytes are reserved for storing five
integers in the memory.
In the same way, array of different data types can be declared as follows:
char ch[10];
float real[10];
long num[5];
4. How are array elements accessed?
Ans: Individual elements of the array are accessed by specifying the subscript. Index is the other
name of subscript. The subscript is enclosed in the square bracket. Subscript provides an easy way to
address the individual elements of an array. Array elements are numbered starting from 0, i.e., subscript
starts from 0. The first element of the array is num[0], second is num[1] and so on.
Subscript to the array specifies an exact element in the array.
For example,
num[0]=1; /* First element*/
num[1]=2;
num[2]=4;
num[3]=2;
num[4]=5;
5. How is one-dimensional array initialization done?
Ans: Assigning initial values to an array is nothing but initialization of an array. The array initializa-
tion can be done as under:
int a [5]={1,2,3,4,5};
where five elements are stored in an array a. The array elements are stored sequentially in separate
locations. Then, question arises how to call individually to each element from this bunch of integer ele-
ments. Reading of array elements begins from 0. By indicating the position of element, one can retrieve
any element of an array. Array elements are called with array name followed by element numbers.
Table 7.1 explains the same.
Table 7.1 Calling Array Elements
a[0] refers to 1st element i.e. 1
a[1] refers to 2nd element i.e. 2
a[2] refers to 3rd element i.e. 3
a[3] refers to 4th element i.e. 4
a[4] refers to 5th element i.e. 5
M07_ITL-ESL4791_02_SE_C07.indd 131 12/22/2012 5:02:59 PM

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required