Copyright © Genetic Computer School 2007 Discrete Math 2-1
Lesson 2
More uses of Matrices Arrays
Copyright © Genetic Computer School 2007 Discrete Math 2-2
Lesson 2 Overview
Building arrays from raw dataAdding rows or columnsArray arithmeticArrays for pricingMore about matrices
An array exampleBinary treesBack and trace pointersNetworksNetworks as arrays
Copyright © Genetic Computer School 2007 Discrete Math 2-3
Building arrays from raw data
One dimension arrayArr: array[1..3] of Real=
(10.25, -20.15, 30.85);
Multi-dimension array Arr: array[1..2,1..3] of Real=
((10.25, -20.15, 30.85), (14.50, 14.45, 10.75));
Copyright © Genetic Computer School 2007 Discrete Math 2-4
Adding rows or columns
Var arr:Array[1..2,1..3] of Integer;…(* adding row 2 *)Result:=0;For i:=1 to 3 do
Result:=Result + arr[2][i];
Copyright © Genetic Computer School 2007 Discrete Math 2-5
Array arithmetic
Add/Subtract arrays: Arrays must be same format (same number of columns and number of rows) “Scalar" multiplication: Multiply the “scalar” with all elements of the array Multiply two array: in order to multiply arrays A and B the number of columns in A must equal the number of rows in B
Copyright © Genetic Computer School 2007 Discrete Math 2-6
Arrays for pricing
Array multiplication for order costing
178
106
205
195
6
9
2
13
8
201983
38401
212734
82457
Copyright © Genetic Computer School 2007 Discrete Math 2-7
More about matrices
The common operations on a matrix Add a constant vector/matrix to a matrix Multiply/divide a scalar to a matrix Add/Subtract from another matrix Cell-by-cell multiplication Multiply by a row/column vector Multiply, selectively, by a square matrix Multiply by another matrix
Copyright © Genetic Computer School 2007 Discrete Math 2-8
An array example
“A manufacturing company, in factories at Andover and Bristol respectively, employs the following numbers of people in 4 grades for each of 6 job-categories and at different monthly pay scales”
Copyright © Genetic Computer School 2007 Discrete Math 2-9
Binary trees
1
2
3
4
5
Copyright © Genetic Computer School 2007 Discrete Math 2-10
Binary treesGreat-Great-Grand-parents
Great-Grand-parents
Grand-parents
Parents
Sons and Daughters
Copyright © Genetic Computer School 2007 Discrete Math 2-11
Back and trace pointers
BACK POINTERS can be added to each node so that it is possible to work up the tree from any node to trace its ancestors.TRACE POINTER can also be added which, from any node, would allow to trace the next in alphabetical (or numerical) sequence.
Copyright © Genetic Computer School 2007 Discrete Math 2-12
Networks
A
B
C
D
Copyright © Genetic Computer School 2007 Discrete Math 2-13
Networks as arrays
Star networkA B C D
A 0 1 1 1S = B 1 0 0 0
C 1 0 0 0D 1 0 0 0
Copyright © Genetic Computer School 2007 Discrete Math 2-14
Networks as arrays
RingA B C D
A 0 0 0 0R = B 0 0 1 1
C 0 1 0 1D 0 1 1 0
Copyright © Genetic Computer School 2007 Discrete Math 2-15
Networks as arrays
LANA B C D
A 0 0 1 1L = B 0 0 0 0
C 1 0 0 1D 1 0 1 0
Top Related