cs31 discussion 1e
play

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


  1. CS31 Discussion 1E Spring 17’: week 05 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju

  2. Road map of CS31 String Array Function Class Variable If / else Loops Pointer

  3. Today’s Agenda } Use project 3 to get more insights about functions } Peer discussion

  4. When we talk about function… } How function are executed in a program } Consumer-Provider relation

  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?

  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

  7. What is your role? User Provider } How to use the function? } What’s the goal of the function? } How do you expect users to use this function? } How do you implement it?

  8. User Math example 10 1 + 2.7 cout <<

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

  10. User Math example } Step 1: Use your own words to ask Google

  11. User Math example } Step 1: Use your own words to ask Google } Step 2: Read the parameter description carefully

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

  13. User Dice throw example cout <<

  14. User Dice throw example } Step 1: Use your own words to ask Google

  15. User Dice throw example } Step 1: Use your own words to ask Google } Step 2: Read the parameter description carefully

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

  17. User Math example #include <iostream> #include <cstdlib> using namespace std; int main() { cout << (rand() % 6 + 1) << endl; return 0; } } Question: why it always give the same value?

  18. User Project 3 warm-up } What’s the difference between #include “…” and #include <...>?

  19. User Project 3 warm-up } *.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

  20. Provider 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?

  21. Provider 1. What’s the goal of the function

  22. Provider 2. How people are going to use this function A: Hey, I will give you a base and an exponent B: Sure, I will give you the result

  23. Provider 2. How people are going to use this function

  24. Provider 2. How people are going to use this function 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.

  25. Provider 2. How people are going to use this function distance true dir plotChar Start at (r, c) false

  26. Provider 3. How to implement } Modular design!

  27. Provider 3. How to implement } Modular design!

  28. Provider 3. How to implement } Modular design!

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

  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

  31. Determine function call graph int factorial(int n) { void printFactorial(int n) { int prod = 1; int prod = 1; for (int i = 2; i <= n; i++) for (int i = 2; i <= n; i++) prod *= i; prod *= i; return prod; for (int k = 1; k <= 6; k++) { } cout << "The factorial of " << k << " is " int main() { << factorial(k) << endl; 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 5 Function fun question 4 main() factorial() main() printFactorial() Call direction

  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); } main() printFactorial() factorial()

  33. Provider 3. How to implement Always pay attention to what are available to you!

  34. Provider Project 3 warm-up draw() clearGrid() setChar() getChar()

  35. Provider Project 3 warm-up main() draw() clearGrid() setChar() getChar()

  36. Provider Project 3 – Phase 2 plotLine() What’s the call relation here? draw() clearGrid() setChar() getChar()

  37. Provider Project 3 – Phase 2 plotLine() draw() clearGrid() setChar() getChar()

  38. Provider Project 3 – Phase 3 main() executeCommands() plotLine() draw() clearGrid() setChar() getChar()

  39. Provider Project 3 – Phase 3 main() executeCommands() C F@ B@ H00 V00 plotLine() draw() clearGrid() setChar() getChar()

  40. Provider Project 3 – Phase 3 main() executeCommands() C F@ B@ H00 V00 plotLine() draw() clearGrid() setChar() getChar()

  41. Provider Project 3 - executeCommands() } A parser question Give me the "35 + 27 * 69" calculation result T ell me the "report.docx" "report.png" file type Generate a "int main() { return 0; }" program

  42. Provider Project 3 - executeCommands() } A parser question } A typical approach: } Tokenize } Interpret / convert tokens "v2b h12fHh1fih0"

  43. Provider Project 3 - executeCommands() } A parser question } A typical approach: } Tokenize } Interpret / convert tokens "v2b h12fHh1fih0"

  44. Provider Project 3 - executeCommands() } 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

  45. Provider Project 3 - executeCommands() } Write down a lot of both good examples and bad examples

  46. Provider Project 3 - executeCommands() } Tokenize: Look forward strategy } Separate into several cases: } C } F@ } B@ } H-- "v2b h12fHh1fih0" } H2 } H22 } H-2 } H-22 } V

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend