Transcript ppt

CS100A Sections Dec 1-2 1998
• Loop Invariant Review
• C Review and Example
• Recursion
1
Loop Invariant Review
A loop invariant is a true-false statement (about the variables 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 ensure that after execution of the loop body invariant P is
still true.
init;
// invariant P: …
while ( B )
{S}
S must make progress
toward termination
and keep P true
2
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] contains the non-duplicates of b[0..n-1]. Invariant P:
0
k
i
n
Sorted. Contains all dis- looked at, these values
b tinct values of b[0..i-1], but not
haven’t been
and has no duplicates
changed
looked at
(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;
}
3
(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:
a
m
j
<=
b
sorted, >=
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];
}
4
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;
5
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.
6
Pointers and References in C
•What if you WANT f to modify j and k…
•j
10
•k
20
&j is a pointer to j; j must be a variable
type of j:
type of &j:
int
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.
7
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.
8
C Example
Give the output produced by the following program that has been
written in C.
#include <stdio.h>
int a, b, c;
double d;
void One (int *r, int *a, int b, double s) {
*a = 20;
s = s + b;
printf (“%d %d %d %.1f\n”, *a, b, *r, s);
void main (void) {
a = 1;
b = 2;
c = 7;
d = 8.0;
<call to One>
printf ("%d %d \n ", a, b, c, d );
}
9
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)
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
10
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 = 3 hi = 5
Algorithm:
base case:
otherwise:
3 x 4 x 5 = 60
if lo= hi,
return lo
(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));
}
11