PF Lab14
PF Lab14
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.
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
int array[5] = {10, 20, 30, 40, 50}; // Declaration and initialization
Key Points
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
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
Conditions
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
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
Example Input:
Example Output:
Example Input:
Example Output:
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.