SOLUTIONS: Midterm, CS161, Fall 2013
1. For the following problem, the set cfw_p, p + 1, p + 2, . . . , q is the set of integers from p
through q . For example, if p = 5 and q = 8, this set is cfw_5, 6, 7, 8. If p = 5 and q = 5,
this set is cfw_5. If q <
Recursion - examples
Problem: given a string as input, write it
Suppose youre looking up a word in the
dictionary (paper one, not online!)
1 + 2 + + n = n(n + 1)/2.
Rosen Chapter 5
Suppose we know: P(1) n 1, P(n) P(n + 1).
2. P(1) P(2).
[specialization of premise]
[step 1, 2, & modus pon
Counting: the product rule
Rosen, Chapter 6.1
If there are n1 ways of doing one task, and for
each of way of doing the first task there are n2
ways of doing a second task, then there are
n1n2 ways of performing both tasks.
Programming as a contract
Assertions, pre/postconditions and invariants
Specifying what each method does
Assertions: Section 4.2 in Savitch (p. 239)
Loop invariants: Section 4.5 in Rosen
Specify it in a comment before method's header
Using arrays to store data
Arrays: store multiple values of the same type.
Conveniently refer to items by their index
q Need to know the size before declaring them:
int numbers = new int;
n We often need to store an unknown number of
Objects, classes, and object-oriented
Anatomy of a class
Savitch, ch 5
relationship between classes and objects
Objects and classes
Permutations and Combinations
In a family of 5, how many ways can we
arrange the members of the family in a line
for a photograph?
Rosen, Chapter 5.3
A permutation of a set of distinct objects is an
The software crisis
software engineering: The practice of conceptualizing,
designing, developing, documenting, and testing largescale computer programs.
Large-scale projects face many issues:
You have been
/ Java's built in Math class
public class Math cfw_
public static int abs(int a) cfw_
if (a >= 0) cfw_
public static double toDegrees(double radians) cfw_
return radians * 180 / PI;
Easier to search (binary search)
Sorting used as a step in many algorithms
Savitch Chapter 7.4
There are many algorithms for sorting:
Arrays are not always the optimal data
Chapter 12.3 in Savitch
Objects and references
Object variables do not actually store an object; they
store the address of an object's location in the
Relatedness of types
Consider the task of writing classes to represent
2D shapes such as Circle, Rectangle, and
There are certain attributes or operations that
are common to all shapes:
Savitch ch. 8.4
Quiz HW10, CS161, Fall 2013, McConnell
1. What is P (100, 5)/C (100, 5)? Give an exact numerical answer.
5! = 120. For every way to select a 5-subset, there are 5! ways to select a 5permutation.
2. What is the probability of getting exactly two heads when
SOLUTIONS: Homework 7, CS161, Fall 2013, McConnell
1. Prove that the number of moves of the straight-line version is 3n 1. This is the one
that you proved was possible to solve on the last assignment, where must move the
disks from peg 1 to peg three or f
SOLUTIONS: Homework 6, CS161, Fall 2013, McConnell
1. Show by induction that n ii! = (n+1)!1 for n 0. Hints: x+(k +1)x = (k +2)x;
(k + 2)(k + 1)! = (k + 2)!.
Solution: There are various ways to express it. Here is one way.
Base Case: For k = 0, the su
Study Guide, Counting
There will be no homework due on Monday. Instead, we will have a quiz on 11/20, which will
count both as a homework quiz and as a reading/lecture quiz, and which will be lengthier
than past homework and reading quizzes.
You should ma
About this course
CS 161: Object Oriented
About this course
RamCT will be used for: forums, grades
Why is math important to us? What does that
have to do with computer science?