Surigao Del Norte State University
C COLLEGE OF ENGINEERING & INFORMATION TECHNOLOGY
Narciso Street, Surigao City
Basic Operations in Array
DRILL 3
STUDENT NAME :
SECTION :
DATE OF SUBMISSION: SCORE
Score
Engr. Catherine M. Verallo, MSCpE
Instructor
Topic 3 Array Traversal & Array Searching
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]);
}
Example No. 2. Calculate the sum of the elements in an array.
#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]);
for (int i = 0; i < length; i++)
{
if (arr[i] % 2 == 0)
{
arr[i] *= 2; // Doubling the even elements
}
}
printf("Updated array: ");
for (int i = 0; i < length; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 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 :
1. Inserting Elements in an Array at the End
2. Inserting Elements in an Array at any Position in the Middle
3. Inserting Elements in a Sorted Array
Example No. 5. Sample program to insert element in an unsorted array.
#include <stdio.h>
int insertSorted(int arr[], int n, int key, int capacity)
{
if (n >= capacity)
return n;
arr[n] = key;
return (n + 1);
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.
Sample Program using binary search
int binarySearch(int arr[], int left, int right, int target) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid; // Return the index of the target element if found
}
else if (arr[mid] < target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return -1; // Return -1 if the element is not found in the array
}
Part III. Exercises :
1. Create a C Program that would ask the user to input the size of an array and its elements then
count the number of duplicates (20 points).
Sample Output
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 1
element - 1 : 1
element - 2 : 2
element - 3 : 3
element - 4 : 3
Total number of duplicate elements found in the array: 2
(Print the copy of the source Code.. and attach a screenshot of the code at runtime).
IV. Review Questions
1. A program P reads in 500 integers in the range [0.. 100] representing the scores of 500
students. It then prints the frequency of each score above 50. What would be the best
way for P to store frequencies?
_____________________________________________________________________
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