Chapter 7
Introduction to Programming for Engineers
Lecture 7 focuses on one-dimensional arrays.
Instructor: Dr. Abeer Badawi
Additional Lectures by: Dr. Khalid Elgazzar
Course Outline
Arrays
Statistical Measurements
Sorting and Search Algorithms
Character Strings
The String Class
The Vector Class
Learning Objectives
Develop solutions in C++ using:
One-dimensional arrays and vectors
Programmer-defined modules for statistical analysis
Functions for sorting and searching data
Probability functions
Character strings and string objects
Standard library functions (
cstring,string) and custom header files
Understanding Arrays
Definition: An array is a data structure that stores a contiguous block of data.
All elements in an array are of the same type.
Access elements using an array name and an offset (index), starting at 0.
Example Syntax
Declaration Syntax:
Example:
Array Characteristics
Single Type Holding: All elements must be of the same type.
Memory Allocation: Arrays allocate memory for a fixed number of values; size declarators must be a positive integer constant.
Example:
Initializing Arrays
Initialization Syntax combines with declaration:
Example:
Accessing Array Elements
General format to access:
Ex:
Element Modification
Example of accessing and modifying:
Bugs to Avoid
Array Bounds: C++ does not enforce bounds checking on arrays; this can lead to segmentation faults or unpredictable behavior.
Functions and Arrays
When passing arrays, the reference to the starting address (first element) is passed without size information.
Problem-Solving Techniques
Filling an Array: Example to initialize with zeros
Copying Arrays
Using a loop to copy arrays:
Example:
Finding Maximum and Minimum in Arrays
To find the largest:
Arrays in Functions
Arrays cannot be returned directly but can be passed to define behavior.
Example of calculating mean:
Sorting Algorithms
Selection Sort:
Find the smallest item from the unsorted part and swap it with the first unsorted item.
Repeat for the next unsorted elements until sorted.
Example of Selection Sort Implementation
Search Algorithms
Unordered datasets can be searched sequentially (Simple Search), and ordered datasets may use Sequential or Binary Search for efficiency.
Example of Binary Search
Character Strings and String Class
C-style Strings: Are arrays of characters ended with a null character.
String Class: Dynamically sized and has many operators and methods including:
Size and empty checks:
size(),empty().Accessing substrings:
substr(int start, int len).
Vectors
Definition: A vector can grow dynamically, unlike arrays.
Manipulation: Use
push_back()to add orpop_back()to remove elements.
Example of Vector Usage
Passing Vectors to Functions
Vectors passed by reference can be modified inside functions; otherwise, they are passed by value.
Conclusion
One-dimensional arrays and vectors are fundamental data structures in C++ used for numerous programming tasks, including sorting, searching, and statistical analysis. Understanding their mechanics is crucial for effective programming in engineering applications.
More detailed version
Introduction to Programming for Engineers
Lecture 7 focuses on one-dimensional arrays, integral components in programming that allow for efficient data manipulation and storage.
Instructor: Dr. Abeer Badawi
Additional Lectures by: Dr. Khalid Elgazzar
Course Outline
Arrays
Definition and characteristics
Syntax and declarations
Initialization, modification, and accessing elements
Functions that utilize arrays
Statistical Measurements
Mean, median, mode calculations using arrays
Sorting and Search Algorithms
Detailed methods for sorting (Selection, Bubble, Insertion)
Efficient searching techniques (Linear and Binary Search)
Character Strings
The distinction between C-style strings and the String class in C++
The String Class
Operators and methods available in the String class
Memory management of string objects
The Vector Class
Dynamic array behavior and manipulation in vectors
Memory and performance considerations
Learning Objectives
Develop solutions in C++ using:
One-dimensional arrays and vectors
Programmer-defined modules for statistical analysis
Functions for sorting and searching data
Probability functions
Character strings and string objects
Standard library functions (cstring, string) and custom header files
Understanding Arrays
Definition: An array is a data structure that stores a contiguous block of data. All elements in an array must be of the same type to ensure data consistency.
Access: Elements are accessed using an array name and an offset (index), starting from 0, allowing random access to any element in constant time.
Example Syntax
Declaration Syntax:
Example:
Array Characteristics
Single Type Holding: All elements must be of the same type, enforcing type safety.
Memory Allocation: Arrays allocate memory for a fixed number of values. The size declarators must be a positive integer constant.
Example:
Initializing Arrays
Syntax: Initialization can occur at the time of declaration, ensuring elements are assigned initial values.
Example:
Accessing Array Elements
General format to access array elements:
Example:
Element Modification
Modifying array elements can be done directly using their index:
Example:
Bugs to Avoid
Array Bounds: C++ does not enforce bounds checking on arrays, leading to segmentation faults or unpredictable behavior if out-of-bound indices are accessed.
Functions and Arrays
When passing arrays to functions, a reference to the starting address (first element) is passed, without size information. This affects how arrays are handled within functions.
Problem-Solving Techniques
Filling an Array: Initialize an array with zeros efficiently.
Example:Copying Arrays: Arrays can be copied using loops, maintaining data integrity.
Example:Finding Maximum and Minimum in Arrays: Find the largest/smallest element via iteration, ensuring efficiency.
Example:
Arrays in Functions
Arrays are not directly returned but can be passed to functions to define behavior.
Example of Calculating Mean:
Sorting Algorithms
Selection Sort: A simple yet effective sorting algorithm that repeatedly selects the smallest item from the unsorted section and swaps it.
Step-by-step process leading to a sorted array.
Example of Selection Sort Implementation:
Search Algorithms
Searching Techniques: Unordered datasets require sequential searching, while ordered datasets can utilize Sequential or Binary Search for improved performance.
Example of Binary Search:
Character Strings and String Class
C-style Strings: Defined as arrays of characters terminated with a null character. This representation is crucial for legacy C code.
String Class: A modern C++ feature with dynamic sizing, equipped with various operators and methods such as size(), empty(), and substring access via substr().
Vectors
Definition: Vectors are dynamic arrays that can grow and shrink, overcoming the fixed-size limitation of regular arrays.
Manipulation: Utilize methods like pushback() to add elements and popback() to remove elements efficiently.
Example of Vector Usage:
Passing Vectors to Functions
Vectors passed by reference can be modified inside functions, enhancing efficiency; otherwise, they are passed by value, which may incur a performance penalty.
Conclusion
One-dimensional arrays and vectors are fundamental data structures in C++ extensively utilized for sorting, searching, and statistical analysis. A deep understanding of their mechanics is essential for effective programming in engineering applications. Their implementation, optimization strategies, and potential pitfalls are crucial for coding proficiently in C++, paving the way for more advanced programming concepts.