Drill 3
Drill 3
STUDENT NAME :
SECTION :
DATE OF SUBMISSION: SCORE
Score
I. Learning Objective
At the end of the session, the student must be able to
• Understand the importance and process of traversal in arrays
• Familiarize commands and syntax in traversing and searching an array.
• Create and execute programs that would search an element of an array.
II. Discussion
Traversing
Array traversal is the process of accessing each element in an array one by one. Traversing an array is
one of the common tasks in programming which will allow you to manipulate and perform operations on
specific elements of an array.
Example No. 1. Output the elements of the array.
int arr[ ]; {1,2,3,4,5};
int length = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < length; i++)
{
printf(“%d”, arr[i]);
}
#include <stdio.h>
int main()
{
int arr[ ]; {1,2,3,4,5};
int length = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for (int i = 0; i < length; i++)
{
Sum += arr[i];
}
Return 0;
}
Common array traversal operations include :
• Traversing an array in forward direction
• Traversing an array in reverse direction
Traversing arrays involves looping through each element in the array and processing each element one
at a time. This allows you to access all array elements and perform tasks such as printing, copying,
comparing, or sorting.
Example No. 3. Output the maximum or highest value among the elements of an array.
#include <stdio.h>
int main() {
int arr[] = {4, 2, 9, 1, 7};
int length = sizeof(arr) / sizeof(arr[0]);
int max = arr[0];
for (int i = 1; i < length; i++) {
if (arr[i] > max) {
max = arr[i]; // Updating the maximum value
}
}
printf("Maximum value: %d\n", max);
return 0;
}
2
Example No. 4. Output updated values of the elements of the array. All even element values will be
doubled.
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);
Inserting
Insertion is the process of adding new elements into an existing array. This can be done by providing an
index for where the insertion should occur and then shifting other elements in the array to make space for
the insertion. Insertion can either be :
3
}
int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;
printf("\n Before Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
n = insertSorted(arr, n, key, capacity);
printf("\n After Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Expected Output
Before Insertion: 12 16 20 40 50 70 ;
After Insertion: 12 16 20 40 50 70 26
Searching
Array searching can be defined as the operation of finding a particular element or a group of elements
in the array.
Importance of Searching
• Data Retrieval : Searching enables efficient retrieval of specific data from large datasets, allowing
for quick access and manipulation information.
• Database Queries : Database utilize search algorithms to quickly locate records based on
specified criteria ensuring faster response times for queries.
• Sorting Algorithms : Searching is often employes in sorting algorithms to determine the correct
position of elements during the sorting process.
• Information retrieval : in search engines and informal retrieval systems, searching algorithms help
user find relevant information quickly.
• Optimization Problems : Searching is a crucial component in solving optimization problems such
as maximum or minimum value in the dataset.
Types of searching :
1. Linear Search also known as sequential search is a simple straightforward searching algorithm.
It involves checking each element in an array until the target element is found or reached.
Sample Program that will search if a certain element exists in an array. It will return 1 if it is found, and -1
if the element is not found in the array.
Int linearSearch (int arr[], int n, int target {
For (int I = 0; I < n; i++) {
If (aff[i] == target) {
Return 1;
}
}
Return – 1
}
4
2. Binary Search is a more efficient searching algorithm, but it requires the array to be sorted in
ascending order. It works by repeatedly dividing the search interval in half and narrowing down
the search space until the target element is found.
(Print the copy of the source Code.. and attach a screenshot of the code at runtime).
References
1. Learn.Mircrosoft
2. Coursera.org
3. GeeksforGeeks.com
4. CodeChum
Give a man a fish, and you’ll feed him for a day. Teach a man to fish, and you’ll feed him for a lifetime