PIC 10B SPRING 2014 HOMEWORK 5
DUE FRIDAY 5/2/14 AT 5:00PM.
Mathematical Background
An integer partition of size n is a set of positive integers that sum up to n. For example,
the integer partitions of 5 are cfw_5, cfw_4, 1, cfw_3, 2, cfw_3, 1, 1, cfw_2,

Welcome to PIC 10A
Thats right, please feel free to leave
now if you are in the wrong roomgo
ahead, no need to sit here for an hour
based on a silly mistake.
2013
Stephen DeSalvo, UCLA
1
Enrollment note
If you are not already enrolled in the course, you

PIC 10B EXAM 1
April 23, 2007
NAME: _KEY_
PLEASE PRINT
Check your TA's name:
Hengli (9:00) _
Faizal (10:00) _
You have 50 minutes to complete this exam. No books, notes, or calculators are allowed. Show all work. Incomplete, illegible, or uninte

PIC 10B EXAM 2
March 1, 2010
KEY
Check your TA's name:
Rachel_
Josh _
David_
You have 45 minutes to complete this exam. No books, notes, or calculators are allowed.
Show all work. Incomplete, illegible, or unintelligible answers may receive no credit.
The

Lecture 8: Template Classes
PIC 10B Todd Wittman
Sec 22.3: Template Classes
We can use templates to design general-purpose classes. We've actually already seen this in action with the vector class. vector<double> v(10); v[3]=-42.3; vector<string> s(

PIC 10B EXAM 2
May 19, 2008
KEY
Check your TA's name:
Alvin_
Judah _
You have 50 minutes to complete this exam. No books, notes, or calculators are allowed.
Show all work. Incomplete, illegible, or unintelligible answers may receive no credit.
There are a

PIC 10B
Practice Final Exam KEY
1.) TRUE or FALSE. If the answer is false, briefly explain why the statement is false.
a.) Every key in a hash table must be unique.
TRUE
b.) Every key in a hash table must map to a unique hash index.
FALSE. This is how we

Abstract Classes and Friends (Lesson 3)
What is an abstract class?
An abstract class is a class that cannot be directly instantiated.
Any class that declares a pure virtual member function is automatically abstract.
Any derived class that fails to define

PIC 10B
Lecture 19:
Tree Traversal
Traversing a tree
The process of systematically visiting
every node exactly once in a tree is called
a tree traversal.
There are several ways to traverse a tree.
Certain tree traversals lend themselves
naturally to ap

Memory Management (Lesson 8)
Copy Constructor
A copy constructor is a constructor that takes as an argument a reference to an object of the
same class.
Its purpose is to create an exact copy or clone of the object argument.
The copy constructor is invo

Operating Overloads (Lesson 5)
What is an operator?
An operator is a special function typically mathematical or logical in nature that is denoted
using symbols or reserved words.
An operators arguments are called operands.
The action carried out by an

Inheritance (Lesson 1)
What is inheritance?
Inheritance is the ability to derive a new class by extending existing ones.
The derived class (aka the subclass) is an extension of its base classes (aka superclasses).
The derived class automatically inherits

PIC 10B
Lecture 18:
Trees
What is a Tree?
A tree is a data structure that consists of a
collection of nodes and a set of directed
edges that connect pairs of nodes.
A directed edge denotes a parent-child
relationship where the parent node
points to its ch

PIC 10B
Lecture 17:
Queues and Stacks
What is a Queue?
A queue is a data structure which
organizes data items in a linear fashion
adds a new item to the end of the line
removes an item from the beginning of the
line
We say a queue is a container that s

PIC 10B
Lecture 23:
Heaps
What is a heap?
A heap is a binary tree that satisfies two
properties:
1. It is complete: Every level of the tree,
except possibly the last, is completely filled
with nodes, and all nodes on the last level
are as far left as poss

PIC 10B
Lecture 20:
Sets and Multisets
What is a Set?
A set is a data structure that consists of a
(not necessarily ordered) collection of
distinct elements.
It has the following behaviors:
Adding an element
Finding an element
Removing an element
Trav

