Содержание

Слайд 2

Multiple-Subscripted Arrays

Multiple subscripts
a[ i ][ j ]
Tables with rows and columns
Specify

Multiple-Subscripted Arrays Multiple subscripts a[ i ][ j ] Tables with rows
row, then column
“Array of arrays”
a[0] is an array of 4 elements
a[0][0] is the first element of that array

Row subscript

Array name

Column subscript

Слайд 3

Multiple-Subscripted Arrays

To initialize
Default of 0
Initializers grouped by row in braces
int b[ 2

Multiple-Subscripted Arrays To initialize Default of 0 Initializers grouped by row in
][ 2 ] = { { 1, 2 }, { 3, 4 } };
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };

Row 0

Row 1

Слайд 4

Multiple-Subscripted Arrays

Referenced like normal
cout << b[ 0 ][ 1 ];
Outputs 0
Cannot reference

Multiple-Subscripted Arrays Referenced like normal cout Outputs 0 Cannot reference using commas cout Syntax error
using commas
cout << b[ 0, 1 ];
Syntax error

Слайд 5

1 // Fig. 3.22: fig04_22.cpp
2 // Initializing multidimensional arrays.
3 #include
4
5

1 // Fig. 3.22: fig04_22.cpp 2 // Initializing multidimensional arrays. 3 #include
using std::cout;
6 using std::endl;
7
8 void printArray( int [][ 3 ] );
9
10 int main()
11 {
12 int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
13 int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
14 int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
15
16 cout << "Values in array1 by row are:" << endl;
17 printArray( array1 );
18
19 cout << "Values in array2 by row are:" << endl;
20 printArray( array2 );
21
22 cout << "Values in array3 by row are:" << endl;
23 printArray( array3 );
24
25 return 0; // indicates successful termination
26
27 } // end main

Слайд 6

28
29 // function to output array with two rows and three

28 29 // function to output array with two rows and three
columns
30 void printArray( int a[][ 3 ] )
31 {
32 for ( int i = 0; i < 2; i++ ) { // for each row
33
34 for ( int j = 0; j < 3; j++ ) // output column values
35 cout << a[ i ][ j ] << ' ';
36
37 cout << endl; // start new line of output
38
39 } // end outer for structure
40
41 } // end function printArray

Values in array1 by row are:
1 2 3
4 5 6
Values in array2 by row are:
1 2 3
4 5 0
Values in array3 by row are:
1 2 0
4 0 0

Слайд 7

Multiple-Subscripted Arrays

Next: program showing initialization
After, program to keep track of students grades
Multiple-subscripted

Multiple-Subscripted Arrays Next: program showing initialization After, program to keep track of
array (table)
Rows are students
Columns are grades

Слайд 8

1 // Fig. 3.23: fig04_23.cpp
2 // Double-subscripted array example.
3 #include
4
5

1 // Fig. 3.23: fig04_23.cpp 2 // Double-subscripted array example. 3 #include
using std::cout;
6 using std::endl;
7 using std::fixed;
8 using std::left;
9
10 #include
11
12 using std::setw;
13 using std::setprecision;
14
15 const int students = 3; // number of students
16 const int exams = 4; // number of exams
17
18 // function prototypes
19 int minimum( int [][ exams ], int, int );
20 int maximum( int [][ exams ], int, int );
21 double average( int [], int );
22 void printArray( int [][ exams ], int, int );
23

Слайд 9

