Last quarter we did most of chapters 1-8 and some of Chapter 9 Outline of what we learned Parts of a computer (main memory, auxiliary memory, CPU, I/O) Part of a CPU: Control Unit, program counter, instruction register, ALU, accumulator, general registers Model of main memory (Looks like a long linear tape - each spot in memory has an address and data) The binary number system - counting; converting between decimal and binary The simplest C++ program (main, #include, cout, using, return 0) Using the software (cygwin, gvim, g++, gdb) Variables Operators (arithmetic, assignment, relational operators, logical operators) Control structures (if, if/else, switch, for, while, do/while, nesting) Functions Some more details of what we learned: New this quarter: hexadecimal number system, converting between decimal, binary, and hex Variables Declarations and definition (definitions reserve space, must define exactly once) Initializing a variable in its definition (2 ways) Standard types (int, float, double, long, bool, and strings) Scope (local vs. global) - variables are accessible for a specific section of C++ code Storage class (automatic vs. statis - variables exist for a specific part of execution of code Expressions and Operators Arithmetic, note that / is integer division No built-in exponentiation operator Relational operators Equality operators Logical operators Assignment operator (itself takes a value) Precedence Control structures (if, while, for, do/while, if/else, switch) Combine by nesting or stacking Controlling repetition with counter or flag Warning on switch: all cases executed until break is reached, so don't forget the break at the end of each case Break (causes immediate exit from while, for do/while and switch, continues on next statement) Continue (interupts body of while, for and do/while, continues on next iteration of loop) Comments - two forms, and how to make them useful Functions - The purpose of a function is to separate out a well-defined task of manageable size - Putting code in functions facilitates code reusability and program clarity - The name should reflect the task - A function's name shows up in 3 places - in a prototype (the declaration), in the definition of the function itself, and when the function is called. The syntax for each of these is of course different. - The prototype looks like the header line of the function, with no variables (unless you want to put them in for clarity) - A function can have parameters - "input values" For example, foo_function(int num_1, int num_2, char c) If there are no parameters, then the parentheses will be empty as in foo_function() - If a parameter is passed by value, then a copy of that value is given to the function - the caller's value is safe and the function has received the value and stored it in a new location - the function can modify it. - If a parameter is passed by reference, then that function has access to the caller's copy of that variable - this can be very useful, but beware! - There are advantages to pass-by-value and advantages to pass-by-reference What are they? - Careful - don't return a reference to a local variable! - A function can return values - "output values" If there are no return values, declare the function "void" If there is one return value, declare the function to return a variable of this type If there is more than one return value, then return this value with a reference parameter or pointer parameter - A function can have default parameters - if a default value is specified then the caller may supply it optionally, or the default value is used. Defaults are typically supplied in the prototype. Note that default variables must go at the end of the parameter list. - One important use for reference parameters is to return one or multiple values to the caller - Const parameters - pass by reference or by value, but function is not allowed to change them - Some important library functions - if you #include <cmath>, you get access to functions such as sqrt(x), ceil(x), floor(x), log(x), sin(x), pow(x,y) (each of which take doubles as parameters and return double as the function value - To get random numbers, #include <cstdlib> and use the function rand(), which returns an unsigned int between 0 and RAND_MAX. Use % to scale it to the range of values you want. - Call srand(time(0)); once at the beginning of the program to initialize the random number generator. #include <ctime> to be able to call the function "time" Arrays - How to declare - How to initialize - How to access individual elements File I/O - ifstream, ofstream are classes (they are not built-in types, and they have functions associated with them) - filestreamvariable.open() - filestreamvariable.is_open() - filestreamvariable.close() - using << and >> with ifstream and ofstream - How to handle errors in opening files, and in getting input out of files Strings - The string type is actually not built in - it's called a class, and thus has functions associated with it. - Some of these functions are stringvariable.c_str() stringvariable[i] stringvariable.length() - You can do arithmetic with strings, e.g. str2 = str1+str2; This concatenates. - You can compare two strings - they are in lexicographic (alphabetic) order