Programming C++: Chapter 5: Arrays and Strings
Programming C++: Chapter 5: Arrays and Strings
BWB 10203
Objectives
In this chapter, you will:
Learn about arrays
Explore how to declare and manipulate data into
arrays
Understand the meaning of array index out of
bounds
Become familiar with the restrictions on array
processing
Discover how to pass an array as a parameter to
a function
2
Objectives (continued)
Learn about C-strings
Examine the use of string functions to
process C-strings
Discover how to input data intoand output
data froma C-string
Learn about parallel arrays
Discover how to manipulate data in a twodimensional array
Learn about multidimensional arrays
3
Data Types
A data type is called simple if variables of that
type can store only one value at a time
A structured data type is one in which each
data item is a collection of other data items
Arrays
Array: a collection of a fixed number of
components wherein all of the components
have the same data type
In a one-dimensional array, the components
are arranged in a list form
Syntax for declaring a one-dimensional array:
Arrays (continued)
Example:
int num[5];
10
11
//Line 1
//Line 2
Example:
for (i = 0; i < 100; i++) //Line 1
cin >> list[i];
//Line 2
13
Example:
double sales[] = {12.25, 32.50, 16.90, 23, 45.68};
16
The statement:
int list[25]= {4, 7};
18
19
20
21
22
23
25
26
27
28
29
31
String Comparison
C-strings are compared character by
character using the collating sequence of the
system
If we are using the ASCII character set
"Air" < "Boat"
"Air" < "An"
"Bill" < "Billy"
"Hello" < "hello"
32
String Input
cin >> name; stores the next input Cstring into name
To read strings with blanks, use get:
cin.get(str, m+1);
String Output
cout << name; outputs the content of
name on the screen
<< continues to write the contents of name
until it finds the null character
If name does not contain the null character,
then we will see strange output
<< continues to output data from memory
adjacent to name until '\0' is found
36
37
38
Parallel Arrays
Two (or more) arrays are called parallel if
their corresponding components hold related
information
Example:
int studentId[50];
char courseGrade[50];
39
Two-Dimensional 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:
where intexp1 and intexp2 are expressions
yielding positive integer values, and specify the
number of rows and the number of columns,
respectively, in the array
40
Two-Dimensional Arrays
(continued)
41
42
43
Two-Dimensional Array
Initialization During Declaration
Two-dimensional arrays can be initialized
when they are declared:
45
Processing Two-Dimensional
Arrays
Ways to process a two-dimensional array:
Process the entire array
Process a particular row of the array, called
row processing
Process a particular column of the array, called
column processing
Processing Two-Dimensional
Arrays (continued)
47
Initialization
To initialize row number 4 (i.e., fifth row) to 0
48
Print
To output the components of matrix:
49
Input
To input data into each component of
matrix:
50
Sum by Row
To find the sum of row number 4 of matrix:
51
Sum by Column
To find the sum of each individual column:
52
53
Reversing Diagonal
Before:
54
55
56
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++)
58
59
60
61
Multidimensional Arrays
Multidimensional array: collection of a fixed
number of elements (called components)
arranged in n dimensions (n >= 1)
Also called an n-dimensional array
Declaration syntax:
To access a component:
62
64
65
Message transmitted OK
68
71
Programming Example:
readCode
First, read length of secret code
If length of secret code is greater than 250
Set lenCodeOk (a reference parameter) to
false and the function terminates
Programming Example:
readCode (continued)
73
Programming Example:
compareCode
Set a bool variable codeOk to true
If length of code and copy are not equal
Output error message and terminate function
Programming Example:
compareCode (continued)
75
Programming Example:
compareCode (continued)
76
Declare variables
Open the files
Call readCode to read the secret code
If (length of the secret code <= 250)
Call compareCode to compare the codes
else
Output an appropriate error message
77
Summary
Array: structured data type with a fixed
number of components of the same type
Components are accessed using their relative
positions in the array
Summary (continued)
The base address of an array is the address
of the first array component
When passing an array as an actual
parameter, you use only its name
Passed by reference only
Summary (continued)
Commonly used C-string manipulation
functions include:
strcpy, strcmp, and strlen
80
Summary
To access an element of a two-dimensional
array, you need a pair of indices:
One for the row position
One for the column position
81