Lecture 17 - Arrays

Lecture 17 - Arrays - 3/23/2010 TODAY’S TOPICS: Arrays...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 3/23/2010 TODAY’S TOPICS: Arrays Engr 160 Arrays How will learning this help you? Connecting to your Background • What are arrays? – Arrays are mathematical structures that enable you to work with multiple values of a variable rather that just one at a time. – Arrays are “matrices” – Matrix math enables you to work with systems of equations Community Mailbox Engr 160 1 3/23/2010 Arrays • This is an array of mailboxes – 5 rows – 4 columns • Where is cell (element) (3,2)? – Row number is the first number – Column number is the second number Engr 160 Examples of Arrays • Say you had the following “system” of equations in which you needed to solve for x, y, and z: Engr 160 2 3/23/2010 Arrays System of Equations A New Way to Write This This “array” is 3 rows x 3 columns These “arrays” are 3 rows x 1 column Engr 160 Arrays A vector is a matrix with only 1 row or 1 column A is the “coefficient matrix” b is the “unknown vector” c is the “right hand side vector” Engr 160 3 3/23/2010 Arrays We are multiplying the matrix A and the vector b to get the vector c Row 1 Row 2 Row 3 C o l u m n 1 C o l u m n 2 C o l u m n 3 What do the numbers mean? • A11 is in Row 1, column 1 • A12 is in Row 1, column 2 • A 13 is in Row 1, column 2 What about b and c? what rows and columns are they in? Engr 160 Arrays and VBA Let’s read in these arrays from a file How do we dim Arrays? • Dim A(1 to 3,1 to 3) As Single – The first 1 to 3 says: make 3 rows and number them 1 to 3 – The second 1 to 3 says: make 3 columns and number them 1 to 3 3 2 4 8 6 5 ‐1 3.5 ‐12 • How about b and c? 14 22 33 This is Array A This is Array (vector) c – Dim b(1 to 3) as Single – Dim c (1 to 3) As Single Engr 160 4 3/23/2010 Arrays and VBA How do we use Arrays in subroutines? Let’s write the following subroutines • Dim A(1 to 3,1 to 3) As Single • – Same rules as “scalar” values – Must be the same type • • Single, double, integer, character – Must be in the same order in the argument list – Must be passed as ByRef • Write a subroutine that multiplies each value of the matrix A by 2. Write a second subroutine that finds the biggest value of the vector c Write the results from this exercise into a file called sample_arrays_sub_out.txt Engr 160 In-Class Find vector c Engr 160 5 3/23/2010 Arrays • Let the rows vary from 1 to 3 – i = 1 to 3 • Let the columns vary from 1 to 3 – j = 1 to 3 Engr 160 Write a VBA program that will 1. Using loops, assign the numbers 1 to 12 to an array named A. A is a 3 x 4 (3 rows, 4 columns). 2. Using a subroutine called Addem_rows add the elements of each row of A and assign the sum of each row to a vector called b. 3. Add the elements of each column of A and assign the sum of each column to a vector called c in a subroutine called Addem_columns 4. Find the biggest value in each row and each column in a subroutine called large Engr 160 6 ...
View Full Document

This note was uploaded on 02/09/2012 for the course ENGR 160 taught by Professor Sengupta during the Spring '07 term at Iowa State.

Ask a homework question - tutors are online