Chapter07 Array - String - PPT
Chapter07 Array - String - PPT
Chapter 7
Array & String
What is an array?
An array stores more than one value of the
same data type under a collective name.
2
Chapter 7 Array and String
Why arrays?
char initial; // A character variable
int number; // An integer variable
double value; // A floating point variable
These variables can each be used to store one value only.
Why arrays?
A list of variables of the same type
int num1, num2, num3, …, num10;
can be replaced by a single array
int num[10];
Accessing individual data in array is easy
and fast by using index and loop.
4
Chapter 7 Array and String
Array Declaration
The syntax for declaring an array is:
data_type array_name[size];
e.g.
int marks[10];
data type size
char city[15];
0 1 2 3 4 5 6 7 8 9
marks
int: 4 bytes
40 bytes
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
city
char: 1 byte
15 bytes
6
Chapter 7 Array and String
Initializing an Array
#include <iostream>
using namespace std;
int main()
Declaration Initialization
{
int num[4] = {2, 4, 6, 8};
char letter[4] = {'A', 'B', 'C', 'D' };
:
:
}
num 2 4 6 8
8
Chapter 7 Array and String
10
10
Chapter 7 Array and String
11
11
12
12
Chapter 7 Array and String
Quiz 7.1.1
An array is used to hold several data items
together as one entity. Can data of
different types be stored in a single array
(Yes / No)?
13
13
Quiz 7.1.2
Which of the following statement(s) about the character '5'
and the string "5" in the C++ language is (are) true?
(a) The character '5' can be assigned or stored in a character variable.
(b) The string "5" can be assigned or stored in a character variable.
(c) The string "5" consists of 2 characters, '5' and '\0'.
(d) The string "5" can be assigned in a character array of size 2.
14
14
Chapter 7 Array and String
int main()
{
int num[5] = {9, 0, −5, 7, 8};
double change[4] = {−3.5, 0, −2, 1.05};
char key[4] = {'W', '@', 'a', '1'};
char date[8] = "1st Jan";
:
:
}
15
15
16
Chapter 7 Array and String
−3.5 0 −2 1.05 ? ?
17
17
int main()
{
int num[ ] = {9, 0, −5, 7, 8};
double change[ ] = {−3.5, 0, −2, 1.05};
char key[ ] = {'W', '@', 'a', '1'};
char date[ ] = "1st Jan";
:
:
}
18
18
Chapter 7 Array and String
19
19
20
20
Chapter 7 Array and String
21
21
index or
subscript
22
22
Chapter 7 Array and String
23
23
Quiz 7.2.1
Given that
int num[5] = {9, 0, −5, 7, 7}; // 1 1) num
char month[9]; // 2 2) month
char year[ ]; // 3 3) year
double price[5]; // 4 4) price
char key[5] = {'W', '@', 'a', '1' }; // 5 5) key
char Christmas[ ] = "25 Dec"; // 6 6) Christmas
double change[6] = {−3.5, 0, −2, 1.05}; // 7 7) change
24
Chapter 7 Array and String
Quiz 7.2.2
Given that 1) array num
int num[5] = {9, 0, −5, 7, 7}; // 1 2) array month
char month[9]; // 2 3) array year
char year[5] = {'1', '9', '6', '5', '\0'}; // 3 4) array price
double price[5]; // 4 5) array key
char key[5] = {'W', '@', 'a', '1' }; // 5 6) array Christmas
char Christmas[ ] = "25 Dec"; // 6 & 7 7) "25 Dec"
double change[6] = {−3.5, 0, −2, 1.05}; // 8 8) double change
25
Quiz 7.2.3
int main()
{
int d[ ] = {1, 2, 3, 4, 5};
char code[6] = {'W', '@', 'a', ‘7' };
code[2] = 'x';
code[0] = d[1] + code[2] ;
cout << code[0];
return 0;
}
26
Chapter 7 Array and String
Accessing String
A string is a sequence of characters ends with '\0'.
Display the entire string with a single cout statement.
int main()
{
char date[ ] = "1 Jan";
date[0] = '2';
cout << date[0] << endl; 2
2 Jan
cout << date;
:
: Note: No [ ] after the
array name, date.
}
date '2'
'1' ' ' 'J' 'a' 'n' '\0'
27
28
28
Chapter 7 Array and String
29
29
Unknown data in
arr 'H' 'e' 'l' 'l' 'o'
memories behind.
30
30
Chapter 7 Array and String
int main()
{ 5
7
char date[20] = "1 Mar";
cout << strlen(date) << endl;
cout << strlen("1 March") << endl;
:
:
}
The input parameter strlen( ) is a string.
strlen( ) returns the length of the string.
31
31
#include <iostream>
using namespace std;
int main()
{
char date[6];
date = "1 Jan"; // Wrong statement. Compilation error.
cout << date;
}
32
32
Chapter 7 Array and String
33
33
34
34
Chapter 7 Array and String
35
35
Quiz 7.3.1
Determine the output of the program.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char eqn[20] = {'1', '+', '1', '=', '2', '\0'};
(a) 52
(b) 59
(c) 62
(d) 65
36
36
Chapter 7 Array and String
Quiz 7.3.2
Given that the user enters "Bye" for the program below, determine
which statement(s) can be filled in the blank below to display the
word, Bye. You may select more than one statements.
int main()
Select statement(s):
{
(a) strcpy(str, "Bye");
char ch[30];
char str[30] = "Good night"; (b) strcpy(ch, str);
(c) strcpy(str, ch);
cout << "User entry: "; (d) strcpy(ch[ ], str[ ]);
cin >> ch ; (e) strcpy(str[30], ch[30]);
?
______________
cout << str;
return 0;
}
37
37
int main()
{
int marks[5]; //An array to store module scores
int i;
Loop is very useful for
//Enter scores by user accessing array elements.
for (i=0; i<5; i++)
{
cout << "Enter module score: ";
cin >> marks[i];
} Loop is very useful for
accessing array elements.
//Display scores
for (i=0; i<5; i++)
{
cout << "Module " << i+1 << ": " << marks[i] << endl;
}
return 0;
}
38
38
Chapter 7 Array and String
96
? 47
? 85
? 64
? 40
?
39
96 47 85 64 40
marks[0] marks[1] marks[2] marks[3] marks[4]
i+1 marks[i]
Module 1: 96 when i = 0
Module 2: 47 when i = 1
Module 3: 85 when i = 2
Module 4: 64 when i = 3
Module 5: 40 when i = 4
40
40
Chapter 7 Array and String
Quiz 7.4.1
Fill in the blank. The program segment
below computes the average mark.
sum = 0;
for (i=0; i<5; i++) ?
sum += marks[_];
average = sum/5;
41
41
Quiz 7.4.2
Fill in the blank. The program segment
below computes the number of students
who scores grade A which is 80 marks or
above.
gradeA = 0;
for (i=0; i<5; i++)
{
if (marks[i]__________)
gradeA++;
}
42
42
Chapter 7 Array and String
2-Dimensional Array
Arrays in C can have multiple indexes or subscripts.
A common use of such arrays is to represent tables of
values consisting of information arranged in rows and
columns.
To identify a particular table element, two indexes are
required: The first index identifies the element’s row and
the second index identifies the element’s column.
Referred to as double-index array, double-subscripted
array or 2-dimensional array.
43
43
alph
44
44
Chapter 7 Array and String
#include <iostream>
using namespace std;
int main()
{
char alph[2][3] = { {'A', 'B', 'C'}, {'X', 'Y', 'Z'} };
char alph[2][3] = {'A', 'B', 'C', 'X', 'Y', 'Z'};
:
:
}
Column 0 Column 1 Column 2
Row 0 'A' 'B' 'C'
Row 1 'X' 'Y' 'Z'
alph
45
45
marks
46
46
Chapter 7 Array and String
int main()
{
char alph[2][3];
alph[0][1] = 'A'
'A';
alph[1][0] = 'C'
'C';
alph[0][0] = '%'
'%';
alph[1][2] = '4'
'4';
:
:
}
Column 0 Column 1 Column 2
Row 0 ? ? ?
Row 1 ? ? ?
alph
47
47
48
Chapter 7 Array and String
49
49
50
50
Chapter 7 Array and String
51
52
Chapter 7 Array and String
53
53
54
Chapter 7 Array and String
55
int main()
{ i i<5 Proc Cal j j<3 Proc Write to Cal
// Getting the marks inner i++ cout marks[i][j] j++
j loop & cin
⋮
// Display the marks 4 T Yes j=0
cout << "\n\t\t\tSubject 1\tSubject 2\tSubject 3"<< endl; 4 0 T Yes 17
for (i=0; i < 5; i++) 1
{
cout << "Marks for student " << i+1; 4 1 T Yes 55
cout << "\t"; 2
for (j=0; j<3; j++)
{ 4 2 T Yes 90
cout << " " << marks[i][j] << "\t\t"; 3
}
cout << "\n"; 4 3 F No
} 5
return 0;
} 5 F No
56
56
Chapter 7 Array and String
Quiz 7.5.1
Determine the output of the program.
#include <iostream>
using namespace std;
int main()
{
int table[4][3] = {9, 0, 6, 3, 1, 8, 5, 3, 9, 2, 4, 7};
table[2][0] = table[0][0];
cout << table[2][0] - table[3][1] << endl;
return 0;
}
(a) 1
(b) 5
(c) 6
(d) 7
57
57
Quiz 7.5.2
Fill in the missing statement.
#include <iostream> cout<<"\nMatrix B =\n";
using namespace std; for (i=0; i<4; i++)
{
int main() for (j=0; j<2; j++)
{ cout<<matrix_B[i][j]<<"\t";
int matrix_A[2][4] = {11, 13, 17,
0, -19, -17, -13, -11}; cout<<endl;
}
int matrix_B[4][2], i, j; return 0;
}
cout<<"Matrix A =\n"; Output Window of the Program
for (i=0; i<2; i++)
{ Matrix A =
for (j=0; j<4; j++)
cout<<matrix_A[i][j]<<"\t"; 11 13 17 0
cout<<endl; -19 -17 -13 -11
}
Matrix B =
//Matrix B = Transpose of Matrix A 11 -19
for (i=0; i<4; i++) 13 -17
for (j=0; j<2; j++) 17 -13
? 0 -11
58
Chapter 7 Array and String
59
59
60
Chapter 7 Array and String
return marks;
marks[i] = marks[i] + 5;
With the reference address,
return; memory of the array declared in
main( ) is accessed and updated.
} Need not to return
updated marks from
add5marks( ) to main( ).
61
61
62
62
Chapter 7 Array and String
63
64
64
Chapter 7 Array and String
65
return 0;
}
66
66
Chapter 7 Array and String
return average;
}
67
67
Quiz 7.6.1
Fill in the prototype for EnterMark ( )
#include <iostream> void enterMark(int marks[], int s)
using namespace std; {
int i;
//Prototype
? for (i=0; i<s; i++){
double avMark(int marks[], int s); cout << "Enter mark of student "
<< i+1 << ": ";
int main() cin >> marks[i];
{ }
int marks[7];
double ave; return;
}
enterMark(marks, 7);
ave = avMark(marks, 7); double avMark(int marks[], int s)
{
cout << "\nAverage mark = " double sum, ave;
<< ave << endl; int i;
return 0;
} for (sum=0, i=0; i<s; i++)
sum += marks[i];
ave = sum/s;
return ave;
}
68
68
Chapter 7 Array and String
Quiz 7.6.2
Fill in the missing statement.
#include <iostream> void enterMark(int marks[], int s)
using namespace std; {
int i;
//Prototype
void enterMark(int marks[], int s); for (i=0; i<s; i++){
int findMax(int marks[], int s); cout << "Enter mark of student "
int main() << i+1 << ": ";
{ cin >> marks[i];
int marks[7], maxMark; }
enterMark(marks, 7); return;
}
?
int findMax(int marks[], int s)
cout << "\nHighest mark = "
<< maxMark << endl; {
int i, curMax;
return 0;
} curMax = marks[0];
for (i=1; i<s; i++){
if (curMax < marks[i])
curMax = marks[i];
}
return curMax;
}
69
69
Quiz 7.6.3
Fill in the missing statement.
#include <iostream> void enterMark(int marks[], int s)
using namespace std; {
int i;
//Prototype
void enterMark(int marks[], int); for (i=0; i<s; i++){
double passRate(int marks[], int); cout << "Enter mark of student "
int main() << i+1 << ": ";
{ cin >> marks[i];
int marks[7]; }
return;
enterMark(marks, 7); }
cout << "\nPassing Rate = "
<< passRate(marks, 7) ?
<< "%\n"; {
int i;
return 0;
} double numPass;
70
70