Control and environment diagrams
Draw environment diagrams for function calls, trace conditionals and loops, and write loop-based functions on the digits of a number.
- Lessons
- Names and functions · Control
This discussion goes with Lessons 2 and 3. Environment diagrams come first: they are the tool CS61A uses for every question about what a name means. The rules are in Lesson 5; for now, one frame per function call is enough.
Q1: Draw the frames
Draw the environment diagram for this program at the moment it prints, and say what it prints.
func square(_ x: Int) -> Int {
x * x
}
func sumOfSquares(_ a: Int, _ b: Int) -> Int {
let x = square(a)
return x + square(b)
}
let x = 2
print(sumOfSquares(x, x + 1))
Show solution
It prints 13.
Global frame
square │ func square(_:) [parent = Global]
sumOfSquares │ func sumOfSquares(_:_:) [parent = Global]
x │ 2
f1: sumOfSquares [parent = Global]
a │ 2
b │ 3
x │ 4
Return │ 13
f2: square [parent = Global]
x │ 2
Return │ 4
f3: square [parent = Global]
x │ 3
Return │ 9There are three different names x, in three frames: the global x (2),
the local x in f1 (4), and each call’s parameter x. None of them affects
the others. The argument x + 1 is evaluated in the global frame, before
f1 exists.
Q2: What would Swift print?
What would Swift print?
func check(_ n: Int) -> String {
if n > 10 {
return "big"
} else if n > 5 {
return "medium"
}
return "small"
}
print(check(10), check(11), check(5))Show answer and explanation
Answermedium big small
10 is not greater than 10, but it is greater than 5. 11 is big. 5 is not
greater than 5, so it falls through to the final return.
What would Swift print?
var n = 12
var steps = 0
while n > 1 {
n = n / 2
steps += 1
}
print(n, steps)Show answer and explanation
Answer1 3
12 becomes 6, then 3, then 1 (integer division), which ends the loop after 3 steps.
What would Swift print?
var total = 0
for i in 1...4 {
total = total * 10 + i
}
print(total)Show answer and explanation
Answer1234
Each pass shifts the total one decimal place left and appends i: 1, 12,
123, 1234.
Q3: Is it prime?
Write isPrime(_:) with a while loop. A number is prime if it is greater
than 1 and has no divisor other than 1 and itself. You only need to test
divisors k with k × k ≤ n.
func isPrime(_ n: Int) -> Bool {
// your code here
}
Show solution
func isPrime(_ n: Int) -> Bool {
if n < 2 {
return false
}
var k = 2
while k * k <= n {
if n % k == 0 {
return false
}
k += 1
}
return true
}
assert(!isPrime(10))
assert(isPrime(7))
assert(!isPrime(1))
assert(isPrime(97))If n had a divisor larger than √n, it would pair with one smaller than
√n, which the loop would already have found.
Q4: Unique digits
Write uniqueDigits(_:), the number of different digits in a non-negative
integer. uniqueDigits(8675309) is 7 and uniqueDigits(1313131) is 2.
Hint: first write hasDigit(_:_:), which checks whether a number contains a
given digit, and then count the digits 0 through 9 it contains.
func hasDigit(_ n: Int, _ digit: Int) -> Bool {
// your code here
}
func uniqueDigits(_ n: Int) -> Int {
// your code here
}
Show solution
func hasDigit(_ n: Int, _ digit: Int) -> Bool {
var rest = n
repeat {
if rest % 10 == digit {
return true
}
rest /= 10
} while rest > 0
return false
}
func uniqueDigits(_ n: Int) -> Int {
var count = 0
for digit in 0...9 where hasDigit(n, digit) {
count += 1
}
return count
}
assert(uniqueDigits(8675309) == 7)
assert(uniqueDigits(1313131) == 2)
assert(uniqueDigits(13173131) == 3)
assert(uniqueDigits(10000) == 2)
assert(uniqueDigits(101) == 2)
assert(uniqueDigits(0) == 1)Splitting the problem in two keeps each function simple: hasDigit walks
the digits of one number, and uniqueDigits asks ten yes-or-no questions.
repeat-while makes sure the single digit of 0 is checked.
Q5: Find the bug
This function should return how many times the digit 7 appears in n, but
countSevens(707) returns 1. Find the mistake without running it, and fix
it.
func countSevens(_ n: Int) -> Int {
var rest = n
var count = 0
while rest > 10 {
if rest % 10 == 7 {
count += 1
}
rest /= 10
}
return count
}
print(countSevens(707))
Show solution
The loop stops when rest is 10 or less, so the leading digit is never
checked once it is alone: for 707, the last pass leaves rest at 7, and
7 > 10 is false. The condition should be rest > 0.
func countSevens(_ n: Int) -> Int {
var rest = n
var count = 0
while rest > 0 {
if rest % 10 == 7 {
count += 1
}
rest /= 10
}
return count
}
assert(countSevens(707) == 2)
assert(countSevens(10) == 0)
assert(countSevens(7) == 1)Boundary conditions are where loops go wrong. Checking the smallest inputs, a one-digit number here, finds this kind of bug fastest.