Arrays
Dincy R Arikkat
Christ College(Autonomous),Irinjalakuda
One-Dimensional Arrays
•A list of values with the same data type
that are stored using a single group
name (array name).
•Elements of the array are stored in
consecutive memory location.
•Only One dimension
•General array declaration statement:
data-type array-name[number-of-items];
•Example
float arr[SIZE];
One-Dimensional Arrays(cont.)
•Individual elements of the array can be accessed by
specifying the name of the array and the element's
index:
arr[3]
•Warning: indices assume values from 0 to number-of-
items -1 !!
One-Dimensional Arrays(cont.)
arr[0]arr[1]arr[2]arr[3]arr[4]
Skip over 3 elements to get the
starting location of element 4
The array name arridentifies the
starting location of the array
Start here
element 4
Some array terminology
•Size-No.of elements in an array is called the size of
the array.
•Type-The kind of values it can store.
•Array name-name of array
•Base or index address-memory location where the
first element of the array is located.
Some Array Terminology
mark[50]
mark[5]
mark[2]
mark[2] = 32;
Array name
Index-also called a subscript
-must be an int,
-or an expression that evaluates to an int
Indexed variable-also called an
elementor subscripted variable
Note that "element" may refer to either a single indexed
variable in the array or the valueof a single indexed variable.
Value of the indexed variable
-also called an element of the array
size
Memory representation
1
st
eleme
nt
2
nd
eleme
nt
3
rd
eleme
nt
…… Nth
eleme
nt
Array[1]Array[0] Array[2] Array[n-1]
Example
Memory address calculation
•Address of A[I]=
Base Address + size * (I -Lower Bound)
•Normally Lower Bound is 0,In some programming
language it will differ.
Memory address calculation
Question
•Given an array int marks[]={99,67,85,32,48,55,52};
Calculate the address of marks[4],if the base address is
1000.
Solution
•Address of A[I]=
Base Address + size * (I -Lower Bound)
Here,
Base address=1000, I=4, Lower Bound=0
Size of Int=2
Marks[4]=1000+ 2*(4-0) = 1008
Question
•Suppose an array A[10 … 20] is stored in a memory
whose starting address is 4000.Find the address of
A[15]?
Solution
•Base address=4000
•I=15,Lower Bound=10
•Size-2
•Address of A[I]=B.A + size*(I-Lower
bound)
•Address of A[15]=4000+2*(15-10)
=4010