380 likes | 493 Views
E0001 Computers in Engineering. FOR ..NEXT Loops Arrays. Readings and exercises. Schneider Section 6.3 p 209 exercises 6.3 p 214 as required Module 12 from Study Book. For i = 1 to 4 x = x + i PRINT i, x NEXT. Output . i = i + 1. FOR NEXT loops. FOR i = 2 TO 10 STEP 3
E N D
E0001 Computers in Engineering FOR ..NEXT Loops Arrays
Readings and exercises • Schneider Section 6.3 p 209 • exercises 6.3 p 214 as required • Module 12 from Study Book
For i = 1 to 4 x = x + i PRINT i, x NEXT Output i = i + 1 FOR NEXT loops
FOR i = 2 TO 10 STEP 3 x = x + i PRINT i, x NEXT output i = i + 3 FOR NEXT loops
FOR NEXT LOOPS FOR controlvariable = low TO high [STEP increment] action action NEXT [controlvariable] FOR i = 1 to 10 STEP 2 FOR j = x to y PRINT i INPUT z x = x + i READ a NEXT NEXT
assign starting values to variables that appear in the loop assign starting values loop variables FOR compare loop variable to final value loop variable greater loop variable =< execute loop statements NEXT Increment loop variable by step amount continue with program
FOR i = 1 TO 10 PRINT i NEXT FOR i = 104 TO 345 STEP 3 PRINT i * i NEXT FOR i = 10 TO 1 STEP -1 PRINT I NEXT FOR I = 1 TO N c = c + 1 NEXT EXAMPLES
Nested FOR NEXT LOOPS FOR var1 = low1 to high1 [STEP inc1] action.... FOR var2 = low2 to high2 [STEP inc] action.... NEXT action.. NEXT
each iteration of outer loop requires complete execution of inner loop • QB allows for consolidation of NEXT clauses • total no. of times inner loop is executed is = (high1 - low1+1)(high2 - low2 +1).... (highn- lown+1)
FOR I = 1 TO 3 FOR J = 1 TO 3 PRINT I + J NEXT NEXT complete the table
FOR I = 1 TO 3 FOR J = 1 TO 3 PRINT I + J NEXT NEXT
FOR NEXT Loops • used when know number of loop executions • used as element position in arrays • maybe nested • do not use EXIT FOR - > DO LOOP
INDEPENDENT LOOPS FOR A.... --- --- NEXT A --- --- FOR b ---- ---- NEXT b NESTED LOOPS FOR a --- --- FOR b ---- ---- NEXT b --- --- NEXT a
crossed loops FOR a -- -- FOR b -- NEXT a NEXT b nested-same counter FOR a -- FOR a ---- NEXT a --- NEXT a ILLEGAL LOOPS
Rainfall for the Month of February • individual variable names
Use Arrays • set up a variable called Feb • allocate 28 storage locations to that variable • into each location put the rainfall for that day
DIM Feb(28) FOR I= 1 TO 28 INPUT Feb(I) NEXT ‘sets aside variable Feb with 28 locations ‘use variable I specify each position of Feb ‘input numbers into each position ‘endloop The Process
Add to the Program FOR I = 1 TO 28 TOTAL = TOTAL + Feb (I) NEXT PRINT “total rainfall for February = “; TOTAL amt = Feb (1) + Feb (10) ‘use array element as ordinary variable
FOR I = 1 TO 3 FOR J = 1 TO 3 action NEXT NEXT Make use of i and j
2 Dimensional Array Transpose into matrix I - ROW J- COLUMN
2 dimensional arrays • set aside memory; 2 dimensions; number of rows and columns • name matrix as a variable - X e.g. DIM X (3,3) • elements specified by subscripts for row & column (I, J) e.g. X(I,J) or X$(2,3)
DIM a(3,3) ‘allocate storage FOR i = 1 to 3 ‘i and j as subscripts FOR j = 1 to 3 INPUT a(i,j) ‘input into array NEXT ‘using subscripts NEXT i = 2: j=1 PRINT a(i,j) ‘use any array element by specifying exact location
Arrays • collection of storage locations • accessed by the same variable name • store the same kind of data - numeric or string • individual storage locations are called elements
number of storage locations associated with a variable must be indicated - DIM statement • subscripts identify particular elements of an array • arrays may be one or two dimensional • to identify a particular element both the array name and the subscript must be specified • use array element as an ordinary variable as long as subscript is specified
DIM statement 1. DIM variable ([lower TO] upper), .... for single dimension arrays e.g. DIM names$(7), area(2 TO 10) 2. DIM variable(row,column) for two dimensional arrays e.g. DIM num(2,3), DIM rate (2 to 11, 1 to 5)
DIM day$(7) FOR I = 1 TO 7 READ day$(I) NEXT DATA Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday PRINT MID$(day$(3), 2, 3) k = 7 : PRINT day$(k) j = 1 : day$(j) = “cancelled” FOR X = 1 TO 7 PRINT day$(X) NEXT
Problem • Using the first part of the previous program write some code which will display the days of the week (Monday to Sunday) and request the number of hours studied each day and calculate the average.
Example • Investors are planning to build a ski resort. They have three possible sites under study. Naturally they are interested in the snowfall at these sites. They have collected data on the snowfall, in centimetres, for the three sites for the last 5 years as shown following:
Data Year Site Num12345 1 106 147 139 153 126 2 143 151 117 134 139 3 136 143 130 142 145
problem • Write a program that • takes the information from DATA statements and places it in a 2 dimensional array • calculates the average snowfall for the last 5 years for each of the three sites • prints out the data and the calculated average in a table • Calculates and prints the grand average snowfall for ALL three sites.
FOR i = 1 to 2 PRINT “hello” NEXT • i = 1 (initial); i > final? • prints hello • i is incremented by STEP (default of 1) and loops back; i = 2; i > final? • prints hello • i is incremented; i= 3; tests; exits loop
Example FOR i = 1 TO 10 STEP 2 FOR j = 20 TO 2 STEP -.02 FOR k = 2 TO 5 FOR i = X TO Y STEP a/b^2
FOR I = 1 TO 3 FOR J = 1 TO 3 PRINT I + J NEXT NEXT Complete the table
FOR I = 1 TO 3 FOR J = 1 TO 3 PRINT I + J NEXT NEXT Complete the table
FOR NEXT Loops • used when know number of loop executions • used as element position in arrays • maybe nested • do not use EXIT FOR - > DO LOOP
used for loops with a fixed or known number of iterations • low, high and increment can be variables, numbers, or expressions • i - controlvariable: controls the number of iterations in the loop • when the loop starts controlvariable = lowvariable • at loop end (NEXT) contolvariable = controlvariable + increment
loop executes if controlvariable <= highvalue • loop does not execute if controlvariable is greater then high value • number of loop iterations; • (highvalue - lowvalue + 1)/increment • default increment is 1and cannot =0 • increment may be positive or negative • STEP values must be a consistent data type with controlvariable