CS31 Discussion 1E Spring 17: week 05 TA: Bo-Jhang Ho - - PowerPoint PPT Presentation

cs31 discussion 1e
SMART_READER_LITE
LIVE PREVIEW

CS31 Discussion 1E Spring 17: week 05 TA: Bo-Jhang Ho - - PowerPoint PPT Presentation

CS31 Discussion 1E Spring 17: week 05 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju Road map of CS31 String Array Function Class Variable If / else Loops Pointer Todays Agenda } Use project 3 to get more


slide-1
SLIDE 1

CS31 Discussion 1E

Spring 17’: week 05

TA: Bo-Jhang Ho

bojhang@cs.ucla.edu

Credit to former TA Chelsea Ju
slide-2
SLIDE 2

Road map of CS31

Variable If / else Loops Pointer Array Function Class String

slide-3
SLIDE 3

Today’s Agenda

} Use project 3 to get more insights about functions } Peer discussion

slide-4
SLIDE 4

When we talk about function…

} How function are executed in a program } Consumer-Provider relation

slide-5
SLIDE 5

An example of a function

int square(int a) { return a * a; } int main() { int a = 3; int b = 6; cout << square(a + b) << endl; return 0; }

} Are two variable a’s the same?

slide-6
SLIDE 6

An example of a function

int square(int a) { return a * a; } int main() { int a = 3; int b = 6; cout << square(a + b) << endl; return 0; }

} Different functions have their own variable sets } Try it on VC++ / Xcode

slide-7
SLIDE 7

What is your role?

} How to use the function?

User Provider

} What’s the goal of the

function?

} How do you expect users

to use this function?

} How do you implement

it?

slide-8
SLIDE 8

Math example

User

1 + 2.7

cout <<

10

slide-9
SLIDE 9

Math example

User

#include <iostream> using namespace std; int main() { cout << ???????????????????? << endl; return 0; }

slide-10
SLIDE 10

Math example

User

} Step 1: Use your own words to ask Google

slide-11
SLIDE 11

Math example

User

} Step 1: Use your own words to ask Google } Step 2: Read the parameter description carefully

slide-12
SLIDE 12

Math example

User

#include <iostream> #include <cmath> using namespace std; int main() { cout << (1 + pow(2.7, 10.0)) << endl; return 0; }

slide-13
SLIDE 13

Dice throw example

User

cout <<

slide-14
SLIDE 14

Dice throw example

User

} Step 1: Use your own words to ask Google

slide-15
SLIDE 15

Dice throw example

User

} Step 1: Use your own words to ask Google } Step 2: Read the parameter description carefully

slide-16
SLIDE 16

Math example

User

#include <iostream> #include <cstdlib> using namespace std; int main() { cout << (rand() % 6 + 1) << endl; return 0; }

slide-17
SLIDE 17

Math example

User

#include <iostream> #include <cstdlib> using namespace std; int main() { cout << (rand() % 6 + 1) << endl; return 0; }

} Question: why it always give the

same value?

slide-18
SLIDE 18

Project 3 warm-up

User

} What’s the difference between

#include “…” and #include <...>?

slide-19
SLIDE 19

Project 3 warm-up

User

} *.h shows the “interface” of the functions

} And this is what you need only

} *.cpp should be included to make the program complete

} You’re welcome to check the details, but it’s not necessary

slide-20
SLIDE 20

Questions you have to ask yourself when developing a function

} What’s the goal of the function? } How do you expect users to use this function?

} What information have to pass to your function? } What message are you going to pass back?

} How do you implement it?

Provider

slide-21
SLIDE 21
  • 1. What’s the goal of the function

Provider

slide-22
SLIDE 22
  • 2. How people are going to use this function

Provider

A: Hey, I will give you a base and an exponent B: Sure, I will give you the result

slide-23
SLIDE 23
  • 2. How people are going to use this function

Provider

slide-24
SLIDE 24
  • 2. How people are going to use this function

Provider

Please pass a coordinate (r, c) to me, remember to mention whether it’s a horizontal line or a vertical line, also give me the length as well as the character. Let me know the mode: I can support both foreground and background. If I can successfully draw a line based on the parameters you gave me, I tell you true, otherwise I reply false.

slide-25
SLIDE 25
  • 2. How people are going to use this function

Provider

Start at (r, c) dir distance plotChar true false

slide-26
SLIDE 26
  • 3. How to implement

} Modular design!

Provider

slide-27
SLIDE 27
  • 3. How to implement

} Modular design!

Provider

slide-28
SLIDE 28
  • 3. How to implement

} Modular design!

Provider

slide-29
SLIDE 29

Determine function call graph

