Below are my notes from CS 31 during the Fall 2013 quarter at UCLA. This is the first CS class at UCLA, teaching basic object-oriented programming with C++. I hope that current CS 31 students find them useful, as well as students at other schools using C++, or those who would like to see the C++ equivalent of what they are taught in Java, Python, and so on.

Donations are welcome via Venomo or Square Cash, and help support me in offering ongoing mentorship.

I also highly recommend the notes and practice problems by Andrew Forney, one of the best CS TAs at UCLA.

Open a pull request if you find any errors, and I’ll gladly correct them. To read the notes offline, download this page’s Markdown version and open it in Sublime Text, with the MarkdownEditing package installed.

Happy studying!

## Lecture 1: 27 September 2013

### Introduction

Professor Smallberg is making it clear that this is a C++ programming course, not a broad overview survey course of CS in general. That said, we’re apparently beginning with some sort of historical flashback to help us with problem-solving and trouble-shooting.

### History

Reprogrammability of historical mechanical trinkets and machines were obviously very difficult to reconfigure for different purposes.

Relevant development: textile manufacturing in the early 1800s with a loom that allowed for fancier patterns. It was controlled by some hooks (that picked up threads to let a shuttle through) blocked/not blocked by boards with holes in them that could be moved to change the pattern.

** Jacquard designed that.

This is programmability: no need to significantly change the machine to change its behavior.

Interestingly, some evidence that da Vinci may have designed a programmable floor bot that could move about.

** Now, we have Charles Babbage, and his Difference Engine, finally, used for computing values, useful for functions like the trig ones and logs. It was never finished though, due to his personality, engineering difficulties, and eventual loss of funding. That said, the tolerances for building the parts was difficult for that time, but a 1980s project showed it to be possible and the machine does work.

He went on to design the Analytical Engine, still mechanical, but it was programmable, able to store numbers and compute with them.

** Ada Augusta, daughter of Lord Byron, and designed some programs for the Analytical Engine, and is credited as the world’s first programmmer. To this day, no Analytical Engine has been built based on that original design.

Later technology developments forgot/ignored Babbage and Ada’s work.

1880s, US: A census has to happen, and it works via census workers doing surveys house-to-house, with the data collected on little cards, finally completed in 1892 because of the manual processing needed for it all.

The proposed solution: a series of holes to encode the information, a certain hole pattern representing something, with the cards put through a cylinder with copper contacts to use the idea of circuits completing; we can detect the little pulses.

** This was Hollerith –> created a company for creating machines catering to businesses, –> BAM! –> IBM (International Business Machines), which had the market cornered till the personal computer revolution of 70s/80s onward

** Next: Atanasoff & Berry: the first electromechanical computer, done at Iowa State Uni.

** Zuse, in Germany, with similar work; his machine was lost to WWII bombs

** Alan Turing (!): in Britain, working on computers (the Colossus) for cryptographic work

** The three above were forgotten until post-1970s; remembered:

Aiken, -- Harvard Mark I
ENIAC -- military artillery calculations, used for artillery tables
its clock speed: ~0.0000005 GHz
EDSAC
...[all post-WWII]


Until this time, “computer” meant a person doing calculations

There were also “differential analyzers” which were mechanical but could plot differential equations’ outputs/inputs; LA’s is now held at the Smithsonian.

NIST came up with the SEAC, SWAC (Standard’s West Automatic Computer) – both late 1940s to early 1950s.

The rate of computing speed increasing is insane; pity the airline industry couldn’t sustain similar growth.

Popular culture in the 1950s thought that things like chess were hard, not the problems we consider hard today, like facial recognition, natural language processing, etc. Sci-fi saw bigger and bigger computers, centralized and “smarter”, “knowing everything”.

Our modern use of computers for entertainment and communication would have been unimaginable back then.

### Components of Computing

• Memory

• Central Processing Unit (CPU)

• Input/Output Devices (e.g. keyboard, monitor, etc.)

Memory stores integers, with each stored in a location. Memory location numbers and content numbers.

How about dealing with fractions and decimals if we have only integers?

43.78 –> 4378 100 [irrational fraction]

      4378 1 [scientific notation-like system; most modern ones use this]

43 78 [split it into parts]


How about text using just numbers?

–> map characters to numbers with an encoding scheme: a sequence of numeric codes represents a certain character sequence:

H e l l o --> 72 45 76 76 24


Input/output will ultimately reduce down to numbers.

cout << "$" << amountWithheld << " was withheld.\n"; }  Normally, languages distinguish between different types of numbers, etc. Machine language instructions working with integers is historically faster than dealing with numbers with fractional parts (i.e. floating-point types). We need to consider what type of number we’ll need. Here, for example, we can have fractional hours worked. #### cin Analogous to cout: cin » variable; ### Identifiers Rules: 1. Starts with a letter or underscore, followed by 0 or more letters, digits, or underscores. NO spaces. Cannot begin with a number. Example of a bad name: expertsexchange.com Be careful when running words together Conventions: 1. Run-together: hoursworked 2. Underscores (snake_case): hours_worked 3. CamelCase: hoursWorked ** Single-letter variables are a BAD idea in programming ** ### Variables First, we declare variables, specifying their type and identifier. Example: double hoursWorked;  Range for doubles: -/+ 10e-308 to -/+ 10e308 And for signicant digits, there is a limit: about 15 for a double, and 6-7 for a float. /* TABLE: Variable Types |——-|——–| | Variable Types | |——-|——–|  Type Size Values bool 1 byte True (1) or False (0) char 1 byte a-z, A-z, 0-9, space, tab, etc. short 2 bytes [-]2^15 to (2^15)-1 –> [-]32,768 to 32,767 int 4 bytes [-]2^31 to (2^31)-1 –> 2,147,483,648 to 2,147,483,647 long 4 bytes [-]2^31 to (2^31)-1 –> 2,147,483,648 to 2,147,483,647 float 4 bytes [+/-]2^(-126) to 2^128 –> ( 1.2 * 10^(-38) to 3.4 * 10^38) double 8 bytes [+/-] (2.3 * 10^(-308) to 1.7 * 10^308) */ ### Comments // This is a comment, meant for human readers ## Lecture 4: 7 October 2013 ### Earnings Calculator Continued #include <iostream> using namespace std; int main() { // Smallberg's stylefor "section headings" for explaining int hours; double payPerHour; double earnings; double amountWithheld; const double PCT_WITHHELD = 0.10; cout.setf(ios::fixed); // see pp. 30-31 in Savitch book cout.precision(2); // Pay attention to where newlines are supposed to happen! cout << "How many hours did you work this week? "; cin >> hours; cout << "What is your hourly rate of pay? "; cin >> payPerHour; earnings = hours * payPerHour; amountWithheld = earnings * PCT_WITHHELD; cout << "You earned$" << earnings << endl;

// Don't forget the dollar sign; the computation
// won't automatically prefix it to the result