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 <
2/17/13
Recursion - examples
n
Problem: given a string as input, write it
backward
n
Recursion continued
Base case?
Recursion
n
Dictionary lookup
n
n
n
Binary search
Suppose youre looking up a word in the
dictionary (paper one, not online!)
You probably
2/24/13
Motivation
Mathematical Induction
n
1 + 2 + + n = n(n + 1)/2.
n
Rosen Chapter 5
n
Proving P(3)
n
Suppose we know: P(1) n 1, P(n) P(n + 1).
n
n
Proof:
n
1. P(1).
[premise]
2. P(1) P(2).
[specialization of premise]
3. P(2).
[step 1, 2, & modus pon
3/3/13
Counting: the product rule
Counting: Basics
Rosen, Chapter 6.1
n
n
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.
Ex
2/14/13
Programming as a contract
Assertions, pre/postconditions and invariants
n
Specifying what each method does
q
n
q
q
Assertions: Section 4.2 in Savitch (p. 239)
Loop invariants: Section 4.5 in Rosen
n
Specify it in a comment before method's header
P
2/4/13
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[100];
n We often need to store an unknown number of
value
2/4/13
Outline
n
Java classes
Objects, classes, and object-oriented
programming
q
q
n
Anatomy of a class
q
Savitch, ch 5
relationship between classes and objects
abstraction
q
q
instance variables
instance methods
constructors
2
Objects and classes
n
ob
3/9/13
Permutations
Permutations and Combinations
n
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
Permutations
n
A permutation of a set of distinct objects is an
ordered arrangeme
3/23/13
The software crisis
Inheritance
n
n
software engineering: The practice of conceptualizing,
designing, developing, documenting, and testing largescale computer programs.
Large-scale projects face many issues:
q
q
q
q
q
n
Example
n
n
You have been
4/27/13
Static methods
/ Example:
/ Java's built in Math class
public class Math cfw_
public static int abs(int a) cfw_
if (a >= 0) cfw_
return a;
else cfw_
return -a;
public static double toDegrees(double radians) cfw_
return radians * 180 / PI;
S
4/21/13
Why sort
Sorting
n
n
Easier to search (binary search)
Sorting used as a step in many algorithms
Savitch Chapter 7.4
Sorting algorithms
n
There are many algorithms for sorting:
q
q
q
q
q
q
q
q
n
Selection sort
Insertion sort
Bubble sort
Merge sor
4/13/13
Preliminaries
n
Linked Lists
Arrays are not always the optimal data
structure:
q
q
Chapter 12.3 in Savitch
n
Objects and references
Object variables do not actually store an object; they
store the address of an object's location in the
computer'
3/29/13
Relatedness of types
Interfaces
n
n
Consider the task of writing classes to represent
2D shapes such as Circle, Rectangle, and
Triangle.
There are certain attributes or operations that
are common to all shapes:
perimeter, area
Savitch ch. 8.4
In
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;
i=1
(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
Problem Solving
n
q
n
About this course
n
Course webpage:
http:/www.cs.colostate.edu/~cs161/
q
RamCT will be used for: forums, grades
q
Why is math important to us? What does that
have to do with computer science?