The document discusses arrays in C++, including parallel arrays, two-dimensional arrays, and multidimensional arrays. It describes how to declare, initialize, access components of, and process these different array types. Some key points covered include using parallel arrays to store related data, declaring and initializing two-dimensional arrays, accessing array components using indices, and common operations for processing two-dimensional arrays like initialization, input, output, summing rows and columns. The document also discusses passing multi-dimensional arrays to functions and using enumeration types for array indices.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
90 views37 pages
Powerpoint 4
The document discusses arrays in C++, including parallel arrays, two-dimensional arrays, and multidimensional arrays. It describes how to declare, initialize, access components of, and process these different array types. Some key points covered include using parallel arrays to store related data, declaring and initializing two-dimensional arrays, accessing array components using indices, and common operations for processing two-dimensional arrays like initialization, input, output, summing rows and columns. The document also discusses passing multi-dimensional arrays to functions and using enumeration types for array indices.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37
Chapter 8
Arrays and Strings
Parallel Arrays • Two (or more) arrays are called parallel if their corresponding components hold related information • Example: int studentId[50]; char courseGrade[50];
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 2
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 3 Two- and Multidimensional Arrays • Two-dimensional array:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 4
Two- and Multidimensional Arrays • Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two dimensions – Sometimes called matrices or tables • Declaration syntax:
– intExp1 and intExp2 are expressions with
positive integer values specifying the number of rows and columns in the array C++ Programming: From Problem Analysis to Program Design, Seventh Edition 5 C++ Programming: From Problem Analysis to Program Design, Seventh Edition 6 Accessing Array Components • Accessing components in a two-dimensional array:
– Where indexExp1 and indexExp2 are
expressions with positive integer values, and specify the row and column position • Example: sales[5][3] = 25.75;
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 7
Accessing Array Components (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 8
Two-Dimensional Array Initialization During Declaration • Two-dimensional arrays can be initialized when they are declared: – Elements of each row are enclosed within braces and separated by commas – All rows are enclosed within braces – For number arrays, unspecified elements are set to 0
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 9
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 10 Two-Dimensional Arrays and Enumeration Types • Enumeration types can be used for array indices:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 11
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 12 Processing Two-Dimensional Arrays • Ways to process a two-dimensional array: – Process entire array – Row processing: process a single row at a time – Column processing: process a single column at a time • Each row and each column of a two- dimensional array is a one-dimensional array – To process, use algorithms similar to processing one-dimensional arrays C++ Programming: From Problem Analysis to Program Design, Seventh Edition 13 C++ Programming: From Problem Analysis to Program Design, Seventh Edition 14 Initialization • Examples:
– To initialize the entire matrix to 0:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 15
Print • Use a nested loop to output the components of a two dimensional array:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 16
Input • Examples: – To input into row number 4 (fifth row):
– To input data into each component of matrix:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 17
Sum by Row • Example:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 18
Sum by Column • Example: – To find the sum of each individual column:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 19
Largest Element in Each Row and Each Column • Example: – To find the largest element in each row:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 20
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 21 Passing Two-Dimensional Arrays as Parameters to Functions • Two-dimensional arrays are passed by reference as parameters to a function – Base address is passed to formal parameter • Two-dimensional arrays are stored in row order • When declaring a two-dimensional array as a formal parameter, can omit size of first dimension, but not the second
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 22
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 23 C++ Programming: From Problem Analysis to Program Design, Seventh Edition 24 C++ Programming: From Problem Analysis to Program Design, Seventh Edition 25 C++ Programming: From Problem Analysis to Program Design, Seventh Edition 26 C++ Programming: From Problem Analysis to Program Design, Seventh Edition 27 Inclass 1/24
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 28
Arrays of Strings • Strings in C++ can be manipulated using either the data type string or character arrays (C- strings) • On some compilers, the data type string may not be available in Standard C++ (i.e., non-ANSI/ISO Standard C++)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 29
Arrays of Strings and the string Type • To declare an array of 100 components of type string: string list[100]; • Basic operations, such as assignment, comparison, and input/output, can be performed on values of the string type • The data in list can be processed just like any one-dimensional array
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 30
Arrays of Strings and C-Strings (Character Arrays)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 31
Another Way to Declare a Two-Dimensional Array • Can use typedef to define a two-dimensional array data type:
• To declare an array of 20 rows and 10
columns:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 32
Multidimensional Arrays • n-dimensional array: collection of a fixed number of elements arranged in n dimensions (n >= 1) • Declaration syntax:
• To access a component:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 33
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 34 Project – chapter 8 Due date: 1/31 before 11:59pm
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 35
Input date (see notes below)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 36
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 37