Download presentation
Presentation is loading. Please wait.
Published byJohn Parrish Modified over 8 years ago
1
ME 142 Engineering Computation I More Loops & Arrays
2
Key Concepts Nesting Loops Dynamic Arrays Multi-Dimensional Arrays
3
Nesting Loops
4
Nesting What is Nesting? How do you Nest loops?
5
Nesting Example Counter = 1 For I = 1 to 5 For J = 1 to 4 Cells(I,J) = Counter Counter = Counter + 1 Next J Next I
6
Dynamic Arrays
7
Dynamic Array Created without a preset number of elements When using dynamic arrays, number of elements is typically determined when code is run Use ReDim command to establish number of elements Dynamic arrays can be reused
8
Dynamic Array Syntax Dim ArrayName() … ReDim ArrayName(NumElements)
9
Dynamic Array Example Sub ReDimDemo() Dim A() Num = Val(InputBox("Enter number")) ReDim A(1 To Num) For i = 1 To Num A(i) = InputBox("Enter name") Next I For i = 1 To Num Cells(1, i) = A(i) Next I End Sub
10
Multi-Dimensional Arrays
11
Can be used to store a 2-D or 3-D grid of data as opposed to just a row or column Array Comparisons: Spreadsheet row- 1-dimensional array Spreadsheet column- 1-dimensional array Worksheet- 2-dimensional array Workbook- 3-dimensional array
12
Example Problem Use a two dimensional array to create table of temperatures and their conversions. User will be prompted for number of Temperatures User will enter temperatures in degrees F Program will convert temperatures to C, K, & R Program will create table all 4 temperatures
13
Sub MultArr() 'Temperature conversion example 'Get # of Temps Dim Temp() N = InputBox("Enter number of temperatures to convert") ReDim Temp(1 To N, 1 To 4) 'Read in F Temps For i = 1 To N Temp(i, 1) = InputBox("Enter Temp #" & i) 'read F temp. Next i 'Continued next slide…
14
'Convert Temps For i = 1 To N Temp(i, 2) = (Temp(i, 1) - 32) * 5 / 9 'convert to C Temp(i, 3) = Temp(i, 2) + 273 'convert to K Temp(i, 4) = Temp(i, 1) + 460 'convert to R Next i 'Write Table For i = 1 To N For j = 1 To 4 Cells(i + 1, j) = Temp(i, j) 'write temp. Next j Next i End Sub
15
Example Problem Use nested For Next loops to create a grid of random 2 digit numbers. Read directly from spreadsheet cells the number of rows and columns in the grid to be created.
16
Sub CreateGrid() 'Creates a grid of random numbers 'read rows and columns from spreadsheet NRow = Cells(2, 1) NCol = Cells(2, 2) Randomize For i = 1 To NRow For j = 1 To NCol Cells(i + 3, j) = Round(Rnd, 2) Next j Next i End Sub
17
Review Questions
18
Review Question Which of the following are valid examples of nesting loops: A.Example #1 B.Example #2 C.Example #3 D.Both Examples #1  E.Both Examples #1 & #3 For I = 1 to N For J = 1 to M … Next J Next I For I = 1 to N For J = 1 to M … Next I Next J For I = 1 to N … Next I For J = 1 to M … Next J Example #1Example #2Example #3
19
Review Question Multi-Dimensional Arrays How many elements may be stored in the following array: Dim MyArray(1 to 100, 1 to 100, 1 to 100) A.100 B.300 C.1000 D.10,000 E.1,000,000
20
Homework Help ‘n Hints
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.