Lecture 7

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

1

Lecture # 7

Arrays (Memory representation of Arrays)

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


2
Memory Representation of Array

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.

Below is the figure that represents the memory representation in an array.

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.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


3
int a [5];

..... Each box represents the amount of memory


needed to hold one array element. For ints this is
a[0] =1; usually 4 bytes.

for (int i = 1; i< 5; i++)

a[i] = a[i-1]* 2;

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


4 Array Traversal
Introduction to traversing of linear arrays

Linear Array (One dimension array)

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:

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


5

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

We can perform various operations on a linear array:

 Traversing- processing each element of the array list.

 Inserting- adding new elements in the array list.

 Deleting- removing an element from the array list.

 Sorting- arranging the elements of the list in some sorting order.

 Merging- combining the elements of two array lists in a single array list.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


7 Array traversal

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;
}

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


9 Example 2: Traversing an Array in C++ with Recursive Method
#include<iostream>
using namespace std;

int recursiveSearch(int arr[], int l, int r, int x)


{
if (r < l)
return -1;
if (arr[l] == x)
return l; Input the element to be searched: 7
if (arr[r] == x) Element is not present in array
return r;
return recursiveSearch(arr, l+1, r-1, x);
}

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

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh

You might also like