24
Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University [email protected] EECS 230 Lectures Series

Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University [email protected] EECS 230 Lectures Series

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

Final Review

Ying Wu Electrical Engineering & Computer

ScienceNorthwestern [email protected]

EECS 230 Lectures Series

Page 2: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

About EECS 230 The goal

– Getting a good glimpse of C/C++ programming– Learning a real useful programming language– Understanding software design– Accumulating experience– Becoming a junior programmer

What I want to concentrate– Familiarizing C/C++ syntax– Understanding important and core C/C++ concepts– Grasping a tool, i.e., the Debugger– Finding the best way for further study

What are beyond– Data structure– Compiler and OS– Algorithms– Architecture

Page 3: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

Grading Scale

MPs and exams[MPs Midterm Final][30% 30% 40%]

The weights for MPs[MP#1, MP#2, MP#3, MP#4, MP#5, MP#6, MP#7,

MP#8][ 3% 3% 3% 5% 4% 4% 4%

4% ]

Grade scale A/A- > 40%B+/B/B- > 45%C+/C/C- < 15%D you can figure it out!

Page 4: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

CTEC survey

Please fill the CTEC survey Our course number: EECS 230-0-20 Instructor:

– Professor Ying Wu TAs are:

– Ming Yang – David Choffnes – Yao Zhao

Page 5: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

What we’ve learned (I)

Basic C/C++ syntax– Data type– Control

if, if/else, switch, while, do/while, for

– Pointer, array, reference– Function– C++ Class

Page 6: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

What we’ve learned (II) Basic C/C++ concepts

– FunctionCall-by-value vs. call-by-reference What can be returned and what can not be

return?

– Array and pointers Relationship? (similarity and dissimilarity)Data in memory

– Local variable and scope (life-cycle)– Pointers and references

What is reference? Why is it special?Point arithmetic

– static– Const

Page 7: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

What We’ve learned (III) Advanced C++ concepts

– Dynamic Memory Allocation – Classes with pointer data members – Copy constructor – Operator overloading– Constructors/destructors

When constructors and destructors are called

– Linked list

Page 8: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

About Final exam

Date: March 16th (Fri) 9 – 11 am Place: Tech LR4 Content:

– Several straightforward problems– Read code and write results– Write code

For your benefits– Be extremely careful– Write down your intermediate results

(as more as you can) for partial credits

Page 9: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

Let’s Warm up …Questions 1:

int a = 2, b = 4, c;

c = (++a >= 3 ? b -- : b ++);

Then a = ? b = ? c = ?

Questions 2:

int a = 2, b = 0, c = 0;

While ( a < 4){

b += c ++;

a ++;

}

Then a = ? b = ? c = ?

Page 10: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

2D pointers Important Concepts

int *p1;– p1 is a pointer– What does p1 point to?

Can be an integerCan be an integer array and we can index by p1[n]. Here p1[n] is an integer

int **p2;– p2 is a pointer– What does p2 point to?

Can be a pointer pointing to an integerCan be an array of pointers and we can index by p2[n]. Here p2[n] is a pointer! Therefore, p2[n] can point to an integer or an integer array.

Page 11: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

810010000

1001000010020008

1002000810090080

int a = 8;

int *pa = &a;

int **ppa = &pa;

Question:

[1] (*pa) = ?

[2] (**ppa) = ?

Page 12: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

10010000

1001000010020008

int *pa = new int [3];

Page 13: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

10014006

10020008

int **ppa = new int* [3];

for(int k=0;k<3;k++)

ppa[k] = new int [3];

1002000810090080

10010308

10010000

10010000

10010308

10014006 Question:

(1) ppa[1] = ?

(2) ppa[1][2] = ?

(3) ppa + 2 = ?

Page 14: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

Example: Table

I want to implement a general table class, which holds any size of row/column.

How can I construct such a table? What should we do for the constructor and

destructor?

Page 15: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

class CTable()

{

int **m_pData;

int m_nRow;

int m_nCol;

public:

CTable(int r = 1, int c = 1);

~CTable();

};

Page 16: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

CTable::CTable(int r, int c)

{

assert(r>0 && c>0);

m_nRow = r;

m_nCol = c;

m_pData = new int *[m_nRow];

assert(m_pData != NULL);

for(int k=0; k<m_nRow; k++){

m_pData[k] = new int [m_nCol];

assert(m_pData[k] != NULL);

}

}

Page 17: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

CTable::~CTable()

{

for(int k=0; k<m_nRow; k++)

delete [] m_nData[k];

delete [] m_nData;

}

Page 18: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

Local variable and scopeint myfunc(int a, int& b)

{

int c;

c = a + b;

return c;

}

Questions:• What are the exact stuff passed to the function?

• What happen to the memory when executing into the function?• What are the exact stuff return from the function?

• What can NOT be returned?

• What if a/b/c/t1/t2/t are not just int but objects?

void main()

{

int t, t1 = 1, t2 = 1;

t = myfunc(t1 + t2);

}

copy

t

t1=1

t2=1

a

c

c’

copy

Page 19: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

An interesting questionint *LocationOfAge()

{

int age;

cin >> age;

// missing code here

}

return &age;

int *p = new int;

*p = age;

return p;

return (new int(age));

Page 20: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

When constr/desctr are calledclass M

{

public:

M();

M(const M & );

~M();

};

M::M()

{

cout << "call default constructor\n";

}

M::M(const M & m)

{

cout << "call copy constructor\n";

}

M::~M()

{

cout << "call destructor\n";

}

Page 21: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

void myfun1(M t)

{

cout << "go inside myfunc1()\n";

}

M& myfun4()

{

cout << “go inside myfunc4()\n”;

M *temp = new M;

return *temp;

}

void myfun2(M &t)

{

cout << "go inside myfunc2()\n";

}

M myfun3()

{

cout << “go inside myfunc3()\n”;

M temp;

return temp;

}

Page 22: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

void myfun1(M t);

void myfun2(M &t);

M myfun3();

M& myfun4();

void main()

{

M a;

M b = a;

M c(a);

myfun1(b);

myfun2(a);

a = myfun3();

b = myfun4();

M *d = new M;

delete d;

}

call default constructor

call copy constructor

call copy constructor

call copy constructor

go inside myfunc1()

call destructor

go inside myfunc2()

go inside myfunc3()

call default constructor

call copy constructor

call destructor

call destructor

go inside myfunc4()

call default constructor

call default constructor

call destructor

call destructor

call destructor

call destructor

IMPORTANT!

RUN this example by yourself!

Page 23: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

Linked list

class CNode {

public:

CNode( const int &d ) : data(d), nextPtr(0){};

 

int data;

CNode *nextPtr;

};

Page 24: Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

Linked list

Question: how do you find 8 in the list?

firstPtr

7 9 8 35

Node* find(Node* firstPtr, int q)

{

ListNode *curPtr = firstPtr; // step I

while (curPtr != NULL) // step II

if (curPtr->data == q) // step III

break;

curPtr = curPtr->nextPtr; // step IV

}

return curPtr;

}

Question: how to allocate a node to hold 10?

Question: how to insert the node of 10 after 8? (8 could be the last one in the list)