Intro to programming - Summary for entire course PDF

Title Intro to programming - Summary for entire course
Course Introduction to Programming
Institution University of Southern California
Pages 17
File Size 856.4 KB
File Type PDF
Total Downloads 38
Total Views 146

Summary

Summary for entire course...


Description

Basics Monday, February 18, 2019 10:08 PM

In C++ a representation is called a type Var Widths (bits not bytes): char: 8 short: 16 int and float: 32 long and double: 64 Two properties of integer types:  Width (number of bits)  Signed vs. unsigned Operator Precedence: Parentheses, Exponents, Multiplication/Division/Modulo, Addition/Subtraction Computers love integer math - very fast

Why you need to cast!

PRE AND POST INCREMENT X++ and ++X 4 > 3 > 2 is false Evaluates to 1 > 2 As 4>3 is true which is numerically 1!

Control Structures Monday, February 18, 2019 10:22 PM

else if and else blocks not mandatory. Ternary Operator: x > 2 ? 1 : 0; Can be assigned: y = x > 2 ? 1 : 0; 1 if true, 0 if false do-while if you want body to execute once at least. break; //exit loop continue; //jump to beginning of loop

Arrays Monday, February 18, 2019 10:33 PM

Data structure: “In computer science, a data structure is a particular way of organizing data in a computer so that it can be used efficiently.” Primary Characteristics:  Homogeneous data elements: all elements are the same type (int, char, etc.)  Contiguously allocated: one element after another  Statically sized: size of the collection must be known at creation and can not change Memory allocated = n * element size Array of 50 ints = 200 bytes C/C++ does not keep track of where each element is Rather, calculates *position* when needed When declaring functions with them as a parameter, func_name(array_name[]) But when calling func_name(array_name)

No brackets

C-strings are “delimited”, or marked with the NULL character (\0) to signify their end. After all, C and C++ do not keep track of array bounds. When declaring functions with multidimensional arrays as parameters, Type in dimensions but first is func_name(array_name[][2][3]) not mandatory Same way of calling, no brackets Linear Address: Start Address + (i*numc + j)*sizeof(data_type) Start Address + (i*numr*numc +j*numc + k)*sizeof(data_type)

2D Array - B/W Image 3D Array - Color Image int foo[3] = {0, 2, 4}; cout 2 ? 1 : 0; int foo[4]; //Sample int array declaration int jimmy [3][5]; Linear Address: Start Address + (i*numc + j)*sizeof(data_type) Start Address + (i*numr*numc +j*numc + k)*sizeof(data_type) int foo[3] = {0, 2, 4}; cout > numbers[*n] >> items[*n]; if (!ifile.fail()) { *n = (*n) + 1; } } }

If you're using getline() after cin >> something or ifstream >> something, you need to flush the newline character out of the buffer in between. You can do it by using cin.ignore(). getline(cin, var). Can use getline on string and ifstream, not stringstream. If you use >> on ifstream before getline, getline will only get the remainder of the line. Using getline twice overwrites the variable. stringstream ss(str); //str is a string while (ss >> x) Array: Go to index i, O(1). Append, O(1). Prepend, O(n).

LinkedList: Go to index I, O(i). Append, O(n). Prepend, O(1). Vectors: O(1), O(1), O(n) Deques: O(1), O(1), O(1), Middle Insertion, O(n) vector my_vec(5); my_vec.push_back(10); my_vec[0] = 30; my_vec.pop_back(); my_vec.size(); my_vec.erase(my_vec.begin() + 2); my_vec.insert(my_vec.begin() + 1, 43); //(my_vec.begin(), 2, 43) inserts 43 twice If you don’t provide an initial size to the vector, you must add items using push_back() When iterating over the items with a for loop, use an 'unsigned int' Pros: Fast access, fast addition or removal from back. Cons: Erasing from beginning or middle has poor performance. If you push_back() past the declared size, has to allocate new block of memory and copy everything. Deque has pop_front and push_front unlike vector. Slightly slower random access, but fast at adding/removing at both front/back. Pass by pointer vs pass by reference: PBR can't pass array Big O: Loop O(n), Nested Loop O(n^2), If loop counter multiplies instead of increments, O(log n) for that layer. So if it were stand alone, O(log n), if it were nested O(n logn). #ifndef DATE_H #define DATE_H class Date { private: int m_year; public: Date(int year); void SetDate(int year); int getYear() { return m_year; } }; #endif Shift Right: x>>y = x/2^y. Shift Left: x...


Similar Free PDFs