{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

(7) Loops Definite & Indefinite

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

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

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

View Full Document
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 number of times 2009 Arrays 11 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
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 computing GCD’s For example, reducing rational numbers to their simplest form in seminumeric computations We present a very simple (slow) algorithm

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

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

{[ snackBarMessage ]}

### Page1 / 8

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

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

View Full Document
Ask a homework question - tutors are online