Kerala Plus One Computer Science Notes Chapter 8 Arrays
Summary
An array is a collection of elements with same data type Or with the same name we can store many elements, the first or second or third, etc can be distinguished by using the index(subscript). The first element’s index is 0, the second element’s index is 1, and so on.
Declaring arrays:
Suppose we want to find the sum of 100 numbers then we have to declare 100 variables to store the values. It is laborious work. Hence the need for array arises.
Syntax: data_type array_name[size];
To store 100 numbers the array declaration is as follows
int n[100]; By this we store 100 numbers. The index of the first element is 0 and the index of last element is 99.
Memory allocation for arrays:
The amount of memory requirement is directly related to its type and size,
- int n[100]; It requires 2Bytes(for each integer) × 100 = 200 Bytes.
- float d[100]; It requires 4Bytes(for each float) × 100=400 Bytes.
Array initialization:
Array can be initialized in the time of declaration. eg: int age[4] = {16, 17, 15, 18};
Accessing elements of arrays:
Normally loops are used to store and access elements in an array.
eg:
int mark[50], i;
for(i=0;i<50;i++)
{
cout<<“Enter value for mark”<<i+1;
cin>>mark[i];
}
cout<<“The marks are given below:”;
for(i=0;i<50;i++)
cout<<mark[i];
Array operations:
Traversal:
Accessing all the elements of an array is called traversal.
Sorting:
Arranging elements of an array in an order(ascending or descending)
1. Bubble sort:
It is a simple sorting method. In this sorting considering two adjascent elements if it is out of order, the elements are interchanged. After the first iteration the largest(in the case of ascending sorting) or smallest(in the case of descending sorting) will be the end of the array. This process continues.
2. Selection sort:
In selection sort the array is divided into two parts, the sorted part and unsorted part. first smallest element in the unsorted part is searched and exchanged with the first element. Now there is 2 parts sorted part and unsorted part. This process continues.
Searching:
It is the process of finding the position of the given element.
1. Linear search:
In this method each element of the array is compared with the element to be searched starting from the first element. If it finds the position of the element in the array is returned.
2. Binary search:
It uses a technique called divide and conquer method. It can be performed only on sorted arrays. First we check the element with the middle element. There are 3 possibilities. The first possibility is the searched element is the middle element then search can be finished.
The second possibility is the element is less than the middle value so the upper bound is the middle element. The third possibility is the element is greater than the middle value so the lower bound is the middle element. Repeat this process.
Two dimensional (2D) arrays:
Some occasions we have to store 6 different marks of 50 students. For this we use 2D arrays. An array with two subscripts is used.
eg: int mark[r][c]; Here r is the row and c is the column.
Declaring 2D arrays:
Syntax: datatype array_name[rows][columns];
The elements of this array is referred as mark[0][0], mark[0][1], mark[r – 1][c – 1].
eg: int m[5][5]; This array can store 5 × 5 = 25 elements.
Matrices as 2D arrays:
Matrix is a concept in mathematics that can be represented by 2D array with rows and columns. A nested loop(a loop contains another loop) is used to store and access elements in an array.
Multi-dimensional arrays:
3 Dimensional(3D) array is an example of this.
Syntax: data_type array_name[size1 ][size2][size3];
eg: int m[5][5][5]; This array can store 5 × 5 × 5 = 125 elements.