24 int main()
25 {
26 // initialize student grades for three students (rows)
27

24 int main() 25 { 26 // initialize student grades for three
int studentGrades[ students ][ exams ] =
28 { { 77, 68, 86, 73 },
29 { 96, 87, 89, 78 },
30 { 70, 90, 86, 81 } };
31
32 // output array studentGrades
33 cout << "The array is:\n";
34 printArray( studentGrades, students, exams );
35
36 // determine smallest and largest grade values
37 cout << "\n\nLowest grade: "
38 << minimum( studentGrades, students, exams )
39 << "\nHighest grade: "
40 << maximum( studentGrades, students, exams ) << '\n';
41
42 cout << fixed << setprecision( 2 );
43

Слайд 10

44 // calculate average grade for each student
45 for ( int person

44 // calculate average grade for each student 45 for ( int
= 0; person < students; person++ )
46 cout << "The average grade for student " << person
47 << " is "
48 << average( studentGrades[ person ], exams )
49 << endl;
50
51 return 0; // indicates successful termination
52
53 } // end main
54
55 // find minimum grade
56 int minimum( int grades[][ exams ], int pupils, int tests )
57 {
58 int lowGrade = 100; // initialize to highest possible grade
59
60 for ( int i = 0; i < pupils; i++ )
61
62 for ( int j = 0; j < tests; j++ )
63
64 if ( grades[ i ][ j ] < lowGrade )
65 lowGrade = grades[ i ][ j ];
66
67 return lowGrade;
68
69 } // end function minimum

Слайд 11

70
71 // find maximum grade
72 int maximum( int grades[][ exams ],

70 71 // find maximum grade 72 int maximum( int grades[][ exams
int pupils, int tests )
73 {
74 int highGrade = 0; // initialize to lowest possible grade
75
76 for ( int i = 0; i < pupils; i++ )
77
78 for ( int j = 0; j < tests; j++ )
79
80 if ( grades[ i ][ j ] > highGrade )
81 highGrade = grades[ i ][ j ];
82
83 return highGrade;
84
85 } // end function maximum
86

Слайд 12

87 // determine average grade for particular student
88 double average( int

87 // determine average grade for particular student 88 double average( int
setOfGrades[], int tests )
89 {
90 int total = 0;
91
92 // total all grades for one student
93 for ( int i = 0; i < tests; i++ )
94 total += setOfGrades[ i ];
95
96 return static_cast< double >( total ) / tests; // average
97
98 } // end function maximum

Слайд 13

99
100 // Print the array
101 void printArray( int grades[][ exams ],

99 100 // Print the array 101 void printArray( int grades[][ exams
int pupils, int tests )
102 {
103 // set left justification and output column heads
104 cout << left << " [0] [1] [2] [3]";
105
106 // output grades in tabular format
107 for ( int i = 0; i < pupils; i++ ) {
108
109 // output label for row
110 cout << "\nstudentGrades[" << i << "] ";
111
112 // output one grades for one student
113 for ( int j = 0; j < tests; j++ )
114 cout << setw( 5 ) << grades[ i ][ j ];
115
116 } // end outer for
117
118 } // end function printArray

Слайд 14

The array is:
[0] [1] [2] [3]
studentGrades[0] 77 68 86 73
studentGrades[1] 96

The array is: [0] [1] [2] [3] studentGrades[0] 77 68 86 73
87 89 78
studentGrades[2] 70 90 86 81
Lowest grade: 68
Highest grade: 96
The average grade for student 0 is 76.00
The average grade for student 1 is 87.50
The average grade for student 2 is 81.75

Слайд 15

“Well, I’ll eat it,” said Alice, “and if it makes me grow

“Well, I’ll eat it,” said Alice, “and if it makes me grow
larger, I
can reach the key; and if it makes me grow smaller, I can creep
under the door; so either way I’ll get into the garden.”
Lewis Carroll, Alice’s Adventures in Wonderland

Слайд 16

VECTORS

Array
cannot change the length
Vector
the same purpose as arrays
except can change length

VECTORS Array cannot change the length Vector the same purpose as arrays
while the program is running
Like an array, a vector has a base type, and like an array, a vector stores a collection of values of its base type.

Слайд 17

Vectors

Library:
#include
Declaration:
vector name;
Example:
vector v;
vector v(10);

Vectors Library: #include Declaration: vector name; Example: vector v; vector v(10);

Слайд 18

Vectors

To add an element to a vector for the first time, you

Vectors To add an element to a vector for the first time,
normally use the member function push_back.
Example:

Слайд 19

Vectors

The number of elements in a vector is called the size of

Vectors The number of elements in a vector is called the size
the vector.
The member function size can be used to determine how many elements are in a vector.
Example:

Слайд 20

// Demonstrating C++ Standard Library class template vector.

// Demonstrating C++ Standard Library class template vector.

Слайд 25

Two / Three / Multi Dimensioned arrays using vector

A two dimensional array

Two / Three / Multi Dimensioned arrays using vector A two dimensional
is a vector of vectors.
The vector contructor can initialize the length of the array and set the initial value.
Example of a vector of vectors to represent a two dimensional array:
vector< vector > vI2Matrix(3, vector(2,0));

Слайд 26

Loop by index:
0
1
10
11
20
21

Loop by index: 0 1 10 11 20 21

Слайд 27

Two / Three / Multi Dimensioned arrays using vector

A three dimensional vector

Two / Three / Multi Dimensioned arrays using vector A three dimensional
would be declared as:

Слайд 29

Quiz

1. What is vector?
Benefits of vector over array.
Difference between array

Quiz 1. What is vector? Benefits of vector over array. Difference between
and vector.
Which one is better for what purpose?
2. Write a program that create a 2-dimensional vector vec1.
initialize all elements of a vector to 1.
then output content of the vector vec1.
Имя файла: Lecture_05.pptx
Количество просмотров: 24
Количество скачиваний: 0