110 likes | 239 Views
EECS230 Lectures Series. Midterm Review. Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu. What we’ve learned. Basic C/C++ syntax ++/-- C/C++ control structures if, if/else, switch/case for, while, do/while
E N D
EECS230 Lectures Series Midterm Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu
What we’ve learned • Basic C/C++ syntax • ++/-- • C/C++ control structures • if, if/else, switch/case • for, while, do/while • Array, Pointer and Reference • Pointer arithmetic • char array and string • Call-by-Value and Call-by-Reference • MP#1,#2,#3,#4
About the exam • Time • 1:00 – 1:50pm • Format • Read code and write down the results • Fill in blanks • Convert code • How to do well in exam? • Master all CORE concepts • Be extremely careful • Always write down the intermediate results • Do all the MPs by yourself • Convert yourself to be a debugger.
Let’s warm up! I. An easy question: int k = 5; cout << ++k << endl; cout << k++ << endl; After that, k = ? II. After you fully understand it, then int k = 5, j = 0; do{ if (j++ < 2) cout << k++ << endl; else cout << ++k << endl; } while(k++ < 10) How does the loop go?
A good approach … Convert it into something you can easily understand int k = 5, j = 0; do{ if (j < 2){ j j+1; cout << k << endl; k k+1; } else{ j j+1; k k+1; cout << k << endl; } } check(k < 10), k k+1 then loop
Pointers and References int a = 3; int c = a; int &b = a; int *p = &a; b = 4; // then a = ? c = ? and *p = ? *p = 5; // then b = ? c = ? and a = ?
Pointer Arithmetic location 3000 3004 3008 3012 3016 v[0] v[1] v[2] v[3] v[4] pointer variable vPtr • int v[5] = {2, 4, 6, 1, 3}; vPtr= v; (i.e., vPtr points to first element v[0]). And supposing vPtr is at location 3000 • Questions: • Q1: vPtr[3] = ______ • Q2: vPtr + 1 = _______ • Q3: After vPtr += 2, then (*vPtr) = ___________ • Q4: After Q3, vPtr – v = _____________
Switch and if/elseif Classification: ‘A’ ---- [90 100) or [0 10) ‘B’ ---- [70 80) or [10 20) ‘C’ ---- [50 70) ‘D’ ---- otherwise It is easy to write an if/elseif control, but the structure is not so clear. Can we write it using switch/case?
int x; switch (x/10){ case 0: case 9: cout << “A Class” << endl; break; case 1: case 7: cout << “B Class” << endl; break; case 5: case 6: cout << “C Class” << endl; break; default: cout << “D Class” << endl; break; }
To be a tracer! • void main() • { • int h = 9/2, m, n; • for ( int k=0; k<9; k++){ • m = ( h-k>0 ? h-k : k-h ); • n = 2*(h-m) + 1; • print_char(m, ‘ ‘); • print_char(n, ‘*’); • cout << endl; • } • } • Where • void print_char(int n, const char c) • { • for(int k=0;k<n;k++) • cout << c; • }
Mastering CBV vs. CBF void Swap(int* a, int* b) { int c; c = (*a); (*a) = (*b); (*b) = c; } void Swap(int *a, int *b); void main( ) { int x = 2, y = 3; Swap(&x, &y); cout << x << “ “ << y; } void Swap_2(int* a, int* b) { int *c; c = a; a = b; b = c; } Comparing …