PRESENTATION SLIDES_Module 8 Purple
Module 8: Arrays, Vectors, & Data Parallelism
8.1 Module Objectives
Implement array data structures successfully.
Execute basic algorithms related to arrays.
Solve problems using array techniques.
Utilize C++ libraries for c-strings, vectors, and algorithms.
Debug and test code that involves arrays.
Identify and implement algorithmic solutions representing data parallelism.
8.2 Array Concepts
Definition: An array is a single variable capable of holding multiple values of the same data type, stored in contiguous memory locations.
Elements: Each value stored in an array is referred to as an element, identified via an index (subscript), which starts from 0.
Built-in Support: Arrays are built directly into the C++ language, unlike in Python.
8.3 Declaring Arrays
Syntax: int tests[5]; creates an integer array named 'tests' with five elements.
Key Components:
int: Data type of the elements.tests: Name of the array.[]: Indicates it's an array.5: Number of elements in the array.
Memory Allocation: Each element size contributes to the total byte allocation based on its data type size.
8.4 Using Named Constants
Example: const int SIZE = 5; int tests[SIZE];
Using constants for sizes makes code easier to maintain, allowing centralized changes for array sizes.
8.5 Accessing Array Elements
Elements can be accessed using an index.
Example of Access: tests[0] = 79; (assigning a value), cout << tests[0]; (outputting a value).
Array elements must be accessed individually to avoid printing memory addresses.
8.6 Iterating Through Arrays
For Loop Iteration: Example for initializing an array:
const int ARRAY_SIZE = 5; int numbers[ARRAY_SIZE]; for (int i = 0; i < ARRAY_SIZE; i++) { numbers[i] = 99; }Bounds Checking: C++ does not perform bounds checking for arrays, which can lead to dangerous memory operations if indices go out of bounds.
8.7 Common Algorithms on Arrays
Find Maximum Value
Initialize maxValue to the first element and traverse the array to compare.
Find Minimum Value
Similar process as max value but looking for the least value.
Sum of All Elements
Accumulate the total by iterating through the elements.
8.8 Two-Dimensional Arrays
Definition: 2D arrays can store multiple sets of related data in a grid-like format, defined with two size declarators.
Accessing Elements: Requires two indexes for row and column.
8.9 Passing Arrays to Functions
Arrays are passed by reference; function prototypes indicate array types using empty brackets.
Example Function Declaration:
void printArr(short[], int);
8.10 Character Arrays (C-Strings)
C-strings store characters using a null terminator (
\0). They are distinct from C++ string classes.Example of C-String:
char movieTitle[20] = "Rogue One";Input Strings: Use cin for input, but getline() is preferred for capturing spaces.
8.11 Vectors
Definition: Vectors are dynamic arrays provided by the STL, capable of changing size during runtime.
Creating Vectors: Include #include <vector> and define using vector <datatype>.
Initializing Vectors: Automatically initialized to zeros; can define default values.
8.12 Data Parallelism
Concept: Involves parallel execution of algorithms across multiple processors, dividing datasets among them for simultaneous processing.
Applications: Particularly effective for algorithms that work on independent data elements, such as basic arithmetic operations on arrays.
Example Comparison: Sequential operations take longer than parallel operations which can dramatically reduce computation time by executing simultaneously.
8.13 Conclusion
Understanding arrays, vectors, and their applications is crucial in programming, offering efficient data manipulation and exploring data parallelism for enhanced performance.