Fundamentals of Programming

Nicholas, MIS 07-015

What is an array? How to declare an array? Describe the characteristics of an array. A series of objects all of which are the same size and type is called an array. It is simply a grouping of like-type data. Each object in an array is called an array element. For example, we could have an array of integers or an array of characters or an array of anything that has a defined data type. Some practical examples where the concept of the array can be used are:    list of employees in an organization test scores of a class of students list of temperatures recorded every hour in a day etc.

How to declare: A typical array can be declared as following:   [elements] where   is a valid type (such as int, float…..),   is a valid identifier and [elements] field, which is always enclosed in square brackets [ ] specifies the length of the array in terms of the number of elements. For example, an array containing 5 integer values of type int called mango could be represented as: 0 mango int 1 int 2 1 int 3 2 int 4 3 4 int 5

Therefore, the mango array, with five elements of type int, can be declared as: int mango [5];

Characteristics of an array: Random access: The elements of an array can be accessed randomly without having to check every element one by one (sequential access). For example: abc memory location elements 0 10 1 20 2 30 3 40 4 50

here abc is an array having 5(n= 5) integer type values. so if we want to access the 3rd element: abc [3]= 40 ; for 4th element : abc [4]= 50. Memory allocation contagious: Array elements are stored in subsequent memory locations. That is, there are no gaps between elements. For

Nicholas, MIS 07-015

example: int abc [5]; This declaration will cause the compiler to allocate space for 5 consecutive int variables in memory. So, the number of elements in an array must be fixed at compile time. Homogeneous data: An array holds elements that have the same data type. An array cannot hold any data with...