Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction to Programming
(in C++)
my_matrix:
Multi-dimensional vectors
3
5
7
8
0
2
1
6
9
0
3
4
Matrices
3
5
7
8
0
2
1
6
9
my_matrix[0][2]
n-dimensional vectors
0
3
4
typedef
typedef
typedef
typedef
my_matrix[1][3]
vector<int> Dim1;
vector<Dim1> Dim2;
vector<Dim2> Dim3;
vector<Dim3> Matrix4D;
Matrix4D my_matrix(5,Dim3(i+1,Dim2(n,Dim1(9))));
my_matrix[2]
Introduction to Programming
Introduction to Programming
Sum of matrices
1
1
1 + 2
3
0
1
3
1 = 2
2
1
By rows
By columns
j
0
0
1
Introduction to Programming
// Pre: a and b are non-empty matrices and have the same size.
// Returns a+b (sum of matrices).
// Pre: a and b are non-empty matrices and have the same size.
// Returns a+b (sum of matrices).
Introduction to Programming
Introduction to Programming
Transpose a matrix
Transpose a matrix
3 8 1
0 6 2
4 5 9
3 0 4
8 6 5
1 2 9
Introduction to Programming
Introduction to Programming
Is a matrix symmetric?
3 0 4
0 6 5
4 2 9
symmetric
not symmetric
Introduction to Programming
10
Is a matrix symmetric?
3 0 4
0 6 5
4 5 9
11
Introduction to Programming
12
Search in a matrix
Search in a matrix
// Pre:
// Post:
//
//
m is a non-empty matrix
i and j define the location of a cell
that contains the value x in M.
In case x is not in m, then i = j = -1
m is a non-empty matrix
i and j define the location of a cell
that contains the value x in m.
In case x is not in m, then i = j = -1.
13
Introduction to Programming
14
m[i][j] m[i][j+1]
m[i][j] m[i+1][j]
10
10
12
17
21
25
1 4 5 7
2 5 8 9
6 7 10 11
9 11 13 14
11 12 19 20
13 14 20 22
Introduction to Programming
1 4 5 7
2 5 8 9
6 7 10 11
9 11 13 14
11 12 19 20
13 14 20 22
12
13
15
20
23
26
15
Introduction to Programming
10
10
12
17
21
25
12
13
15
20
23
26
16
10
10
12
17
21
25
12
13
15
20
23
26
1 4 5 7
2 5 8 9
6 7 10 11
9 11 13 14
11 12 19 20
13 14 20 22
17
Introduction to Programming
10
10
12
17
21
25
12
13
15
20
23
26
18
1 4 5 7
2 5 8 9
6 7 10 11
9 11 13 14
11 12 19 20
13 14 20 22
Introduction to Programming
10
10
12
17
21
25
12
13
15
20
23
26
1 4 5 7
2 5 8 9
6 7 10 11
9 11 13 14
11 12 19 20
13 14 20 22
19
Introduction to Programming
10
10
12
17
21
25
12
13
15
20
23
26
20
1 4 5 7
2 5 8 9
6 7 10 11
9 11 13 14
11 12 19 20
13 14 20 22
Introduction to Programming
10
10
12
17
21
25
12
13
15
20
23
26
21
Introduction to Programming
22
i = nrows - 1;
j = 0;
// Invariant: x can only be found in M[0..i,j..ncols-1]
while (i >= 0 and j < ncols) {
if (m[i][j] < x) j = j + 1;
else if (m[i][j] > x) i = i 1;
else return;
}
nrows ncols
nrows + ncols
i = -1;
j = -1;
}
Introduction to Programming
23
Introduction to Programming
24
Matrix multiplication
Matrix multiplication
// Pre: a is a non-empty nm matrix, b is a non-empty mp matrix.
// Returns ab (an np matrix).
1
3
-1
2
2
0
1
-1
-1
2
3
4
1
8
3 0
4 11
Introduction to Programming
25
Introduction to Programming
Matrix multiplication
Matrix multiplication
Introduction to Programming
Accumulation
26
27
Introduction to Programming
28