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 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
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

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(

cin, cout, and buffers
Yeah a buffer. The family had a
lot of buffers.
- Willi Cicci, The Godfather Part II
2013
Stephen DeSalvo, UCLA
1
Simple input
So far you have only been required to input
strings and integers.
You can also input decimal numbers, s

Control Flow
Control, control, you must learn
control! - Yoda
2013
Stephen DeSalvo, UCLA
1
if-else
Many times in the design of a program, you want
a different set of code to run depending on the
current state of a variable.
For example, suppose we wish

Boolean Operations & Loops
if(x >= 0 & x <= 5) cout<x;
while for loops are running
2013
Stephen DeSalvo, UCLA
1
if(n=5)
We saw last time how to run code depending on the state of an internal variable.
For example,
if(n=5)
cout<n is 5.\n;
else
cout<n is no

The for loop
for(int i=0;i<10;i+)
cout<i<endl;
2013
Stephen DeSalvo, UCLA
1
while(I have your attention)
We are now familiar with if-else statements, the
idea of choosing to run one set of code or
another.
Last time we introduced while loops, which allo

Type casting, cmath, and
arithmetic
int x = 5.678;
double r = sqrt( pow(a,2) + pow(b,2) );
double y = a*a+b*b;
2013
Stephen DeSalvo, UCLA
1
Type Casting
What happens when you try to assign a value like 5.678
to a variable declared int?
int x = 5.678;
Th

All about Numbers
You must learn the ways of
numbers if youre to come with me
to Alderaan.
2013
Stephen DeSalvo, UCLA
1
Floating point numbers
Recall we have data types like int, double, char, bool,
etc.
int and double are our basic numeric types.
A da

Arithmetic and Strings
4+5 vs 4.5+5 vs 3.4+5.6
And a very special class
2013
Stephen DeSalvo, UCLA
1
Watch out!
Arithmetic in C+ can be treacherous!
Once you understand the basics it is quite intuitive, but
there are a couple key points to understand be

Data Types
int, bool, char, etc.
2013
Stephen DeSalvo, UCLA
1
Identifiers
Identifiers a defined name that
represents an element of a program, i.e.,
variable name.
For example, suppose I am thinking of a
number, and I add 5 to it, and then I
multiply tha

The compilers process
We need to think like a compiler
2013
Stephen DeSalvo, UCLA
1
Hello World!
#include <iostream>
using namespace std;
int main() cfw_
cout<Hello World!\n;
return 0;
The computer is going to read instructions starting in the
function

The computing perspective
Welcome to the exciting world of
computing.
2013
Stephen DeSalvo, UCLA
1
Syllabus items not mentioned
There will be 8 homework assignments total this
quarter. Each are worth 20 points.
I will drop the lowest homework score.
Mi

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

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

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

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

Function pointers
function pointers, typedef
double b = applyFunction(v, sqrt);
vector/string member functions review
2013
Stephen DeSalvo, UCLA
1
vector functions
size, push_back, pop_back, resize, assign,
clear, [].
4 different ways to construct a vec

Arrays
int a[] = cfw_2,3,5,7,11;
Array/Pointer duality
2013
Stephen DeSalvo, UCLA
1
A long time ago, in a computer far far away
2013
Stephen DeSalvo, UCLA
2
Arrays
A long time ago, in a computer far far away
int a[5]; / Create an array of 5 ints, garbag

Stack and Heap
new/delete
int* a = new int[10];
delete[] a;
2013
Stephen DeSalvo, UCLA
1
Stack vs Heap
At first, we were able to create variables,
without knowing or caring much about where
in memory they were located.
Then with pointers, we wanted to b

Pointers
In C+, it is polite to point
2013
Stephen DeSalvo, UCLA
1
Address ignorance
Up until now, we have been completely ignorant
of where our objects and variables have been
stored in the computer.
In fact, it really doesnt matter precisely where
the

Class by example
Aspects of classes by example
2013
Stephen DeSalvo, UCLA
1
Parallel Vectors
index
name
ID
grade
0
John Smith
314159
B-
1
Luke Skywalker
271828
B
2
Yoda
999999
A+
3
Darth Vader
000616
D
4
TK-421
123456
C
Keeping a valid set of parallel ve

The subtle aspects of classes
overloading
default constructor automatic?
copy constructor, = operator
Initializer List
Line2D class
2013
Stephen DeSalvo, UCLA
1
Recall Point2D
/* @class Point2D
@brief A class for 2D points (x,y)
This class stores and mani

Classes
Point2D pt(1,2);
pt.move(3.3,4.5);
pt.print();
2013
Stephen DeSalvo, UCLA
1
Parallel Vectors
Suppose I wanted to store a vector of student
names, ID numbers, and a final grade.
vector<string> name;
vector<int> ID;
vector<string> grade;
We set up

vector member functions
v.size(); v.resize(); v.clear();
v.push_back(); v.pop_back();
v.assign()
2013
Stephen DeSalvo, UCLA
1
First, a note about a potential bug
While preparing the code from last lecture, I
got stuck for a few minutes trying to figure o

How to redirect input/output
in MS Visual Studio
By: Michael Puthawala, mputhawala@g.ucla.edu
This is what I get without any redirection. Entering input is tedious
and error prone if I run the program more than a few times!
In order to redirect input you

Data structures:
Stacks & Queues
Lesson 8
PIC 10B, Ricardo Salazar
The concept of a Stack
A stack is a data structure that follows 2 rules:
1. Data can only be accessed by removing the top:
pop()
2. A new data element can only be added to the top of the

Pic 10B
Fall 2016
Midterm 1
Intermediate programming
October 21, 2016
Directions:
You have 50 minutes to complete this exam.
You are not allowed to use any books, notes, calculators, or electronic devices.
Answer the questions in the spaces provided. If y

Pic10B
Fall2015
Midterm1
Intermediateprogramming
October22,2015
Directions:
Youhave90minutestocompletethisexam.
Youarenotallowedtouseanybooks,notes,calculators,orelectronicdevices.
Answer the questions in the spaces provided. If you run out of room us

Data
structures:
Binary Search Trees
Lesson 9
PIC 10B, Ricardo Salazar
Note: BST's are not discussed in detail in our textbook.
Binary Search Trees (BST's)
A BST is an ordered collection of items.
They are organized into nodes which have two
pointers:
4