11 Array: L L L L L
11 Array: L L L L L
11 Array: L L L L L
11.1 Introduction
The ordered collection of identical elements is called an array. Sometimes, it is required to store a large number of variables of the same type, one can use an array. For example: int A[5] ; The above statement declares an array A which reserves 5 memory locations for storing integer data type. These locations are A[0], A[1], A[2], A[3] and A[4]. The number within the square bracket is called index or subscript. The array can be one dimensional (one subscript) or two dimensional (two subscripts).
11.2 Objectives
After going through this lesson, you would be able to: explain the concept of an array define, access and initialize array elements perform common operations on arrays explain two dimensional array define, access and initialize two dimensional array elements
Array :: 91
Traversal It means to access each location of an array, may be for display purpose. Consider a program which will read five values from the user and finds out the maximum value. # include < iostream.h > void main ( ) { int T, A[5], l; cout < < Enter five values; for ( l = 0; l < 5; l ++) cin > > A [ l ]; T = A [ 0 ]; for (l = 1; l < 5; l++) { if (T < A [l]) T = A [ l ]; } cout < < Maximum value < < T; } Searching This method finds out whether the data entered by the user is present in an array or not. There are two types of searching method. (i) (ii) Linear or Sequential search Binary search
Linear or sequential search This method is slower, inefficient and works on unsorted list. If the data we are searching is not present in the list, we come to know at the end of the list. / / Linear search # include < iostream.h > void main ( ) {
Array :: 93
int A[5], l, data, flag = 0; cout < < Enter five values; for (l = 0; l < 5; l ++) cin > > A [l]; cout >> Enter data to be searched; cin >> data; for (l=0; l < 5; l ++) { if (A[l] = = data) flag = 1; } if (flag = = 1) cout < < Data present; else cout << Data not present; }
Binary search
This method requires the array to be either in ascending or descending order. This method calculates the mid location form initial and final locations and compares the data with the value present in mid location. Consider the case where the list is in ascending order. If data is less than a [mid] then data is present in the upper half otherwise data is present in the lower half. The value of either final or initial will be changed. The mid value is calculated again and searching continues till the data is found or initial is greater than final. The values of initial, final and mid for searching a value of 35 in an ascending order sorted list containing 9 elements is shown below:
10 location (0)
15 (1)
18 (2)
20 (3)
27 (4)
30 (5)
35 (6)
40 (7)
45 (8)
Initial + Final Initial Final Mid = 2 0 5 8 8 4 6 27 35 Search is successful / / Binary search # include < iostream.h > const int N = 9; void main ( ) { int A[N], l, initial, final, mid, data; cout < < Enter nine values in ascending order; for (l = 0; l < N; l ++) cin >> A [l]; cout << Enter data to be searched; cin >> data; initial = 0; final = N - 1; mid = (initial + final) / 2; While (initial < = final) & & (A[mid]! = data)) { if (A [mid] > data) final = mid - 1; else initial = mid + 1; } if (A [mid] = = data) cout << data is present; if (initial > final) cout <<data not present in the list; } The advantage of Binary search is that each search cuts the list in to half. A list of 10,000 names can be searched in just 12 searches. A [Mid]
Array :: 95
Sorting It is a method to arrange the list either in ascending or descending order. Bubble sort Consider an array of five locations to be sorted in ascending order:
-1 Index 0
9 1
5 2
0 3
2 4
In this sorting method, A[0] is compared with A[1]. If A[0] is greater than A[1], the values are swapped. Then A[1] is compared with A[2], A[2] is compared with A[3], and A[3] is compared with A[4]. In all cases if the ith location has value greater than I + 1, the values are swapped. The entire process is repeated N-1 times where N is the number of data in an array. # include < iostream.h > const int N = 5; void main ( ) { int A [N], l, j, T; cout << Enter values; for (l = 0; l = N; l ++) cin >> A [ l ]; / / sorting for ( l = 0; l < N - 1; l ++) for (j = 0; j < N - 1; j ++) If (A[j] > A [j + 1]) { T = A [ j ]; A [ j ] = A [ j + 1 ]; A [ j + 1 ] = T; } cout << Sorted array is; for (l = 0; I < N; l ++) cout << A [ l ]; }
Selection sort Consider an array having N elements to be sorted in ascending order. Initially, first element is compared with others so that it holds the smallest value. In the next pass, second element is compared with others so that it holds the smallest value. This procedure is repeated for the entire array. # include < iostream.h > const int N = 5; void main ( ) { int A [N], l, j, T; cout << Enter values; for ( l = 0; l < N; l ++ ) cin >> A [ l ]; / / sorting for ( l = 0; l < N - 1; l ++ ) for (j = l; j < N; j ++ ) If (A [l] > A [j]) { T = A [l]; A [l] = A [j]; A [j] = T; } // printing the sorted data for ( l = 0; l < N; l ++ ) cout < < A [ l ]; } Insertion It means addition of a data item in the middle or at the end of the array. If data is to be added after a given data item then the location of the data item is first determined by applying search procedure and then the insertion procedure is implemented. # include < iostream.h > void main ( ) { int x [20] ;
Array :: 97
int l, loc, n, data; cout << Enter the no. of elements; cin >> n; for (l = 0; l < n; l ++ ) { cout << Enter the array element; cin >> x [ l ]; } cout << Enter the location after which data is to be inserted; cin >> loc; for (l = n - 1; l > = loc; l - - ) x [ l + 1 ] = x [ 1 ]; cout << enter the new data to be added; cin >> x [loc]; n ++ ; cout << Array elements after insertion; for (l = 0; l < n; l ++) cout << x [ l ]; } Let data 12 to be inserted at location 2 0 1 2 3 4 6 8 9 7 10 0 1 2 3 4 5 Before insertion Deletion It means removal of a data. First the location of the item to be deleted is determined by applying an appropriate search procedure and then the value present at particular 6 8 12 9 7 10 After insertion
location is deleted. # include < iostream.h > void main ( ) { int x [20] ; int l, j, n, loc, data; cout << Enter the no. of elements; cin >> n; for (l = 0; l < n; l ++) { cout << Enter value; cin >> x [ l ]; } cout << Enter the location to be deleted; cin >> loc; if (loc ! = 0) { data = x [loc ]; for {j = loc; j < n - 1; j ++ ) x [ j ] = x [ j + 1]; } n = n - 1; cout << Elements after deletion; for ( l = 0; l < n; l ++) cout << x [ l ]; } 0 1 2 3 4 5 12 7 9 10 Before deletion After deletion Let data 7 to be deleted present at location 2 0 1 2 3 5 12 9 10
Array :: 99
4.
5.
What will be the output of the following programs: (a) # include < iostream.h > void main ( ) { int a [ 5 ], t ; for (i= 0; i < 5; i ++ ) a [i] = 5 * i; for (i = o; i < 5; i ++) cout << a [ i ]; } (b) # include < iostream.h > void main ( ) { char title [ ] = Computer; for ( int i = 0; title [ i ]; i ++) cout << \n << (title + i); }
Initialization of Two Dimensional Array The initialization is done at the time of declaration of an array. For example: int A [2] [4] = {1, 2, 3, 4, 5, 6, 7, 8); For more clarity int A [2] [4] = { {1, 2, 3, 4 }, {5, 6, 7, 8} }; The above data can be grouped. The inner braces are ignored by the compiler. For two dimensional array, two loops are required. The following program finds out the maximum value stored in two dimensional array. # include , iostream.h > const int M = 5; void main ( ) { int A [M] [M], i, j, T; cout >> \n Enter Array Elements; for (i = 0; i < = M - 1; i ++ ) for (j = 0; j < = M - 1; j ++ ) cin >> A [i] [j];
Array :: 101
T = A [0] [0]; for ( i = 0; i < = M - 1; i + + ) for (j = 0; j < = M - 1; j + + ) { If (T < A [ i ] [ j ] T = A [ i ] [ j ]; } cout << Largest value << T << \n; }
2. 3. 4.
5. 6.
Which method is better: Bubble or Selection sorting? Why ? Write a program that will read 20 float values in a one dimensional array and find out the following: (i) (ii) (iii) Number of values greater than zero. Number of values equal to zero. Number of values less than zero.
7.
Write a program that will find out the sum of two diagonals of two dimensional array of A [N] [N]. Write a program that will find out whether the data entered by the user present in a one dimensional array of 10 elements using: (i) (ii) Linear search Binary search
8.
9.
Write a program that will sort the given ten numbers in descending order using: (i) (ii) Bubble sort Selection sort
Array :: 103
6.
(a) (b)
10
15
20
(b)