Академический Документы
Профессиональный Документы
Культура Документы
Department
Computer Programming
(MEng 1052)
Lecture 5
April, 2015
Part I: Arrays
Introduction
An array is used to process a collection of data all of which is
declared as follows:
int score[5];
This declaration is like declaring the following five variables
to all be of type int:
score[0], score[1], score[2], score[3], score[4]
The individual variables that together make up the array are
called indexed variables. They are also sometimes called
subscripted variables or elements of the array.
The number in square brackets is called an index or a
subscript.
In C++, indexes are numbered starting with 0, not starting
with 1 or any other number except 0.
Initializing Arrays
An array can be initialized when it is declared. When initializing
the array, the values for the various indexed variables are
enclosed in braces and separated with commas. For example,
int children[3] = {2, 12, 1};
This declaration is equivalent to the following code:
int children[3];
children[0] = 2;
children[1] = 12;
children[2] = 1;
If you list fewer values than there are indexed variables, those
values will be used to initialize the first few indexed variables,
and the remaining indexed variables will be initialized to a zero
of the array base type.
9
Initializing Arrays
The number of elements in the array that we initialized within
11
12
13
14
Array Examples
15
16
Program Output
Month
Month
Month
Month
Month
Month
Month
Month
Month
Month
Month
Month
17
1 has 31 days.
2 has 28 days.
3 has 31 days.
4 has 30 days.
5 has 31 days.
6 has 30 days.
7 has 31 days.
8 has 31 days.
9 has 30 days.
10 has 31 days.
11 has 30 days.
12 has 31 days.
18
19
20
21
22
23
24
25
dimensional array.
A twodimensional array is, in essence, a list of onedimensional
arrays.
The general form of a twodimensional array declaration is
type name[size1][size2];
Pay careful attention to the declaration. Unlike some other
computer languages, which use commas to separate the array
dimensions, C++ places each dimension in its own set of
brackets. Similarly, to access an element, specify own set of
brackets
Twodimensional arrays are stored in a rowcolumn matrix,
where the first index indicates the row and the second indicates
the column
26
the first index indicates the row and the second indicates
the column
27
28
29
Strings
The most common use for one-dimensional arrays is to store
strings of characters.
In C++, a string is defined as a character array terminated by
a null symbol (\0).
char str[11];
Specifying the size as 11 makes room for the null at the end
of the string.
30
Strings
31
Declaration of Strings
Similar to declaration of any array
char name[30];
// no initialization
char title [20] = "Le Grande Fromage";
// initialized at declaration
// with a string
char chList [10] = {'a', 'b', 'c', 'd'};
// initialized with list of char
// values
32
stream.
The following program reads (part of) a string entered by the
user:
33
34
35
String manipulation
strlen (str) = tells the length of a string; the total number
36
37
38
is unchanged.
39
strings:
40
41
42
43
might guess.
The comparison is true if the strings do not match. The
function strcmp compares the characters in the string
arguments a character at a time
44
45
46
Arrays of Strings
An array of strings is a special form of a two-dimensional
array.
The size of the left index determines the number of strings.
The size of the right index specifies the maximum length of
each string.
For example, the following declares an array of 30 strings,
each having a maximum length of 80 characters (with one
extra character for the null terminator):
char string_array[30][81];
For accessing an individual string, one simply specifies only
Arrays of Strings
The following example calls the gets ( ) function with the
48
information.
One such application would be an employee database that
stores
the name
telephone number
hours worked per pay period, and
hourly wage.
These data we could store in arrays:
49
50
52
53
54
Exercises
1.
2.
3.
4.
5.
55
End of course
Thank You!!!
56