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
1247350481.050.png 1247350481.060.png 1247350481.071.png 1247350481.074.png 1247350481.001.png 1247350481.002.png 1247350481.003.png 1247350481.004.png 1247350481.005.png 1247350481.006.png 1247350481.007.png 1247350481.008.png 1247350481.009.png 1247350481.010.png 1247350481.011.png 1247350481.012.png 1247350481.013.png 1247350481.014.png 1247350481.015.png 1247350481.016.png 1247350481.017.png 1247350481.018.png 1247350481.019.png 1247350481.020.png 1247350481.021.png 1247350481.022.png 1247350481.023.png 1247350481.024.png 1247350481.025.png 1247350481.026.png 1247350481.027.png 1247350481.028.png 1247350481.029.png
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 .
1247350481.030.png 1247350481.031.png 1247350481.032.png 1247350481.033.png 1247350481.034.png 1247350481.035.png 1247350481.036.png
 
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).
1247350481.037.png 1247350481.038.png 1247350481.039.png 1247350481.040.png 1247350481.041.png 1247350481.042.png 1247350481.043.png 1247350481.044.png 1247350481.045.png 1247350481.046.png 1247350481.047.png 1247350481.048.png 1247350481.049.png 1247350481.051.png
 
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
1247350481.052.png 1247350481.053.png 1247350481.054.png 1247350481.055.png 1247350481.056.png 1247350481.057.png 1247350481.058.png 1247350481.059.png 1247350481.061.png 1247350481.062.png 1247350481.063.png 1247350481.064.png 1247350481.065.png 1247350481.066.png 1247350481.067.png 1247350481.068.png 1247350481.069.png 1247350481.070.png 1247350481.072.png 1247350481.073.png
Zgłoś jeśli naruszono regulamin