Loop Invariant Review - PowerPoint PPT Presentation

About This Presentation
Title:

Loop Invariant Review

Description:

Store in h the index of the maximum value. of m[a..j]; Swap m[h] and m[j]; m. a. j. b ... m = month and. n is the total number of days in months 1..m-1. m= 1; ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 12
Provided by: david148
Category:
Tags: invariant | loop | review

less

Transcript and Presenter's Notes

Title: Loop Invariant Review


1
CS100A Sections Dec 1-2 1998
  • Loop Invariant Review
  • C Review and Example
  • Recursion

2
  • Loop Invariant Review
  • A loop invariant is a true-false statement (about
    the vari-ables used in a loop) that is true
    before and after each iteration.
  • Given an invariant P, we can develop a loop with
    initialization in four steps
  • (1) Create initialization that makes P true.
  • (2) Determine the condition B so that from !B
    and P you can see that the desired result is
    true. (Or, equivalently, determine the condition
    B such that you know the desired result is not
    true.)
  • (3) Figure out what statement(s) to put in the
    loop body to make progress toward termination of
    the loop.
  • (4) Figure out what else the loop body should do
    to en-sure that after execution of the loop body
    invariant P is still true.
  • init
  • // invariant P S must make progress
  • while ( B ) toward termination
  • S and keep P true

3
Example of a loop developed from an
invariant Problem. Sorted array b0..n-1 may
contain duplicates. Rearrange b0..n-1 and store
in k so that b0..k-1 con-tains the
non-duplicates of b0..n-1. Invariant P (1)
Initialization Make first section empty item
using k 0 make second section contain 1 item
using i 0 (2) Stop when last section is empty,
i.e. when in. So, continue as long as i !
n. (3) Make progress using i i1. (4) Before
incrementing i if bi is not a duplicate, place
it in section b0..k-1. k 0 i 0 while ( i
! n) if (k 0 bk-1 ! bi
) bk bi k k1 i i1
looked at, but not changed
4
  • (A version of selection sort). Write an
    algorithm that, given
  • a lt b1, sorts ma..b. Use the invariant P
    given below. Do not write a loop within the body
    of the main loop instead, write commands in the
    body of the loop as high-level statements, in
    English.
  • P
  • j b1
  • // Invariant P (see the problem description)
  • while ( a lt j )
  • j j-1
  • Store in h the index of the maximum value
  • of ma..j
  • Swap mh and mj

a
j
b
m
lt sorted, gt
5
  • Each element bm (where 1ltmlt12) of array
    b0..12 contains the number of days in month m.
    (January is month 1, a 0 is in b0, etc.) Given
    a date (day, month) in two variables day and
    month, store in n the day of the year on which
    (day, month) occurs. For example, if January has
    31 days, then (5,2), i.e. day 5 of February,
    occurs on day 36 of the year, and if February has
    28 days, then (10,3) occurs on day 68 of the
    year.
  • Your algorithm will need a loop plus some other
    statements. For the loop, use the loop invariant
    P
  • P 1 lt m lt month and
  • n is the total number of days in months 1..m-1
  • m 1 n 0
  • // Invariant P (see the problem description)
  • while (m ! month)
  • n n bm m m1
  • n n day

6
  • Pointers and References in C
  • By default, C methods are call_by_value for
    integer args
  • void f (int x, int y)
  • int tmp
  • tmp x
  • xy
  • ytmp
  • void main (void)
  • int j10
  • int k20
  • f(j,k)
  • No change to j and k as a result of the call to
    f.

7
  • Pointers and References in C
  • What if you WANT f to modify j and k
  • j
  • k
  • j is a pointer to j j must be a variable
  • type of j int
  • type of j pointer to int, int
  • So if you see
  • int p1 p1 j
  • int p2 k
  • then p1 is 10 (follow the pointer to the value)
  • and p2 is 20.

8
  • Pointers and References in C
  • Rewrite f so that it modifies the arguments
  • void f (int x, int y)
  • int tmp
  • tmp x
  • xy
  • ytmp
  • void main (void)
  • int j10
  • int k20
  • f(j, k)
  • Now j and k change as a result of the call to f.

9
C Example
10
Calls to One
  • (a) One (a, c, b, d)
  • (b) One ((ab), c, b, d)
  • (c) One (a, c, b, d)
  • (d) One (a, d, b, d)
  • (e) One (b, b, b, d)
  • (a) Illegal. First and second arguments must be
    pointers to integer variables, not integer
    values.
  • (b) Illegal. The operator can only be applied
    to a variable, not an expression.
  • (c)
  • 20 2 1 10.0
  • 1 2 20 8.0
  • (d) Second parameter must be a pointer to an
    integer. d is a pointer to a double.
  • (e)
  • 20 2 20 10.0
  • 1 20 7 8.0

11
Recursion Example
  • Write a function prod that computes the product
    of integers between lo and hi inclusive. (Assume
    that lo lt hi.)
  • Header
  • public static int product (int lo, int hi)
  • Example
  • lo 3 hi 5 3 x 4 x 5 60
  • Algorithm
  • base case if lo hi, return lo
  • otherwise (go from lo to hi or vice versa)
  • return ( lo product
    (lo1, hi))
  • Code
  • //computes the product of integers between lo and
    hi
  • //inclusive. (Assume that lo lt hi.)
  • public static int prod (int lo, int hi)
  • if (lo hi)
  • return lo
Write a Comment
User Comments (0)
About PowerShow.com