ARRAY: An array is a collection of similar data elements. These data elements have the same data type. The elements of the array are stored in consecutive memory locations and are referenced by an index (also known as the subscript). The subscript is an ordinal number which is used to identify an element of the array.
DECLARATION OF ARRAYS:
An array must be declared before being used
- Data type—the kind of values it can store, for example, int, char, float, double.
- Name—to identify the array.
- Size—the maximum number of values that the array can hold.
Arrays are declared using the following syntax:
Type name [size];
int marks [10];
The type can be either int, float, double, char, or any other valid data type
ACCESSING THE ELEMENTS OF AN ARRAY:
The code accesses every individual element of the array and sets its value to –1. In the for loop, first the value of marks[0] is set to –1, then the value of the index (i) is incremented and the next value, that is, marks[1] is set to –1. The procedure continues until all the 10 elements of the array are set to –1.
Calculating the Address of Array Elements:
Since an array stores all its data elements in consecutive memory locations, storing just the
base address, that is the address of the first element in the array, is sufficient. The address of other data elements can simply be calculated using the base address. The formula to perform this calculation is,
Address of data element, A[k] = BA(A) + w(k – lower_bound)
Here, A is the array, k is the index of the element of which we have to calculate the address, BA is the base address of the array A, and w is the size of one element in memory
EXAMPLE:
Given an array int marks[ ] = {99,67,78,56,88,90,34,85} , calculate the address of marks[4] if the base address = 1000.
We know that storing an integer value requires 2 bytes, therefore, its size is 2 bytes.
marks[4] = 1000 + 2(4 – 0)
Calculating the Length of an Arraythe length of an array is
Length = upper_bound – lower_bound + 1
where upper_bound is the index of the last element and lower_bound is the index of the first element
in the array
STORING VALUES IN ARRAYSThere are three ways to store values in an array
Initializing Arrays during Declaration: When an array is initialized, we need to provide a value for every element in the array. Arrays are initialized by writing,
type array_name [size] ={ list of values };
Inputting Values from the Keyboard:
An array can be initialized by inputting values from the keyboard.In this method, a while/do–while or a for loop is executed to input the value for each element of the array.
Assigning Values to Individual Elements:
The third way is to assign values to individual elements of the array by using the assignment operator.
A simple assignment statement can be written as
Here, 100 is assigned to the fourth element of the array which is specified as marks[3].
Note that we cannot assign one array to another array, even if the two arrays have the same type and size. To copy an array,you must copy the value of every element of the first array into the elements of the second array.
Code to copy an array at the individual element level
int i, arr1[1 ], arr2[1 ];
arr1[ 1 ] = {0,1,2,3,4,5,6,7,8,9 };
Fill an array with even numbers:
OPERATIONS ON ARRAYS:
There are a number of operations that can be preformed on arrays. These operations include:
- Traversing an array
- Inserting an element in an array
- Searching an element in an array
- Deleting an element from an array
- Merging two arrays
- Sorting an array in ascending or descending order
No comments:
Post a Comment