Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Midterm Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS230 Lectures Series.

Similar presentations


Presentation on theme: "1 Midterm Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS230 Lectures Series."— Presentation transcript:

1 1 Midterm Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS230 Lectures Series

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

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

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

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

6 6 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 = ?

7 7 Pointer Arithmetic 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 = _____________ pointer variable vPtr v[0]v[1]v[2]v[4]v[3] 30003004300830123016 location

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

9 9 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; }

10 10 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; }

11 11 Mastering CBV vs. CBF void Swap(int *a, int *b); void main( ) { int x = 2, y = 3; Swap(&x, &y); cout << x << “ “ << y; } void Swap(int* a, int* b) { int c; c = (*a); (*a) = (*b); (*b) = c; } void Swap_2(int* a, int* b) { int *c; c = a; a = b; b = c; } Comparing …


Download ppt "1 Midterm Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS230 Lectures Series."

Similar presentations


Ads by Google