void printSpaceStarLine(int numSpaces, int numStars); void printLayer(int startNumSpaces, int startNumStars) { printSpaceStarLine(startNumSpaces, startNumStars); printSpaceStarLine(startNumSpaces - 1, startNumStars + 2); printSpaceStarLine(startNumSpaces - 2, startNumStars + 4); } int main() { printLayer(5, 1); // * // *** // ***** printLayer(4, 3); // *** // ***** // ******* printLayer(3, 5); // ***** // ******* // ********* printLayer(2, 7); // ******* // ********* //*********** printSpaceStarLine(4, 3); // *** printSpaceStarLine(4, 3); // *** return 0; }

slide-30
SLIDE 30

Determine function call graph

void printSpaceStarLine(int numSpaces, int numStars); void printLayer(int startNumSpaces, int startNumStars) { printSpaceStarLine(startNumSpaces, startNumStars); printSpaceStarLine(startNumSpaces - 1, startNumStars + 2); printSpaceStarLine(startNumSpaces - 2, startNumStars + 4); } int main() { printLayer(5, 1); printLayer(4, 3); printLayer(3, 5); printLayer(2, 7); printSpaceStarLine(4, 3); printSpaceStarLine(4, 3); return 0; }

main() paintLayer() paintSpaceStarLine()

Call direction

slide-31
SLIDE 31

Determine function call graph

int factorial(int n) { int prod = 1; for (int i = 2; i <= n; i++) prod *= i; return prod; } int main() { for (int k = 1; k <= 6; k++) { cout << "The factorial of " << k << " is " << factorial(k) << endl; } } void printFactorial(int n) { int prod = 1; for (int i = 2; i <= n; i++) prod *= i; for (int k = 1; k <= 6; k++) { cout << "The factorial of " << k << " is " << factorial(k) << endl; } } int main() { for (int k = 1; k <= 6; k++) printFactorial(k); }

Function fun question 4 Function fun question 5

main()

Call direction

factorial() main() printFactorial()

slide-32
SLIDE 32

Determine function call graph

int factorial(int n) { int prod = 1; for (int i = 2; i <= n; i++) prod *= i; return prod; } void printFactorial(int n) { for (int k = 1; k <= n; k++) { cout << "The factorial of " << k << " is " << factorial(k) << endl; } } int main() { for (int k = 1; k <= 6; k++) printFactorial(k); }

factorial() main() printFactorial()

slide-33
SLIDE 33
  • 3. How to implement

Provider

Always pay attention to what are available to you!

slide-34
SLIDE 34

Project 3 warm-up

Provider

setChar() clearGrid() getChar() draw()

slide-35
SLIDE 35

Project 3 warm-up

Provider

setChar() clearGrid() getChar() draw() main()

slide-36
SLIDE 36

Project 3 – Phase 2

Provider

setChar() clearGrid() getChar() draw() plotLine()

What’s the call relation here?

slide-37
SLIDE 37

Project 3 – Phase 2

Provider

setChar() clearGrid() getChar() draw() plotLine()

slide-38
SLIDE 38

Project 3 – Phase 3

Provider

setChar() clearGrid() getChar() draw() plotLine() executeCommands() main()

slide-39
SLIDE 39

Project 3 – Phase 3

Provider

setChar() clearGrid() getChar() draw() plotLine() executeCommands() main()

C F@ B@ H00 V00

slide-40
SLIDE 40

Project 3 – Phase 3

Provider

setChar() clearGrid() getChar() draw() plotLine() executeCommands() main()

C F@ B@ H00 V00

slide-41
SLIDE 41

Project 3 - executeCommands()

Provider

} A parser question

"35 + 27 * 69" "report.docx" "report.png" "int main() { return 0; }" Give me the calculation result T ell me the file type Generate a program

slide-42
SLIDE 42

Project 3 - executeCommands()

Provider

} A parser question } A typical approach:

} Tokenize } Interpret / convert tokens

"v2b h12fHh1fih0"

slide-43
SLIDE 43

Project 3 - executeCommands()

Provider

} A parser question } A typical approach:

} Tokenize } Interpret / convert tokens

"v2b h12fHh1fih0"

slide-44
SLIDE 44

Project 3 - executeCommands()

Provider

} A parser question } A typical approach:

} Tokenize } Interpret / convert tokens

} Why parsing is hard?

} The length of tokens vary } There can be many exceptions and hard to come out a unified

parsing strategy

} In most cases, there are constraints across tokens (but not in

this assignment)

} Have to report “precise” error position

slide-45
SLIDE 45

Project 3 - executeCommands()

Provider

} Write down a lot of both good examples and bad

examples

slide-46
SLIDE 46

Project 3 - executeCommands()

Provider

} Tokenize: Look forward strategy } Separate into several cases:

} C } F@ } B@ } H--

} H2 } H22 } H-2 } H-22

} V

"v2b h12fHh1fih0"

slide-47
SLIDE 47

Discussion time

} Find a partner and tell him/her what you have done, what

program you encounter now

} Brain storming the solution } Read code, use debugger help you, give suggestions