Programmng Assgnment Sx, 07 Programmng Assgnment Sx Larry Caretto Mechancal Engneerng 09 Computer Programmng for Mechancal Engneers Outlne Practce quz for actual quz on Thursday Revew approach dscussed last class for passng worksheet arrays as nput arguments n a VBA functon Revew programmng assgnment sx Work on programmng assgnment sx, 07 Semester Calendar Date Assgnment Date Assgnment 9 Assgnment 6 Due 4 8 Quz 5 on Arrays Programmng Exam Fnal Exam :45 to :45 pm Sub samplearrayquz() Dm x( To 00) As Double Dm k As Long, row As Long Dm col As Long x(k) = k ^ + * k ext k For col = To 5 Step Show output from sub on worksheet ext col ext row End Sub 3 4 Revew VBA Arrays from Excel Wll be two-dmensonal VBA arrays unless cells method used to set values for one-dmensonal array For range.value or range nput argument Frst array ndex wll run from to number of rows Second array ndex wll run from to number of columns Range wth only one row or one column wll stll be two-dmensonal array 5 D Excel Worksheet Arrays What happens when we use the column array x or the row array y as an nput to a VBA functon? 6 ME 09 Computer Programmng for Mechancal Engneers
Programmng Assgnment Sx, 07 VBA Functon to Read x and y Functon readarrays(xin As Range, _ yin As Range) As Double Dm x As Varant, y As Varant Dm nrows As Long, ncols As Long x = xin nrows = UBound(x,) ncols = UBound(x,) y = yin nrows = UBound(y,) ncols = UBound(y,) 7 Readng Column Array (Debug) The second argument to the ubound functon s for the frst dmenson of a D array and for the second dmenson 8 Readng Column Array (Debug) Readng Row Array (Debug) 9 0 Readng Row Array (Debug) y Programmng Assgnment Sx See onlne assgnment for detals Ft straght lne to expermental data Workbook pa6start.xlsm has data and shell for functon Equaton of ftted lne: Equatons to use for assgnment: y x x x y ( x b yˆ y x ( x) R y y )( y) a y bx ME 09 Computer Programmng for Mechancal Engneers
Programmng Assgnment Sx, 07 Programmng Assgnment Sx II Download workbook pa6start.xlsm wth VBA shell for array functon Create functon that reads worksheet data nto type Varant arrays x and y Copy approach used n lectures Do calculatons for a, b, and R usng ths array data Use worksheet shell statements to create your own array functon 3 Assgnment Sx Code Shell Functon <yourfunctoname> _ ( <arguments> ) as Varant Dm output( To 3, To ) As Varant <put code for computatons here> output(, ) = " Slope = " output(, ) = "Intercept = " output(3, ) = "R-squared = " output(, ) = <Result for b> output(, ) = <Result for a> output(3, ) = <Result for R^> <yourfunctoname> = output End Functon 4 Program Desgn All equatons shown below have sums of data (or calculatons from data) over same set of (x, y ), =, All calculatons can be done n a sngle For loop except for R numerator yˆ y y y x yˆ y R y y x b x y ( x)( y) x ( x) a y bx 5 Array Functons An Excel array functon s one whch produces values n two or more cells Example s Lnest regresson functon Select multple cells (here D:E5) for output, enter formula, Practce: and press Copy data Ctrl+Shft+Enter from F:G6 and enter array formula n D:E5 6 Result of Lnest Array Functon Output results from array formula See Help for Lnest functon to get meanng of all results for statstcal lne ft of ths functon Input range from prevous slde Ftted lne for data s 0.74.08 Wth R = 0.99557 7 Sub samplearrayquz() Dm x( To Sample 00) As Quz Double What s output Dm k As Long, row As Long from ths sub? Dm col As Long x(k) = k ^ + * k x array s ext k defned here For col = To 5 Step ext col Values of x array sent ext row to worksheet here End Sub 8 ME 09 Computer Programmng for Mechancal Engneers 3
Programmng Assgnment Sx, 07 Sub samplearrayquz() Dm x( To Sample 00) As Quz Double Dm k As Long, row As Long Dm col As Long x(k) = k ^ + * k ext k and 0 Row loop wll produce output n rows, 4, 7 For col = To 5 Step ext col Col loop wll produce output ext row n columns, 3 and 5 End Sub (columns A, C, and E) 9 Sub samplearrayquz() Dm x( To Sample 00) As Quz Double Dm k As Long, row As Long Dm col As Long x(k) = k ^ + * k ext k For row n =, 3, and 5 For col = To 5 Step ext col So x(k) for k =, 3, 5 s ext row + () = 3, 3 + (3) =5, End Sub and 5 + (5) = 35 0 Frst Row of Output Sub samplearrayquz() Dm x( To Sample 00) As Quz Double Dm k As Long, row As Long Dm col As Long x(k) = k ^ + * k ext k For row 4 n = 4,, and 0 For col = To 5 Step ext col So x(k) for k = 4,, 0 s 4 ext row + (4) = 4, + () End Sub =68, and 0 + (0) = 440 Frst Two Rows of Output 3 Sub samplearrayquz() Dm x( To Sample 00) As Quz Double Dm k As Long, row As Long Dm col As Long x(k) = k ^ + * k ext k For row 7 n = 7,, and 35 For col = To 5 Step ext So col x(k) for k = 7,, 35 s 7 + (7) ext = row 63, + () = 483, and 35 + End Sub(35) = 95 4 ME 09 Computer Programmng for Mechancal Engneers 4
Programmng Assgnment Sx, 07 Frst Three Rows of Output 5 Sub samplearrayquz() Dm x( To Sample 00) As Quz Double Dm k As Long, row As Long Dm col As Long x(k) = k ^ + * k ext k For row 0 n = 0, 30, and 50 For col = To 5 Step ext So col x(k) for k = 0, 30, 50 s 0 + ext (0) row = 0, 30 + (30) = 960, and End Sub50 + (50) = 600 6 Complete Output from Sub 7 ME 09 Computer Programmng for Mechancal Engneers 5