CS136 Fall 2014 - Tutorial 5 CS136 ISAs: 15th of October, 2014 0-0

CS136 Fall 2014 - Tutorial 5
CS136 ISAs: [email protected]
15th of October, 2014
0-0
Goals of this Tutorial
The goal of this tutorial is to reinforce the following material:
• Conditions
• Mutation
• Post Conditions
• Passing Pointers by Value
CS 136 Fall 2014
Tutorial 5
1
If Statements
The C “if statement” is another control flow statement.
The syntax is
if (expression) statement
where the statement is only executed if the expression is true.
if (n < 0) printf("n is less than zero\n");
CS 136 Fall 2014
Tutorial 5
2
Problem: Collatz Conjecture
// collatz( n ) returns the length of the collatz
//
sequence of n that ends with 1
// PRE: n >= 1
// POST: returns the length of the collatz sequence
//
starting at the number n
int collatz( int n );
ni+1 =


ni
2
 3ni + 1
: ni ≡ 0
mod 2
: ni ≡ 1
mod 2
Example collatz(5) is 6: (5, 16, 8, 4, 2, 1)
CS 136 Fall 2014
Tutorial 5
3
CQ 1:
Which of the following is an example of why we use if instead of
the ternary operator [?]
A if statements are written on multiple lines
B Multiple line support in if statements
C Easier to handle 3+ scenarios
D all of the above
CS 136 Fall 2014
Tutorial 5
3
Mutation: Racket
Variables allow us to change information and with global variables
allow persistence.
In Racket a variable is changed using set!.
Consider the example below
(define called #f)
(define (first_time_called)
(cond [called #f]
[else (set! called #t) #t]))
CS 136 Fall 2014
Tutorial 5
4
Mutation: C
Variables allow us to change information and with global variables
allow persistence.
In C the value can be reassigned.
Consider the example below
bool called = false;
bool first_time_called(void)
{
if( called ) return false;
called = true;
return true;
}
CS 136 Fall 2014
Tutorial 5
5
Mutation: Post Conditions
Post conditions focus on the side effects from mutation and are
explained in natural language.
For first_time_called the side effect is related to the return
value. Here is a sample of the design recipe.
// first_time_called(void) returns whether this is the
//
first time the function is
//
called
// PRE: true
// POST: Returns true if it is the first time called
//
all other calls will be false
CS 136 Fall 2014
Tutorial 5
6
Problem: C Safe
Consider a safe where you can lock and unlock with a key. Any key
that locks that safe must be used to unlock it. While the safe is
locked it can’t be locked with another key.
// lock_safe( k ) locks the safe with the key k
//
if the safe is unlocked
// PRE: true
// POST: If the safe was unlocked, it is now locked
//
returns true if succesffully locked
bool lock_safe( int k );
CS 136 Fall 2014
Tutorial 5
7
Problem: C Safe
// unlock_safe( int k ) attempts to unlock the safe with k
// PRE: True
// POST: Unlocks the safe if the safe is locked with k
//
returns whether the safe is unlocked at the end
bool unlock_safe( int k );
// is_locked_safe() checks if the safe is locked
// PRE: True
// POST: Returns true if the safe is locked
bool is_locked_safe();
CS 136 Fall 2014
Tutorial 5
8
Problem: Racket Safe
Consider a safe where you can lock and unlock with a key. Any key
that locks that safe must be used to unlock it. While the safe is
locked it can’t be locked with another key.
;; lock_safe( k ) locks the safe with the key k
;;
if the safe is unlocked
;; PRE: To lock, the safe must be unlocked
;; POST: If the safe was unlocked, it is now locked
;;
returns true if succesffully locked
(define (lock_safe k ) ...)
CS 136 Fall 2014
Tutorial 5
9
Problem: Racket Safe
;; unlock_safe( k ) attempts to unlock the safe with key k
;; PRE: True
;; POST: Unlocks the safe if the safe is locked with k
;;
returns whether the safe is unlocked at the end
(define (unlock_safe k) ...)
;; is_locked_safe() checks if the safe is locked
;; PRE: True
;; POST: Returns true if the safe is locked
(define (is_locked_safe) ...)
CS 136 Fall 2014
Tutorial 5
10
Loop Example
Sum the first n numbers with a while loop where n is non-negative.
int sum_first_n(int n) {
assert(n >= 0);
int rtn = 0;
while(n >= 0){
rtn += n;
n -= 1;
}
return rtn;
}
CS 136 Fall 2014
Tutorial 5
11
Loop Example
Sum the first n numbers with a for loop where n is non-negative.
int sum_first_n(int n) {
assert(n >= 0);
int rtn = 0;
for(;n >= 0; n -= 1){
rtn += n;
}
return rtn;
}
CS 136 Fall 2014
Tutorial 5
12
Passing Pointers by Value
By passing the address of x, we can change the value of x.
It is also common to say “pass a pointer to x”.
void inc(int *p) {
*p += 1;
}
To pass the address of x use the address operator (&x).
The corresponding parameter type is an int pointer (int *).
CS 136 Fall 2014
Tutorial 5
13
Passing Pointers by Value: Example
// assign_if_greater( int *a, int *b ) assigns the value
//
of b to a if b is greater than a
// PRE: True
// POST: The value of b is assigned to a
//
if b is greater than a
void assign_if_greater( int *a, int *b );
CS 136 Fall 2014
Tutorial 5
14
Passing Pointers by Value: Example
// assign_if_greater( int *a, int *b ) assigns the value
//
of b to a if b is greater than a
// PRE: True
// POST: The value of b is assigned to a
//
if b is greater than a
void assign_if_greater( int *a, int *b )
{
if( *a < *b ) {
= *b;
*a
}
}
CS 136 Fall 2014
Tutorial 5
15
CQ 2:
Given the code below, which call of assign_if_greater is valid
void assign_if_greater( int *a, int *b );
void main()
{
int a = ...; int b = ...;
...
// line goes here
...
}
A
B
C
D
E
assign_if_greater(
assign_if_greater(
assign_if_greater(
assign_if_greater(
assign_if_greater(
CS 136 Fall 2014
a, b );
&a, &b );
*a, *b );
^a, ^b );
@a, @b );
Tutorial 5
15
Problem: Safe 2.0
Supposed we want our safe module to support locking and
unlocking the safe structure shown below.
struct safe{
bool is_locked;
int key;
};
How could we change this module?
CS 136 Fall 2014
Tutorial 5
16
Problem: Safe 2.0
// make_safe( struct safe *s) initializes a safe s
//
// PRE: A valid safe reference
// POST: Creates a new unlocked safe from a safe reference
void make_safe(struct safe *s);
// lock_safe( struct safe *s, int k ) locks the safe s
//
with k if the safe is unlocked
// PRE: To lock, the safe must be unlocked
// POST: If s was unlocked, it is now locked with key k
//
returns true if succesfully locked
bool lock_safe( struct safe *s, int k );
CS 136 Fall 2014
Tutorial 5
17
Problem: Safe 2.0
// unlock_safe( struct safe *s, int k ) attempts to
//
unlock the safe s with the key k
// PRE: True
// POST: Unlocks the safe if the key matches
//
returns true if successfully unlocke
bool unlock_safe(struct safe *s, int k);
// is_locked_safe( struct safe *s) returns true if
//
the safe is locked and false otherwise
// PRE: True
// POST: Returns true if the safe is locked
bool is_locked_safe(struct safe *s);
CS 136 Fall 2014
Tutorial 5
18