0% found this document useful (0 votes)
14 views8 pages

Week 4 Lecture in C

This document outlines the design and representation of algorithms using arrays in C++, focusing on searching, sorting, and traversing data. It includes detailed explanations of algorithms like Linear Search, Bubble Sort, and finding the maximum element, along with flowcharts, pseudocode, and C++ code implementations. The conclusion emphasizes the importance of these fundamental algorithms for effective data processing in C++.

Uploaded by

shanfromyt28
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)
14 views8 pages

Week 4 Lecture in C

This document outlines the design and representation of algorithms using arrays in C++, focusing on searching, sorting, and traversing data. It includes detailed explanations of algorithms like Linear Search, Bubble Sort, and finding the maximum element, along with flowcharts, pseudocode, and C++ code implementations. The conclusion emphasizes the importance of these fundamental algorithms for effective data processing in C++.

Uploaded by

shanfromyt28
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/ 8

WEEK 4

Designing and Representing Algorithms Using Arrays 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.

Introduction to Arrays in C++

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:

 Storing and processing data in a structured way.


 Performing searching and sorting algorithms.
 Traversing data for operations like summing values, finding averages, or filtering values.

In C++, arrays can be declared as follows:

int arr[5]; // An array of integers with 5 elements

Common Array Algorithms

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.

Example 1: Linear Search Algorithm

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 linearSearch(int arr[], int size, int target) {


for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Return the index if found
}
}
return -1; // Return -1 if target is not found
}

int main() {
int arr[] = {2, 4, 6, 8, 10, 12};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 8;

int result = linearSearch(arr, size, target);


if (result != -1) {
cout << "Target found at index: " << result << endl;
} else {
cout << "Target not found" << endl;
}

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

Example 2: Bubble Sort Algorithm

Problem Statement:
Sort an array of integers in ascending order using the Bubble Sort algorithm.

Step 1: Flowchart Design

+----------------------------+
| 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

Step 3: C++ Code Implementation

#include <iostream>
using namespace std;

void bubbleSort(int arr[], int size) {


for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]); // Swap the elements
}
}
}
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, size);

cout << "Sorted array: ";


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}

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:

 Output: Sorted array: 11 12 22 25 34 64 90

Example 3: Finding the Maximum Element in an Array

Problem Statement:
Find the maximum element in an array of integers.

Step 1: Flowchart Design

+--------------------------+
| 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

Step 3: C++ Code Implementation

#include <iostream>
using namespace std;

int findMax(int arr[], int size) {


int max = arr[0]; // Initialize max as the first element
for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i]; // Update max if a larger value is found
}
}
return max;
}

int main() {
int arr[] = {10, 20, 4, 45, 99};
int size = sizeof(arr) / sizeof(arr[0]);

int max = findMax(arr, size);


cout << "The maximum element is: " << max << endl;
return 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:

 Output: The maximum element is: 99

Conclusion:

In this topic, we explored several key algorithms for manipulating arrays in C++:

1. Linear Search: A basic search algorithm to find if a target exists in an array.


2. Bubble Sort: A simple sorting algorithm that sorts an array in ascending order.
3. Finding the Maximum Element: A traversal algorithm to find the maximum value in an array.

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++.

You might also like