Week 4 Lecture in C
Week 4 Lecture in C
Objective: The objective of this topic is to design and represent algorithms that effectively utilize arrays
to store, manipulate, and process collections of data in C++. We will explore common algorithms such as
searching, sorting, and traversing arrays, and represent them with flowcharts, pseudocode, and C++
code.
An array is a data structure that stores a collection of elements of the same type in a contiguous block of
memory. Arrays are commonly used for tasks such as:
1. Searching Algorithms
Linear Search: This algorithm searches through each element of the array sequentially until the
target element is found.
Binary Search: This algorithm works on sorted arrays and repeatedly divides the search interval
in half.
2. Sorting Algorithms
Bubble Sort: A simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order.
Selection Sort: A sorting algorithm that selects the smallest element in the unsorted part of the
array and swaps it with the first unsorted element.
3. Traversing an Array
Sum of Elements: Traverse the array and calculate the sum of all elements.
Find Maximum/Minimum: Traverse the array and find the largest or smallest element.
Problem Statement:
Given an array, find whether a target value exists within the array.
Step 1: Flowchart Design
+--------------------------+
| Start |
+--------------------------+
|
V
+--------------------------+
| Input array and target |
+--------------------------+
|
V
+--------------------------+
| Set i = 0 |
+--------------------------+
|
V
+--------------------------+
| Is arr[i] == target? |--No-->[Increment i]-->[Is i < size?]-->Yes-->[Return No]
+--------------------------+
|
Yes
|
V
+--------------------------+
| Return "Target Found" |
+--------------------------+
|
V
+--------------------------+
| End |
+--------------------------+
Step 2: Pseudocode
START
Input array and target
Set i = 0
WHILE i < size of array DO
IF arr[i] == target THEN
PRINT "Target Found"
RETURN
END IF
Increment i
END WHILE
PRINT "Target Not Found"
END
Step 3: C++ Code Implementation
#include <iostream>
using namespace std;
int main() {
int arr[] = {2, 4, 6, 8, 10, 12};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 8;
return 0;
}
Explanation:
The algorithm performs a linear scan of the array and compares each element to the target.
The search stops when the target is found, or when the end of the array is reached.
Example Output:
Input: Target = 8
Output: Target found at index: 3
Problem Statement:
Sort an array of integers in ascending order using the Bubble Sort algorithm.
+----------------------------+
| Start |
+----------------------------+
|
V
+----------------------------+
| Input array |
+----------------------------+
|
V
+----------------------------+
| Set i = 0, n = size-1 |
+----------------------------+
|
V
+----------------------------+
| FOR i = 0 TO n-1 |
+----------------------------+
|
V
+----------------------------+
| FOR j = 0 TO n-i-1 |
+----------------------------+
|
V
+----------------------------+
| Is arr[j] > arr[j+1]? |
|Yes|
| V
+----------------------------+
| Swap arr[j] and arr[j+1] |
+----------------------------+
| No |
+-----+
|
V
+----------------------------+
| End loop |
+----------------------------+
|
V
+----------------------------+
| Print sorted array |
+----------------------------+
|
V
+----------------------------+
| End |
+----------------------------+
Step 2: Pseudocode
START
Input array
Set n = size of array
FOR i = 0 TO n-1 DO
FOR j = 0 TO n-i-1 DO
IF arr[j] > arr[j+1] THEN
Swap arr[j] and arr[j+1]
END IF
END FOR
END FOR
Print array
END
#include <iostream>
using namespace std;
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, size);
return 0;
}
Explanation:
The algorithm repeatedly steps through the list, compares adjacent elements, and swaps them if
they are in the wrong order. This process repeats until the array is sorted.
The outer loop controls the number of passes, and the inner loop compares adjacent elements
and swaps them if necessary.
Example Output:
Problem Statement:
Find the maximum element in an array of integers.
+--------------------------+
| Start |
+--------------------------+
|
V
+--------------------------+
| Input array |
+--------------------------+
|
V
+--------------------------+
| Set max = arr[0] |
+--------------------------+
|
V
+--------------------------+
| FOR i = 1 TO size-1 |
+--------------------------+
|
V
+--------------------------+
| Is arr[i] > max? |
| Yes
V
+--------------------------+
| Set max = arr[i] |
+--------------------------+
|
No
|
V
+--------------------------+
| End loop |
+--------------------------+
|
V
+--------------------------+
| Print max |
+--------------------------+
|
V
+--------------------------+
| End |
+--------------------------+
Step 2: Pseudocode
START
Input array
Set max = arr[0]
FOR i = 1 TO size-1 DO
IF arr[i] > max THEN
Set max = arr[i]
END IF
END FOR
Print max
END
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 4, 45, 99};
int size = sizeof(arr) / sizeof(arr[0]);
Explanation:
The algorithm starts by assuming the first element is the largest. It then iterates over the rest of
the elements, updating the maximum value whenever a larger element is found.
Example Output:
Conclusion:
In this topic, we explored several key algorithms for manipulating arrays in C++:
By utilizing flowcharts, pseudocode, and C++ code, we have effectively represented and implemented
algorithms that use arrays for tasks such as searching, sorting, and processing collections of data.
Understanding these fundamental algorithms is essential for working with arrays and optimizing data
processing in C++.