Professional Documents
Culture Documents
Arrays
An array is a series of elements of the same type placed in contiguous memory locations that
can be individually referenced by adding an index to a unique identifier.
That means that, for example, five values of type int can be declared as an array without
having to declare 5 different variables (each with its own identifier). Instead, using an array,
the five int values are stored in contiguous memory locations, and all five can be accessed using
the same identifier, with the proper index.
For example, an array containing 5 integer values of type int called A could be represented as
To declare an array in C++, the programmer specifies the type of the elements and the number
of elements required by an array as follows:
This is called a single-dimension array. The arraySize must be an integer constant greater
than zero and type can be any valid C++ data type. For example, to declare a 10-element array
called balance of type double, use this statement:
Double balance[10]
You can initialize C++ array elements either one by one or using a single statement as
follows:
The number of values between braces { } cannot be larger than the number of elements that
we declare for the array between square brackets [ ]. Following is an example to assign a
single element of the array:
If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write:
You will create exactly the same array as you did in the previous
The above statement assigns element number 5th in the array a value of 50.0. Array with
4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first
element which is also called base index. Following is the pictorial representation of the same
array we discussed above:
An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array. For example:
n[10];
for(int i =0; i <10;i+
+)
{
n[ i ]= i +100;
}
cout<<"Element"<<setw(13)<<"Value"<<endl;
Multi-Dimensional Array:
An array of two one-dimensional arrays is called two dimensional array. It is also known as
table or matrix. Each elements of the two-dimensional array is referenced by its index values or
st nd
subscripts. For 2-D array there are 2 subscripts, 1 for row and 2 for column.
Initializing Tables:
To assign values to a table abc[2][3] that has two rows and three columns, values are assigned
row-wise.
int abc[2][3] = {{16,33,2}, {10,12,16}};
Data is entered into individual elements of a two-dimensional array. To enter data, the element is
referenced by its index or subscript value. Similarly, data is retrieved from an array from
individual elements of the array.
Usually nested loops are used to access elements of the 2-D array.
Linear Search:
Linear search or sequential search is a method for finding a particular value in a list that consists
of checking every one of its elements, one at a time and in sequence, until the desired one is
found. It is the most basic of search algorithm you can have.
The worst case performance scenario for a linear search is that it needs to loop through the entire
collection; either because the item is the last one, or because the item isn't found. In other words,
if you have N items in your collection, the worst case scenario to find an item is N iterations.
This is known as O(N) using the Big O Notation. The speed of search grows linearly with the
number of items within your collection.
Binary Search:
A binary search or half-interval search algorithm locates the position of an item in a sorted array.
Binary search works by comparing an input value to the middle element of the array. The
comparison determines whether the element equals the input, less than the input or greater. When
the element being compared to equals the input the search stops and typically returns the position
of the element. If the element is not equal to the input then a comparison is made to determine
whether the input is less than or greater than the element. A binary search is a divide and conquer
search algorithm.
For binary search the array should be sorted. For this purpose, a sorting algorithm has to be
performed on the array. Let’s perform the bubble sort algorithm. It works on the following
principle.
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
As for the time complexity of this algorithm, it is O(n), where n is the size of the array. This is
because in the worst case scenario, the algorithm has to iterate through the entire array to find the
element. In the best case scenario, the element is found at the first index, so the algorithm only
needs to do one comparison. However, the average case time complexity is also O(n) because the
element could be anywhere in the array
OUTPUT:
As for the time complexity of this algorithm, it is O(log n), where n is the size of the array. This
is because the algorithm repeatedly divides the array in half until it finds the element or
determines that it is not in the array.(average case)
Task 2
Task 3
Task 4
Task 5
Averag
e Marks