0% found this document useful (0 votes)
10 views6 pages

PF Lab14

The lab manual covers programming fundamentals related to arrays and searching techniques in C++. It explains the definition, declaration, manipulation of arrays, and details linear and binary search algorithms, including their complexities and conditions. Additionally, it provides four practice tasks to reinforce the concepts learned.

Uploaded by

f2024065076
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

PF Lab14

The lab manual covers programming fundamentals related to arrays and searching techniques in C++. It explains the definition, declaration, manipulation of arrays, and details linear and binary search algorithms, including their complexities and conditions. Additionally, it provides four practice tasks to reinforce the concepts learned.

Uploaded by

f2024065076
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Manual

CS/SE/IT-141 Programming Fundamentals


Department of Software Engineering
School of Systems and Technology
UMT, Lahore, Pakistan
Instructor: Miss Sehrish Riaz

LAB-14

Here’s a manual based on the topics you covered this week. It includes explanations, syntax,
examples, and key points for each topic.

Manual: Arrays and Searching Techniques

1. Arrays (1D) in C++


What is an Array?

An array is a collection of elements of the same data type stored in contiguous memory
locations. It allows you to store and manipulate multiple values efficiently using a single
variable.

Syntax
data_type array_name[size];
Example
int numbers[5]; // Declares an array of size 5

2. Declaration and Definition of Arrays


Declaration

Declaring an array specifies its name, type, and size.

int array[5]; // Declaration without initialization


Definition

Defining an array involves declaring and initializing it with values.

int array[5] = {10, 20, 30, 40, 50}; // Declaration and initialization
Key Points

 Array indexing starts from 0.


 If the size is omitted during initialization, it is determined automatically:
 int array[] = {10, 20, 30}; // Size is 3

3. Array Manipulation
Basic Operations

 Accessing Elements:
 int value = array[index];
 Modifying Elements:
 array[index] = new_value;
 Iterating through an Array:
 for (int i = 0; i < size; i++) {
 cout << array[i];
 }
Example: Adding 5 to Each Element
for (int i = 0; i < 5; i++) {
array[i] += 5;
}

4. Searching in an Array

Searching is the process of finding a specific value within an array. Common methods
include Linear Search and Binary Search.

5. Linear Search
What is Linear Search?

Linear Search checks each element in the array sequentially until the desired element is found
or the array is fully traversed.

Algorithm

1. Start from the first element.


2. Compare each element with the target value.
3. If a match is found, return the index.
4. If the end of the array is reached, return -1.

Example: Linear Search


int linearSearch(int array[], int size, int target) {
for (int i = 0; i < size; i++) {
if (array[i] == target) {
return i; // Element found
}
}
return -1; // Element not found
}
Variations

 Find All Occurrences: Iterate through the array and store indices of all matches.
 Count Occurrences: Count how many times the target appears.

6. Binary Search
What is Binary Search?

Binary Search is an efficient algorithm for finding an element in a sorted array. It repeatedly
divides the search range in half.

Algorithm

1. Start with the entire array as the search range.


2. Find the middle element.
3. If the middle element matches the target, return its index.
4. If the target is smaller, narrow the range to the left half.
5. If the target is larger, narrow the range to the right half.
6. Repeat until the range is empty.

Conditions

 The array must be sorted.

Example: Binary Search


int binarySearch(int array[], int size, int target) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (array[mid] == target) {
return mid; // Element found
} else if (array[mid] < target) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}
return -1; // Element not found
}

Key Differences Between Linear and Binary Search


Feature Linear Search Binary Search
Precondition Unsorted or sorted Array must be sorted
Complexity O(n) O(log n)
Approach Sequential Divide and conquer

Here are four practice tasks added to your manual. These tasks are designed to help reinforce
the concepts covered.
Practice Tasks
Task 1: Array Manipulation

Write a program to:

1. Declare an array of size 10.


2. Initialize the array with the first 10 positive integers.
3. Multiply each element by 2 and print the updated array.

Example Output:

Original array: 1 2 3 4 5 6 7 8 9 10
Updated array: 2 4 6 8 10 12 14 16 18 20

Task 2: Linear Search

Write a function that:

1. Accepts an array, its size, and a target number.


2. Returns the index of the target number using Linear Search.
3. If the target number is not found, return -1.

Example Input:

Array: 10, 20, 30, 40, 50


Target: 30

Example Output:

Target found at index: 2

Task 3: Binary Search

1. Write a program to implement Binary Search.


2. The program should:
o Take a sorted array and a target number as input.
o Return the index of the target number if found, or -1 otherwise.

Example Input:

Array: 5, 10, 15, 20, 25


Target: 15

Example Output:

Target found at index: 2

Hint: Ensure the array is sorted before performing the search.


Task 4: Find Maximum and Minimum in an Array

Write a program to:

1. Input an array of integers.


2. Find and print the maximum and minimum values in the array.

Example Input:

Array: 3, 1, 7, 5, 9, 2

Example Output:

Maximum: 9
Minimum: 1

These tasks will help you gain hands-on experience and deepen your understanding of arrays
and searching techniques.

You might also like