C++ Arrays
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. The array can be one dimensional (one subscript) or two dimensional (two subscripts).
Initialization of One Dimensional Array
An array can be initialized along with declaration. For array initialization, it is required to place the elements separated by commas enclosed within braces.
int A[5] = {1, 2, 3, 4, 5};
It is possible to leave the array size open. The compiler will count the array size.
int B[] = {6, 7, 8, 11, 10};
Initialization of String
An array of characters known as character string may be initialized by placing the string in double quotes.
char A[20] = “Computer Science”;
char B[] = “Calcutta”;
A string is a series of characters stored in consecutive bytes of memory. This implies that you can store a string in an array of characters, with each character kept in its own array element. The last character of every C++ string is Null character. Therefore,
char city[] = {‘c, ‘a’, ‘l’, ‘c’, ‘u’, ‘t’, ‘t’, ‘a’};
is an array of characters while
char city[] = {‘c, ‘a’, ‘l’, ‘c’, ‘u’, ‘t’, ‘t’, ‘a’, ‘\0’};
is a string because it is terminated by a null character.
Processing an Array
The various operations possible on arrays are:
- Traversal
- Searching
- Sorting
- Insertion
- Deletion
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:
- Linear or Sequential search
- Binary search
Linear or Sequential Search
This method is slower, inefficient and works on unsorted list. If the data you are searching is not present in the list, you come to know at the end of the list.
// Linear search
#include <iostream.h >
void main()
{
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.
// 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.
Sorting
It is a method to arrange the list either in ascending or descending order.
Bubble Sort
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] ;
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];
}
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 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];
}
Two Dimensional Array
It has two subscript or index, first for row and second for column. For example:
int A[5][4];
The above has five rows and four columns. The total number of elements are 20.
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.
Traversal
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];
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”;
}
For two dimensional array, two loops are required.