Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100A Sections Dec Loop Invariant Review C Review and Example

Similar presentations


Presentation on theme: "CS100A Sections Dec Loop Invariant Review C Review and Example"— Presentation transcript:

1 CS100A Sections Dec 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 b[0..n-1] may contain duplicates. Rearrange b[0..n-1] and store in k so that b[0..k-1] con-tains the non-duplicates of b[0..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 i=n. So, continue as long as i != n. (3) Make progress using i= i+1. (4) Before incrementing i: if b[i] is not a duplicate, place it in section b[0..k-1]. k= 0; i= 0; while ( i != n) { if (k == 0 || b[k-1] != b[i] ) {b[k]= b[i]; k= k+1;} i= i+1; } Sorted. Contains all dis- tinct values of b[0..i-1], and has no duplicates these values haven’t been looked at k i n b looked at, but not changed

4 a j b m <= sorted, >=
(A version of selection sort). Write an algorithm that, given a <= b+1, sorts m[a..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= b+1; // Invariant P (see the problem description) while ( a < j ) { j= j-1; Store in h the index of the maximum value of m[a..j]; Swap m[h] and m[j]; } a j b m <= sorted, >=

5 Each element b[m] (where 1<=m<=12) of array b[0
Each element b[m] (where 1<=m<=12) of array b[0..12] contains the number of days in month m. (January is month 1, a 0 is in b[0], 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 <= m <= 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 + b[m]; m= m+1; } 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; x=y; y=tmp; } void main (void) { int j=10; int k=20; f(j,k); No change to j and k as a result of the call to f.

7 10 20 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. 10 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; *x=*y; *y=tmp; } void main (void) { int j=10; int k=20; 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 (&(a+b), &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) (d) Second parameter must be a pointer to an integer. &d is a pointer to a double. (e)

11 Recursion Example Write a function prod that computes the product of integers between lo and hi inclusive. (Assume that lo <= hi.) Header: public static int product (int lo, int hi) Example: lo = 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 (lo+1, hi)) Code: //computes the product of integers between lo and hi //inclusive. (Assume that lo <= hi.) public static int prod (int lo, int hi) { if (lo == hi) return lo; else return (lo * product (lo+1, hi)); }


Download ppt "CS100A Sections Dec Loop Invariant Review C Review and Example"

Similar presentations


Ads by Google