PIC 10B
Lecture 21:
Maps and Multimaps
What is a Map?
A map is a data structure that keeps
associations between elements of one type
called keys and elements of another type
called values.
In a map,
every key is associated to a unique value.
a value may

PIC 10B
Lecture 16:
Templates
Motivating Example
Suppose I define a function that swaps the
values of two int variables.
void swapper(int& a, int& b)cfw_
int temp = a;
a = b;
b = temp;
Motivating Example
T

PIC 10B
Lecture 14:
Introduction to Data Structures:
Linked Lists
What is a data structure?
A data structure is a way of storing and
organizing data so that it may be used
efficiently
Certain data structures are better for
implementing a particular applic

PIC 10B
Lecture 15:
STL Linked Lists
Linked List Pros and Cons
Linked List Pros
efficient insertion and removal algorithms
Both are O(1) (constant time) operations if
we already have Iterators at the the proper
Nodes
Linked List Cons
inefficient rando

Polymorphism (Lesson 2)
What is Polymorphism?
the ability to treat different objects related by a common base class uniformly.
It can be achieved in C+ by
1.overriding a virtual function in a derived class
2.creating a polymorphic collection of objects
3

Recursion, Backtracking (Lesson 9)
What is recursion?
Recursion is a process that solves a problem by solving smaller problems of exactly the
same type as the original problem
The smallest easiest versions of the problem are called the base cases.
Any oth

Searching and Algorithm Analysis (Lesson 10)
What is an algorithm?
An algorithm is a step-by-step procedure for solving a problem in a finite number of steps.
Discovering an algorithm is important since we can program a computer to execute it.
Some proble

All about Functions
return;
of the Jedi : Person bool
Different types of functions
2013
Stephen DeSalvo, UCLA
1
Recall basic function notation
If we have a function f(x) = sin(x) with domain R
and codomain R, we can write the input/output
structure as f

Functions
GreetStudents();
PresentLectureSlides();
answer = QuestionFromStudent(question);
SpeakTheTruth(answer);
2013
Stephen DeSalvo, UCLA
1
Definition of function
According to the Wikipedia Function
(mathematics) page
In mathematics, a function is a

Randomness
srand(time(0);
rand()%(b-a)+a
RAND_MAX
2013
Stephen DeSalvo, UCLA
1
A little random . . .
Random numbers are very useful in
programs because they can help you test
out your program by generating lots of
different types of input.
A basic rando

The do-while loop, input, and
some practical considerations
do, or do notthere is no try Yoda
while(cin>n) cfw_
cin.fail()
2013
Stephen DeSalvo, UCLA
1
do while, do do do while
Sometimes you want to run the contents of a loop first,
and then test for a

Overview of Lecture
1
Mutual Recursion
2
Parity Testing
3
Evaluating Arithmetic Expressions
4
Questions
PIC 10B
Recursion, Part II
Monday, April 25
1/8
Mutual Recursion
Previously we saw examples of recursive functionsfunctions which
call themselves.
We c

Final Exam Solutions
PIC 10B, Spring 2016
Problem 1. (10 pts) Consider the Fraction class, whose partial declaration was given by
1 class Fraction cfw_
2 public :
3
Fraction ( int num , int den );
4
.
5
int num ; / numerator of the Fraction
6
int den ; /

Templates and Iterators
Ali Malik
[email protected]
Game Plan
Recap
Iterator Wrapup
auto and Range Based for
Templates
Iterators (pt. II)
Recap
Associative Containers
Useful abstraction for associating a key with a value.
std:map
map<string, int> dire

Overview of Lecture
1
Overloading I/O Operators
2
Overloading < and > for Fractions
3
Formatted vs Unformatted Input
4
Setting the State of a Stream
5
Questions
PIC 10B
Streams, Part II
April 20, 2016
1 / 10
The Operators < and >
What does the code
int n