One Dimensional Array
If single subscript is required to reference an element of an array then it is known as one dimensional array. One dimensional array is a homogeneous collection of finite data elements which are referenced by an index number. One dimensional arrays are also know ad linear array.
An array ‘Age’ having 10 elements is shown below:
Length = UB – LB + 1
UB is the upper bound or upper index of the array
LB is the lower bound or lower index of the array
Calculate Size of array
Bytes = length * size_of(data_type)
Data type specifies the type of elements stored in array
Size_of(data_type) depends upon the programming language. In case of ‘C’ language 2 bytes are required for integer type and 4 bytes are required for float type elements.
Declaring One Dimensional Array
There are four components of a statement that declares an array.
The semicolon tells the computer that the preceding is a statement. Declaring the array name, type of an array (data type) and setting the number of elements in the array is known as dimensioning the array. The array must be declared before its uses it in C/C++ program like other variables.
There are two techniques for declaring an array
Declare an array either where memory is reserved at compile time
This technique is similar in Java, C, and C++, except in Java you must initialize the array when the array is declared
data_type is used to declare the nature of the data elements stored in the array like character type, integer and float type. The data type is a keyword that tells the computer the amount of memory to reserve for each element of the array. If we use int data type for array then it told the computer to reserve enough memory to store an integer for each array.
array_name is the name of the declared array. The array name is the name you use within a program to reference an array element.
array_size is a positive integers which indicate the size of the array (how many locations are needed).
Here’s valid declaration statement in C and C++: