Chapter 5. Arrays and Strings
Chapter 5. Arrays and Strings
September 2015
Paris Saclay
ARRAYs
STRING
Two-Dimensional Arrays
Initialization
You do it
Q&A
Paris Saclay
What is array?
Paris Saclay
What is array?
Paris Saclay
What is array?
Paris Saclay
One-Dimensional Arrays
where
type: base type of the array, determines the data type of each
element in the array
variable name: the name of the array
size: how many elements the array will hold
Paris Saclay
Example
Declaration without
initialization
int sample[10];
float float_numbers[100];
char last_name[40];
Paris Saclay
Example
score[1] = 9;
for(int i = 0; i < 7; i++)
cout << score[i] << endl;
Example
score[1] = 9;
for(int i = 0; i < 7; i++)
cout << score[i] << endl;
Example
score[1] = 9;
for(int i = 0; i < 7; i++)
cout << score[i] << endl;
10
No Array-to-Array Assignments
You cannot assign one array to another in C++.
Instead, you have to do the
Example
assignments for each element
int a[10], b[10];
// do something
// assign all elements
// of array b to array a
a = b;
int i;
// assign all elements
// of array b to array a
for(i=0; i<10; i++)
a[i] = b[i];
11
No Array-to-Array Assignments
You cannot assign one array to another in C++.
Instead, you have to do the
Example
assignments for each element
int a[10], b[10];
// do something
// assign all elements
// of array b to array a
a = b;
int i;
// assign all elements
// of array b to array a
for(i=0; i<10; i++)
a[i] = b[i];
12
No Bounds Checking
Example
int crash[10], i;
for(i = 0; i < 11; i++)
crash[i] = i;
Paris Saclay
13
What is string?
Paris Saclay
14
Declaration
Specifying the size as 10 makes room for the null at the end
of the string.
Paris Saclay
15
Declaration
Specifying the size as 10 makes room for the null at the end
of the string.
Paris Saclay
16
Declaration
Specifying the size as 10 makes room for the null at the end
of the string.
Paris Saclay
17
Make an array, that will receive the string, the target of a cin
stream.
Paris Saclay
18
Paris Saclay
19
Paris Saclay
20
Paris Saclay
21
Paris Saclay
22
Paris Saclay
23
Example:
strcpy()
and
Example 1: strcpy()
char a[10];
strcpy(a, "hello");
strlen()
Example 2: strlen()
Paris Saclay
24
Example:
strcat()
Example 3: strcpy()
char s1[21], s2[11];
strcpy(s1, "hello");
strcpy(s2, " there");
strcat(s1, s2);
Paris Saclay
25
About
strcmp()
str1 == str2: 0
a < aa < aaa < . . . < b < ba < bb < . . . < bz < baa < . . . < abca < abd
Paris Saclay
26
A two-dimensional array is
a list of one-dimensional
arrays.
To declare a
two-dimensional integer
array two dim of size 10,20
we would write:
int matrix[3][4];
Paris Saclay
27
Paris Saclay
28
Paris Saclay
29
Paris Saclay
30
Paris Saclay
31
Paris Saclay
32
Multidimensional Arrays
[size N];
Paris Saclay
33
Multidimensional Arrays
[size N];
Paris Saclay
34
Multidimensional Arrays
[size N];
Paris Saclay
35
Note: Remember that one has to make sure to make the array
long enough to include the null terminator.
Paris Saclay
36
Note: Remember that one has to make sure to make the array
long enough to include the null terminator.
Paris Saclay
37
Note: Remember that one has to make sure to make the array
long enough to include the null terminator.
Paris Saclay
38
Note: Remember that one has to make sure to make the array
long enough to include the null terminator.
Paris Saclay
39
Note: Remember that one has to make sure to make the array
long enough to include the null terminator.
Paris Saclay
40
Paris Saclay
41
Paris Saclay
42
Paris Saclay
43
Paris Saclay
44
Paris Saclay
45
Paris Saclay
46
Arrays of Strings
Paris Saclay
47
Paris Saclay
48
Paris Saclay
49