(7) Loops Definite & Indefinite

(7) Loops Definite & Indefinite - Input Values into an...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 More Lists and Loops Nathan Friedman Input Values into an Array (DO – LOOP) REAL :: A(1000) INTEGER :: I, SIZE READ(**)SI ZE 2009 Arrays 2 READ( , ) SIZE DO I = 1, SIZE READ (*,*) A(I) END DO Reads one value per line (Each new read command starts a new line) Input Values into an Array (DO – LOOP) In our ISBN example, we could input the digits as follows: 2009 Arrays 3 DO I = 1, 10 READ (*,*) digits(i) END DO We would have to input 10 lines. The first value on each line would be read Input Values into an Array (Implied DO – LOOP) REAL :: A(1000) INTEGER :: I, SIZE READ(*,*) SIZE 2009 Arrays 4 READ (*,*) (A(I), I=1,SIZE) Reads values sequentially from a line If there are not enough values on the line it starts a new line This is called an inline or implied DO loop Input Values into an Array (Implied DO – LOOP) In our ISBN example, we could input the digits as follows: READ (*,*) (digits(I), I=1,10) 2009 Arrays 5 We could input all of the digits on one or more lines separated by blanks The first 10 digits would be read and stored in the digits array Input Values into an Array (Implicit Implied DO – LOOP) REAL :: A(1000) INTEGER :: I, SIZE READ(*,*) SIZE 2009 Arrays 6 READ (*,*) A Reads values sequentially like an implied do loop It must fill the entire array, not just the
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Compute Sum of Array Elements REAL :: Data(100) REAL :: Sum . . . 2009 Arrays 7 Sum = 0.0 DO k = 0, 100 Sum = Sum + Data(k) END DO Inner Product of Vectors The inner product of two vectors is the sum of the products of corresponding elements. REAL :: V1(50), V2(50) 2009 Arrays 8 REAL :: InnerProduct INTEGER :: dim, n READ(*,*) dim !actual dimension of vector InnerProduct = 0.0 DO n = 1, dim InnerProduct = InnerProduct + V1(n)*V2(n) END DO Find Maximum Value How do we find the largest value in an array? Imagine a deck of cards that we look 2009 Arrays 9 through one at a time Keep track of the largest value Start with the one on the first card Keep looking and note whenever a larger value is found Find Maximum Value PROGRAM FINDMAX IMPLICIT NONE INTEGER :: MARKS(210) INTEGER :: MAX, I 2009 Arrays 10 READ(*,*) MARKS MAX = MARKS(1) DO I = 2, 210 IF (MARKS(I) > MAX) MAX = MARKS(I) END DO WRITE (*,*) “THE HIGHEST MARK IS: “, MAX END PROGRAM FINDMAX Definite Iterator ± The DO loop we have looked at is called a definite iterator ± The body of the loop is executed a fixed 2009 Arrays 11 number of times ± The control variable, i , takes on the values x, x+s, x+2s, …, x+ks where ± x is the initial value, ± s is the step size and ± x+ks final value < x+(k+1)s Indefinite Iterators ± For some applications, we do not know in advance how many times to repeat the computation 2009 Arrays 12 ± The loop continues until some condition is met and then it terminates
Background image of page 2
3 GCD ± The greatest common divisor of two integers is the largest number that divides both of them ± There are numerous applications that require 2009 Arrays 13 There are numerous applications that require
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2010 for the course COMP COMP 206 taught by Professor Vybihal during the Spring '04 term at McGill.

Page1 / 8

(7) Loops Definite &amp; Indefinite - Input Values into an...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online