11-array
11-array
What is an Array
• Ordinary variables are capable of holding only one value at a time.
• However, there are situations in which we would want to store more than
one value at a time in a single variable.
• Suppose we wish to arrange the percentage marks obtained by 100
students in ascending order. In such a case we have two options to store
these marks in memory:
– Construct 100 variables to store percentage marks obtained by 100 different students,
i.e. each variable containing one student’s marks.
– Construct one variable (called array or subscripted variable) capable of storing or holding
all the hundred values.
What is an Array
• An array is a collective name given to a group of elements of the same
data type.
• These similar elements could be all ints, or all floats, or all chars, etc.
• Each member in the group is referred to by its position in the group. C the
counting of elements begins with 0 and not with 1.
A Simple Program Using Array
What is an Array: Summary
• An array is a collection of similar elements.
• The first element in the array is numbered 0, so the last element is 1 less
than the size of the array.
• An array is also known as a subscripted variable.
• Before using an array its type and dimension must be declared.
• However big an array its elements are always stored in contiguous
memory locations.
Array Initialization
• int num[6] = { 2, 4, 12, 5, 45, 5 } ;
• int n[ ] = { 2, 4, 12, 5, 45, 5 } ;
• float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;
• Till the array elements are not given any specific values, they are supposed
to contain garbage values.
• If the array is initialized where it is declared, mentioning the dimension of
the array is optional.
Array Elements in Memory
• int arr[8] ;