C - Programming - C - Program - Chapter 09.pdf
(
983 KB
)
Pobierz
CHAPTER
9
A
RRAYS AND
S
TRINGS
IN THIS CHAPTER, YOU WILL:
n
Learn about arrays
n
Explore how to declare and manipulate data into arrays
n
Understand the meaning of ‘‘array index out of bounds’’
n
Become familiar with the restrictions on array processing
n
Discover how to pass an array as a parameter to a function
n
Learn about
C
-strings
n
Examine the use of string functions to process
C
-strings
n
Discover how to input data into—and output data from—a
C
-string
n
Learn about parallel arrays
n
Discover how to manipulate data in a two-dimensional array
n
Learn about multidimensional arrays
470
|
Chapter 9: Arrays and Strings
In previous chapters, you worked with simple data types. In Chapter 2, you learned that
C++ data types fall into three categories. One of these categories is the structured data
type. This chapter and the next few chapters focus on structured data types.
Recall that a data type is called simple if variables of that type can store only one value at
a time. In contrast, in a structured data type, each data item is a collection of other data
items. Simple data types are building blocks of structured data types. The first structured
data type that we will discuss is an array. In Chapters 11 and 12, we will discuss other
structured data types.
Before formally defining an array, let us consider the following problem. We want to
write a C++ program that reads five numbers, finds their sum, and prints the numbers in
reverse order.
In Chapter 5, you learned how to read numbers, print them, and find the sum. The
difference here is that we want to print the numbers in reverse order. This means we
cannot print the first four numbers until we have printed the fifth, and so on. To do this,
we need to store all the numbers before we start printing them in reverse order. From
what we have learned so far, the following program accomplishes this task.
//Program to read five numbers, find their sum, and print the
//numbers in reverse order.
#include
<iostream>
using namespace
std;
int
main()
{
int
item0, item1, item2, item3, item4;
int
sum;
cout << "Enter five integers: ";
cin >> item0 >> item1 >> item2 >> item3 >> item4;
cout << endl;
sum = item0 + item1 + item2 + item3 + item4;
cout << "The sum of the numbers = " << sum << endl;
cout << "The numbers in the reverse order are: ";
cout << item4 << " " << item3 << " " << item2 << " "
<< item1 << " " << item0 << endl;
return
0;
}
This program works fine. However, if you need to read 100 (or more) numbers and
print them in reverse order, you would have to declare 100 variables and write many
cin
and
cout
statements. Thus, for large amounts of data, this type of program is not
desirable.
Arrays
|
471
Note the following in the previous program:
1. Five variables must be declared because the numbers are to be printed in
reverse order.
2. All variables are of type
int
—that is, of the same data type.
3. The way in which these variables are declared indicates that the variables
to store these numbers all have the same name—except the last char-
acter, which is a number.
Statement 1 tells you that you have to declare five variables. Statement 3 tells you
that it would be convenient if you could somehow put the last character, which is a
number, into a counter variable and use one
for
loop to count from
0
to
4
for
reading and another
for
loop to count from
4
to
0
for printing. Finally, because all
variablesareofthesametype,youshouldbeabletospecifyhowmanyvariables
must be declared—and their data type—with a simpler statement than the one we
used earlier.
The data structure that lets you do all of these things in C++ is called an array.
Arrays
An array is a collection of a fixed number of components all of the same data type. A
one-dimensional array is an array in which the components are arranged in a list form.
This section discusses only one-dimensional arrays. Arrays of two dimensions or more are
discussed later in this chapter.
The general form for declaring a one-dimensional array is:
9
dataType arrayName[intExp];
where
intExp
is any constant expression that evaluates to a positive integer. Also,
intExp
specifies the number of components in the array.
EXAMPLE 9-1
The statement:
int
num[5];
declares an array num of five components. Each component is of type
int
.Thecompo-
nents are
num[0]
,
num[1]
,
num[2]
,
num[3]
,and
num[4]
. Figure 9-1 illustrates the
array
num
.
472
|
Chapter 9: Arrays and Strings
num[0]
num[1]
num[2]
num[3]
num[4]
Array
num
FIGURE 9-1
Accessing Array Components
The general form (syntax) used for accessing an array component is:
arrayName[indexExp]
where
indexExp
, called the index, is any expression whose value is a non negative
integer. The index value specifies the position of the component in the array.
In C++,
[]
is an operator, called the array subscripting operator. Moreover, in C++,
the array index starts at
0
.
Consider the following statement:
int
list[10];
This statement declares an array
list
of
10
components. The components are
list[0], list[1], ..., list[9]
. In other words, we have declared 10 variables
(see Figure 9-2).
Arrays
|
473
list[0]
list[1]
list[2]
list[3]
list[4]
list[5]
list[6]
list[7]
list[8]
list[9]
Array
list
FIGURE 9-2
The assignment statement:
list[5] = 34;
stores
34
in
list[5]
, which is the sixth component of the array
list
(see Figure 9-3).
9
list[0]
list[1]
list[2]
list[3]
list[4]
list[5]
34
list[6]
list[7]
list[8]
list[9]
Array
list
after execution of the statement
list[5]= 34;
FIGURE 9-3
Plik z chomika:
Januszek66
Inne pliki z tego folderu:
Back Seat_ A Mumbai Tale - Aditya Kripalani.mobi
(755 KB)
Brief Wondrous Life of Oscar Wao, The - Junot Diaz.opf
(3 KB)
Don't Make Me Think, Revisited_ - Steve Krug.mobi
(9256 KB)
M. T. Anderson - Norumbegan 03 - The Empire of Gut and Bone # (v5.0).epub
(2209 KB)
M. T. Anderson - Norumbegan 02 - The Suburb Beyond the Stars # (v5.0).epub
(2105 KB)
Inne foldery tego chomika:
Dokumenty
Galeria
LUDLUM ROBERT
Midi - Kar
Mszał Rzymski PL
Zgłoś jeśli
naruszono regulamin