cs31 discussion 1e
play

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

CS31 Discussion 1E Spring 17: week 06 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju Puzzle time: How many lines will this program print? #include <iostream> int main() { for (unsigned int i = 3; i >= 0; i--)


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

  2. Puzzle time: How many lines will this program print? #include <iostream> int main() { for (unsigned int i = 3; i >= 0; i--) cout << "Print one line" << endl; return 0; }

  3. Puzzle time: How many lines will this program print? #include <iostream> int main() { for (unsigned int i = 3; i >= 0; i--) cout << "Print one line" << endl; return 0; } Print lines like crazy!!!!

  4. Overflow issue

  5. Puzzle time: How many lines will this program print? #include <iostream> int main() { for (unsigned int i = 3; i >= 0; i--) cout << "Print one line" << endl; return 0; } } Basic data types Type Bytes Bits Value range int 4 32 -2,147,483,648 to 2,147,483,647 unsigned int 4 32 0 to 4,294,967,295 double 8 64 -1.7 * 10^308 to 1.7 * 10^308

  6. Puzzle time: How many lines will this program print? #include <iostream> int main() { for (unsigned int i = 3; i >= 0; i--) cout << "Print one line" << endl; return 0; } } Basic data types Type Bytes Bits Value range int 4 32 -2,147,483,648 to 2,147,483,647 unsigned int 4 32 0 to 4,294,967,295 double 8 64 -1.7 * 10^308 to 1.7 * 10^308 } Discussion: Will double variable overflow?

  7. Today’s Agenda } Overflow } Good coding style part 3 } Project 4 discussion } C-style string } Hints for learning pointer (spoiler alert)

  8. Project 3 call graph - planed main() executeCommands() C F@ B@ H00 V00 plotLine() draw() clearGrid() setChar() getChar()

  9. Project 3 call graph of the sample solution main() executeCommands() executeOneCommand() C F@ B@ H00 V00 consumeNumber() plotLine() draw() clearGrid() setChar() getChar()

  10. Good coding style guideline } (2) Indentation } One more tab inside a { } block } (6) If a function receives invalid parameters, reject the request ASAP! } And by rejecting, I mean “return false.” } (7) Use single quote character when you refer to a certain character, don’t use numbers! } ‘a’ or 97 ?

  11. (2) Indentation – printing rectangle int main() { for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) { cout << "*"; } cout << endl; } return 0; } } Execution result:

  12. (2) Indentation – printing rectangle int main() { for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) { cout << "*"; } cout << endl; } return 0; }

  13. (2) Indentation – printing rectangle int main() int main() { { for (int r = 1; r <= 3; r++) { for (int r = 1; r <= 3; r++) for (int c = 1; c <= 4; c++) { { cout << "*"; for (int c = 1; c <= 4; c++) } { cout << endl; cout << "*"; } } cout << endl; return 0; } } return 0; }

  14. (2) Indentation – printing rectangle int main() { for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) { cout << "*"; } cout << endl; } return 0; }

  15. (2) Indentation – printing rectangle int main() { for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) cout << "*"; cout << endl; } return 0; }

  16. (6) Be a mean person: Reject invalid request ASAP! bool plotLine(int r, int c, int dir, int distance, char plotChar, int fgbg) { // Validate arguments if (r < 1 || r > NROWS || c < 1 || c > NCOLS) return false; switch (dir) { case HORIZ: if (c + distance < 1 || c + distance > NCOLS) return false; break; case VERT: if (r + distance < 1 || r + distance > NROWS) return false; break; default: return false; } if (fgbg != FG && fgbg != BG) return false; if (!isprint(plotChar)) return false; ...

  17. (6) Be a mean person: Reject invalid request ASAP! bool hasCorrectSyntax(string cmd, int& pos) { pos = 0; while (pos != cmd.size()) { switch (toupper(cmd[pos])) { default: return false; case 'C': pos++; break; case 'F': case 'B': // must be followd by a printable character pos++; if (pos == cmd.size() || !isprint(cmd[pos])) return false; pos++; break; case 'H': case 'V': pos++; if (pos == cmd.size()) return false; ... return true; }

  18. (6) Be a mean person: Reject invalid request ASAP! int executeCommands(string cmd, char& plotChar, int& mode, int& badPos) { int pos; if ( ! hasCorrectSyntax(cmd, pos)) { badPos = pos; return SYNTAX_ERROR; } int r = 1; int c = 1; pos = 0; // At the start of each loop iteration, pos is the position of the // start of the next plotting command within the command string, or // the end of that string. while (pos != cmd.size()) { if (!executeOneCommand(cmd, pos, r, c, plotChar, mode)) { badPos = pos; return EXECUTION_ERROR; } } return COMMAND_OK; }

  19. (6) Be a mean person: Reject invalid request ASAP! } Discussion: What are the advantages? } Tell the readers that I’m not going to address this cases } Cross out these impossible cases to make life easier } Make the logic clear } You don’t need to worry about the correctness of parameters anymore

  20. (7) Don’t replace single quote as numbers bool hasCorrectSyntax(string cmd, int& pos) { pos = 0; while (pos != cmd.size()) { switch (toupper(cmd[pos])) { default: return false; case 'C': pos++; break; case 'F': case 'B': // must be followd by a printable character pos++; if (pos == cmd.size() || !isprint(cmd[pos])) return false; pos++; break; case 'H': case 'V': pos++; if (pos == cmd.size()) return false;

  21. (7) Don’t replace single quote as numbers bool hasCorrectSyntax(string cmd, int& pos) { pos = 0; while (pos != cmd.size()) { switch (toupper(cmd[pos])) { default: return false; case 67: pos++; break; case 70: case 66: // must be followd by a printable character pos++; if (pos == cmd.size() || !isprint(cmd[pos])) return false; pos++; break; case 72: case 86: pos++; if (pos == cmd.size()) return false;

  22. Today’s Agenda } Overflow } Good coding style part 3 } Project 4 discussion } C-style string } Hints for learning pointer (spoiler alert)

  23. Project 4 demystify } 11 functions: } int enumerate(const string a[], int n, string target); } int locate(const string a[], int n, string target); } bool locateSequence(const string a[], int n, string target, int& begin, int& end); } int locationOfMin(const string a[], int n); } int moveToEnd(string a[], int n, int pos); } int moveToBeginning(string a[], int n, int pos); } int locateDifference(const string a1[], int n1, const string a2[], int n2); } int eliminateDups(string a[], int n); } bool subsequence(const string a1[], int n1, const string a2[], int n2); } int makeMerger(const string a1[], int n1, const string a2[], int n2, string result[], int max); } int divide(string a[], int n, string divider);

  24. Project 4 demystify } 11 functions: } int enumerate(const string a[], int n, string target); } int locate(const string a[], int n, string target); } bool locateSequence(const string a[], int n, string target, int& begin, int& end); } int locationOfMin(const string a[], int n); } int moveToEnd(string a[], int n, int pos); } int moveToBeginning(string a[], int n, int pos); } int locateDifference(const string a1[], int n1, const string a2[], int n2); } int eliminateDups(string a[], int n); } bool subsequence(const string a1[], int n1, } 2 Observations: const string a2[], int n2); } int makeMerger(const string a1[], int n1, } An array always follows in integer n, indicating the size const string a2[], int n2, string result[], int max); } A const modifier in front of an array } int divide(string a[], int n, string divider);

  25. Project 4 demystify } Observation 1: Why passing array size to a function? } Can we not get the array size from C++? } In C++, there is no standard way to get array size! } Not like string, you can call .size() method

  26. Project 4 demystify } Observation 1: Why passing array size to a function? } But, can we get the array size from C++? } In C++, there is no standard way to get array size! } Not like string, you can call .size() method } This thread talks about some hacky ways to get the size: http://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array } That’s why we should have the agreement with function users that please pass the array as well as the array size } That being said, providing correct parameters is the user’s duty. You shouldn’t worry about invalid parameters } If some users pass bad parameters, it’s their fault!!

  27. Project 4 demystify } Observation 2: Why const?

  28. Pass an array to a function } From caller: int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; foo(arr); } In the function: void foo(int params[10]) { … } or void foo(int params[]) { … } or void foo(int *params) { … }

  29. Pass an array to a function void fillInArray(int params[]) { for (int i = 0; i < 5; i++) params[i] = i; } int main() { int arr[5] = {100, 99, 98, 97, 96}; fillInArray(arr); for (int i = 0; i < 5; i++) cout << "Element " << i << ": " << arr[i] << endl; return 0; } } What’s the output?

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