Professional Documents
Culture Documents
Lecture 7
Lecture 7
Lecture 7
Lecture # 7
Arrays are represented with diagrams that represent their memory use on the computer. These are
represented by the square boxes that represent every bit of the memory. We can write the value of
the element inside the box.
Each box represents the amount the memory needed to hold one array of elements. The pointers in
it hold the memory address of the other data. Let us write code and represent the memory of it.
a[i] = a[i-1]* 2;
A linear array, is a list of finite numbers of elements stored in the memory. In a linear array, we can store
only homogeneous data elements. Elements of the array form a sequence or linear list, that can have the
same type of data.
Each element of the array, is referred by an index set. And, the total number of elements in the array list, is
the length of an array. We can access these elements with the help of the index set.
For example, consider an array of employee names with the index set from 0 to 7, which contains 8 elements
as shown in fig:
Now, if we want to retrieve the name 'Alex' from the given array, we can do so by calling "employees [3]".
It gives the element stored in that location, that is 'Alex', and so on.
We can calculate the length, or a total number of elements, of a linear array (LA), by the given formula:
Length (LA)=UB-LB+1
Here, UB refers to the upper bound, the largest index set of the given array list. LB refers to the lower
bound, smallest index set of the given array list.
For example, we have an array of employees, in which lower bound is 153 and the upper bound is 234, so
we can calculate the total number of elements in the list, by giving the formula.
© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
6
234-153+1=82
Merging- combining the elements of two array lists in a single array list.
Array traversal, or Traversing an Array in C++, is the process of accessing each element of an array, one
at a time, in a sequential manner. It is a common operation in C++ programming and is used to process
the elements of an array in a specific order.
In C++, there are two primary methods for array traversal: iterative and recursive.
Iterative array traversal involves using a loop to move through the array from start to finish. The loop will
typically iterate through the array, beginning with the first element and ending with the last element. At each
iteration, the current element’s value is accessed and processed. This can be accomplished using a for loop.
Recursive array traversal involves using a recursive function to move through the array. This works by calling
the same function on the remaining elements of the array until the last element is reached. The function is
then exited, and the last element is processed.
© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
8 Example 1: Traversing an Array in C++ Using Loop
#include <iostream>
using namespace std;
int main()
{
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int item;
cout << "Enter the item to be searched: ";
cin >> item; Enter the item to be searched: 7
int flag = 0; Item found
for (int i = 0; i < 10; i++)
{
if (array[i] == item)
{
flag = 1;
break;
}
}
if (flag == 1)
cout << "Item found";
else
cout << "Item not found";
return 0;
}
int main()
{
int arr[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int x;
cout<<"Input the element to be searched: ";
cin>>x;
int n = sizeof(arr)/sizeof(arr[0]);
int result = recursiveSearch(arr, 0, n-1, x);
(result == -1) ? cout<<"Element is not present in array“ : cout<<"Element is present at index " <<result;
return 0;
}
© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
10
Thank You