Title: CSI 1100 Lab 10 November 1519
1CSI 1100 Lab 10November 15-19
2Matrix Example 1 Objective
- Design an algorithm that will create a matrix of
wind chill values for various temperatures and
wind speeds. - The set of temperatures will be stored in an
array - The set of wind speeds will be stored in a second
array - An algorithm is already available that calculates
the wind chill for a specific temperature and
wind speed. - Implement the algorithm in Java.
- For those of you who havent yet experienced an
Ottawa winter, you may want to keep the results
for future use ?
3Given algorithm Wind Chill calculation
- Suppose we are given the following algorithm
- Chill ? WindChill( Temperature, WindSpeed )
- that calculates the wind chill for a given
temperature in C, and wind speed in km/hr. - Restrictions
- WindSpeed ? 5.0 km/hr
- Temperature should be between -50.0C and 5.0C.
- More info http//www.msc.ec.gc.ca/education/windc
hill/
4Wind Chill Table
- Design the following algorithm
- GIVENS
- TempArray (an array of temperatures in C)
- NT (length of TempArray)
- SpeedArray (an array of wind speeds in km/hr)
- NS (length of SpeedArray)
- RESULTS
- ChillTable (a matrix of wind chill values, with
NT rows - and NS
columns) - INTERMEDIATES
- (to be determined)
- HEADER
- ChillTable ? WindChillTable( TempArray, NT,
SpeedArray, NS)
5Implementation in Java
- Get the following file from the virtual campus
for lab 10 - WindChill.java
- The file WindChill.java already has a main( )
method as well as an implementation of the method - windChill( temperature, windSpeed )
- The file WindChill.java contains the method
- printTable( temperatureArray, speedArray,
chillTable ) - used to print the matrix.
6Sample output
- CSI1100 Fall 2004, Lab 10, Example 2
- Name Alan Williams, Student 81069665
- Lab section 999, TA Grace Hopper
- Enter a set of temperatures between -50 and 5
- 5 0 -5 -10 -15 -20 -25 -30 -35 -40
- Enter a set of wind speeds
- 5 10 15 20 25 30 35 40
- Wind Chill Table
- Speed 5.0 10.0 15.0 20.0 25.0 30.0 35.0
40.0 - --------------------------------------------------
------ - Temp.
- 5.0 4.1 2.7 1.7 1.1 0.5 0.1 -0.4
-0.7 - 0.0 -1.6 -3.3 -4.4 -5.2 -5.9 -6.5 -7.0
-7.4 - -5.0 -7.3 -9.3 -10.6 -11.6 -12.3 -13.0 -13.6
-14.1 - -10.0 -12.9 -15.3 -16.7 -17.9 -18.8 -19.5 -20.2
-20.8
Lowest value during winter 2003 in Ottawa (10th
coldest winter in past 56 years)
Jan. 1994 this value was achieved in Ottawa!
(coldest winter since 1948)
7Example 2 Matrix Transpose
- Write an algorithm that will take a matrix of
integers A and transpose the matrix to produce a
new matrix AT. The transpose of the matrix is
such that element arc in the original matrix will
be element aTcr in the transposed matrix. The
number of rows in A becomes the number of columns
in AT, and the number of columns in A becomes the
number of rows in AT. - For example
-
8Translate to Java
- Translate your matrix transpose algorithm to Java
- The header of the method should be as follows
- public static int transpose( int a )
- Get the following file from the virtual campus
for lab 10 - MatrixLib.java
- The file MatrixLib.java has the following useful
methods - Method to read a matrix of integers, of specified
dimension. - public static int readIntMatrix( int
numRows, int numCols ) - Method to print a matrix of integers with nice
formatting. - public static void printMatrix( int
matrix )
9Example 3 Matrix Multiplication
- Suppose that A is an m n matrix and B is an n
p matrix. The element at row i and column j in A
is denoted by aij. - Let C A B. Then, C will be an m p matrix,
and for 0 i lt m, and 0 j lt p, - Write an algorithm to multiply two given
matrices. -
10Translate to Java
- Translate your matrix multiplication algorithm to
Java - The header of the method should be as follows
- public static int product( int a,
int b ) - Use the methods from MatrixLib.java to read and
print the matrices.
11- solutions and extra information follow this
slide
12The Wind Chill calculation
- Here is the complete wind chill algorithm, for
your information. It has already been
implemented in WindChill.java. - Source Environment Canada
- GIVENS
- Temperature (a temperature in C between -50
and 5) - WindSpeed (a wind speed in km/hr must be ? 5
km/hr) - RESULTS
- Chill (the wind chill value)
- INTERMEDIATES
- VFactor (the wind speed raised to the power
0.16) - HEADER
- Chill ? WindChill( Temperature, WindSpeed )
- BODY
- VFactor ? (WindSpeed) 0.16
- Chill ? 13.12 0.6215 Temperature - 11.37
VFactor - 0.3965 Temperature VFactor
13Wind Chill Algorithm (page 1)
- GIVENS
- TempArray (an array of temperatures in C)
- NT (length of TempArray)
- SpeedArray (an array of wind speeds in km/hr)
- NS (length of SpeedArray)
- RESULTS
- ChillTable (a matrix of wind chill values, with
NT rows - and NS
columns) - INTERMEDIATES
- T (index into temperature array)
- S (index into speed array)
- HEADER
- ChillTable ? WindChillTable( TempArray, NT,
SpeedArray, NS)
14Wind Chill Algorithm (page 2)
ChillTable ? MakeNewArray(NT) T ? 0
T lt NT
true
false
ChillTableT ? MakeNewArray(NS) S ? 0
S lt NS
false
true
ChillTableTS ? WindChill(TempArrayT,
SpeedArrayS) S ? S 1
T ? T 1
15Transpose Algorithm (page 1)
- GIVENS
- A (an array)
- NRows (number of rows in A)
- NCols (number of columns in A)
- RESULTS
- AT (the transposed array)
- INTERMEDIATES
- TRow (row index in transposed array)
- TCol (column index in transposed array)
- HEADER
- AT ? Transpose( A, NRows, NCols )
16Transpose Algorithm (page 2)
AT ? MakeNewArray(NCols) Row ? 0
TRow lt NCols
true
false
ATTRow ? MakeNewArray(NRows) TCol ? 0
TCol lt NRows
true
false
ATTRowTCol ? ATColTRow TCol ? TCol 1
TRow ? TRow 1
17Multiplication Algorithm (page 1)
- GIVENS
- A (an array of numbers)
- B (another array of numbers)
- NARows (number of rows in matrix A)
- NACols (number of columns in matrix A)
- NBRows (number of rows in matrix B)
- NBCols (number of columns in matrix B)
- RESULTS
- C (a matrix that is the product of A and B,
with - NARows rows and NBCols columns)
- INTERMEDIATES
- Row (row index for C)
- Col (column index for C)
- Index (summation index for matrix product)
- HEADER
- C ? Multiply( A, NARows, NACols, B, NBRows,
NBCols)
18Multiplication Algorithm (page 2)
C ? MakeNewArray(NARows) Row ? 0
Row lt NARows
true
CRow ? MakeNewArray(NBCols) Col ? 0
false
Col lt NBCols
true
false
Sum ? 0 Index ? 0
Index lt NACols
true
false
CRowCol ? Sum Col ? Col 1
Sum ? Sum ARowIndexBIndexCol Index ?
Index 1
Row ? Row 1