Chapter 8 Programming I
Chapter 8 Programming I
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain
1
product or service or otherwise on a password-protected website for classroom
Objectives (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 2
product or service or otherwise on a password-protected website for classroom use.
Objectives (2 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 3
product or service or otherwise on a password-protected website for classroom use.
Objectives (3 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 4
product or service or otherwise on a password-protected website for classroom use.
Introduction
• Simple data type: variables of these types can store only one value at a time
• Structured data type: a data type in which each data item is a collection of
other data items
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 5
product or service or otherwise on a password-protected website for classroom use.
Arrays
• Array: a collection of a fixed number of components, all of the same data type
• One-dimensional array: components are arranged in a list form
• Syntax for declaring a one-dimensional array
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 6
product or service or otherwise on a password-protected website for classroom use.
Accessing Array Components (1 of 3)
• General syntax
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 7
product or service or otherwise on a password-protected website for classroom use.
Accessing Array Components (2 of 3)
list[5] = 34;
stores 34 in list[5], the sixth component of the array list
FIGURE 8-4 Array list after execution of the statement list[5]= 34;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 8
product or service or otherwise on a password-protected website for classroom use.
Accessing Array Components (3 of 3)
list[3] = 10;
list[6] = 35;
list[5] = list[3] + list[6];
FIGURE 8-5 Array list after execution of the statements list[3]= 10;, list[6]=
35;, and list[5] = list[3] + list[6];
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 9
product or service or otherwise on a password-protected website for classroom use.
Processing One-Dimensional Arrays (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 10
product or service or otherwise on a password-protected website for classroom use.
Processing One-Dimensional Arrays (2 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 11
product or service or otherwise on a password-protected website for classroom use.
Processing One-Dimensional Arrays (3 of 3)
• Refer to Example 8-3 in the text, which shows how loops are used to process
arrays
• Initializing an array
• Reading data into an array
• Printing an array
• Finding the sum and average of an array
• Finding the largest element in an array
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 12
product or service or otherwise on a password-protected website for classroom use.
Array Index Out of Bounds
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 13
product or service or otherwise on a password-protected website for classroom use.
Array Initialization During Declaration
• Example 2: the array size is determined by the number of initial values in the
braces if the array is declared without size specified
double sales[] = {12.25, 32.50, 16.90, 23, 45.68}
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 14
product or service or otherwise on a password-protected website for classroom use.
Partial Initialization of Arrays During Declaration
• The statement:
int list[10] = {0};
– Declares an array of 10 components and initializes all of them to zero
• The statement (an example of partial initialization of an array during
declaration):
int list[10] = {8, 5, 12};
– Declares an array of 10 components and initializes list[0] to 8, list[1] to 5,
list[2] to 12
– All other components are initialized to 0
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 15
product or service or otherwise on a password-protected website for classroom use.
Some Restrictions on Array Processing
• Solution
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 16
product or service or otherwise on a password-protected website for classroom use.
Arrays as Parameters to Functions
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 17
product or service or otherwise on a password-protected website for classroom use.
Constant Arrays as Formal Parameters
• Can prevent a function from changing the actual parameter when passed by
reference
• Use const in the declaration of the formal parameter
• Example
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 18
product or service or otherwise on a password-protected website for classroom use.
Base Address of an Array and Array in Computer Memory
• The base address of an array is the address (memory location) of the first array
component
• If list is a one-dimensional array, its base address is the address of list[0]
• When an array is passed as a parameter, the base address of the actual array is
passed to the formal parameter
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 19
product or service or otherwise on a password-protected website for classroom use.
Functions Cannot Return a Value of the Type Array
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 20
product or service or otherwise on a password-protected website for classroom use.
Integral Data Type and Array Indices
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 21
product or service or otherwise on a password-protected website for classroom use.
Other Ways to Declare Arrays
• Example 1
• Example 2
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 22
product or service or otherwise on a password-protected website for classroom use.
Searching an Array for a Specific Item
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 23
product or service or otherwise on a password-protected website for classroom use.
Sorting
• Selection sort: rearrange the list by selecting an element and moving it to its
proper position
• Steps for a selection sort:
• Find the smallest element in the unsorted portion of the list
• Move it to the top of the unsorted portion by swapping with the element currently
there
• Start again with the rest of the list
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 24
product or service or otherwise on a password-protected website for classroom use.
Selection Sort
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 25
product or service or otherwise on a password-protected website for classroom use.
Auto Declaration and Range-Based for Loops
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 26
product or service or otherwise on a password-protected website for classroom use.
C-Strings (Character Arrays) (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 27
product or service or otherwise on a password-protected website for classroom use.
C-Strings (Character Arrays) (2 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 28
product or service or otherwise on a password-protected website for classroom use.
C-Strings (Character Arrays) (3 of 3)
• The size of an array can be omitted if the array is initialized during declaration
– Declares an array of length 5 and stores the C-string "John" in the array
• Useful string manipulation functions include:
– strcpy
– strncpy
– strcmp
– strlen
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 29
product or service or otherwise on a password-protected website for classroom use.
String Comparison
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 30
product or service or otherwise on a password-protected website for classroom use.
Reading and Writing Strings
• Most rules for arrays also apply to C-strings (which are character arrays)
• Aggregate operations, such as assignment and comparison, are not allowed on
arrays
• C++ does allow aggregate operations for the input and output of C-strings
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 31
product or service or otherwise on a password-protected website for classroom use.
String Input
• When executed , the statement stores the next m characters into str, but the newline
character is not stored in str
• If input string has fewer than m characters, reading stops at the newline character
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 32
product or service or otherwise on a password-protected website for classroom use.
String Output
• Example
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 33
product or service or otherwise on a password-protected website for classroom use.
Specifying Input/Output Files at Execution Time
• User can specify the name of an input and/or output file at execution time
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 34
product or service or otherwise on a password-protected website for classroom use.
string Type and Input/Output Files
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 35
product or service or otherwise on a password-protected website for classroom use.
Parallel Arrays
• Two (or more) arrays are called parallel if their corresponding components hold related
information
• The following example illustrates two parallel arrays:
int studentId[50];
char courseGrade[50];
With the following sample data to enter into the arrays:
studentId courseGrade
23456 A
86723 B
22356 C
92733 B
11892 D
.
.
.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 36
product or service or otherwise on a password-protected website for classroom use.
Two- and Multidimensional Arrays
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 37
product or service or otherwise on a password-protected website for classroom use.
Accessing Array Components (1 of 2)
• Where indexExp1 and indexExp2 are expressions with positive integer values,
and specify the row and column position
• Example: sales[5][3] = 25.75;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 38
product or service or otherwise on a password-protected website for classroom use.
Accessing Array Components (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 39
product or service or otherwise on a password-protected website for classroom use.
Two-Dimensional Array Initialization During Declaration
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 40
product or service or otherwise on a password-protected website for classroom use.
Two-Dimensional Arrays and Enumeration Types
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 41
product or service or otherwise on a password-protected website for classroom use.
Processing Two-Dimensional Arrays
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 42
product or service or otherwise on a password-protected website for classroom use.
Initialization
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 43
product or service or otherwise on a password-protected website for classroom use.
Print
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 44
product or service or otherwise on a password-protected website for classroom use.
Input
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 45
product or service or otherwise on a password-protected website for classroom use.
Sum by Row
• The following example shows how to find the sum of row number 4:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 46
product or service or otherwise on a password-protected website for classroom use.
Sum by Column
• The following example illustrates finding the sum of each individual column:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 47
product or service or otherwise on a password-protected website for classroom use.
Largest Element in Each Row and Each Column
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 48
product or service or otherwise on a password-protected website for classroom use.
Passing Two-Dimensional Arrays as Parameters to Functions
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 49
product or service or otherwise on a password-protected website for classroom use.
Arrays of Strings
• Strings in C++ can be manipulated using either the data type string or
character arrays (C-strings)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 50
product or service or otherwise on a password-protected website for classroom use.
Arrays of Strings and the string Type
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 51
product or service or otherwise on a password-protected website for classroom use.
Arrays of Strings and C-Strings (Character Arrays)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 52
product or service or otherwise on a password-protected website for classroom use.
Another Way to Declare a Two-Dimensional Array
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 53
product or service or otherwise on a password-protected website for classroom use.
Multidimensional Arrays
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 54
product or service or otherwise on a password-protected website for classroom use.
Quick Review (1 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 55
product or service or otherwise on a password-protected website for classroom use.
Quick Review (2 of 4)
• The base address of an array is the address of the first array component
• When passing an array as an actual parameter, use only its name
• Passed by reference only
• A function cannot return an array type value
• Individual array components can be passed as parameters to functions
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 56
product or service or otherwise on a password-protected website for classroom use.
Quick Review (3 of 4)
• In C++, C-strings are null terminated and are stored in character arrays
• Commonly used C-string manipulation functions include: strcpy, strncpy,
strcmp, strncmp, and strlen
• Parallel arrays hold related information
• In a two-dimensional array, the elements are arranged in a table form
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 57
product or service or otherwise on a password-protected website for classroom use.
Quick Review (4 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 58
product or service or otherwise on a password-protected website for classroom use.