cs31 discussion 1e
play

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

CS31 Discussion 1E Spring 17: week 04 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju Puzzle time: What does the code print? int main() { int n = 5; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout


  1. One more example – put a lot spaces int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=66

  2. One more example – put a lot spaces int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=66

  3. One more example – put a lot spaces int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=66

  4. One more example – put a lot spaces int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=66

  5. One more example – put a lot spaces int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=66

  6. One more example – Put numbers in one line int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: 11 22 33

  7. One more example – Put numbers in one line int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: 11 22 33 Enter second number: Enter third number: 11+22+33=66

  8. One more example – Put a character int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: aa

  9. One more example – Put a character int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: aa Enter second number: Enter third number: 0+593+9527=10120

  10. One more example – Put a floating number int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: 11.5

  11. One more example – Put a floating number int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: 11.5 Enter second number: Enter third number: 11+0+9527=9538

  12. One more example – If we change the type to string? int main() { string a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; cout << a << "+" << b << "+" << c << ”=" << (a + b + c) << endl; return 0; } Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=112233

  13. Project 2 – Use const // Asset value cutoff points and trustee fee rates const double CUTOFF_1 = 1000 * 1000; const double CUTOFF_2 = 10000 * 1000; const double RATE_1 = 0.013; const double RATE_2_USUAL = 0.010; const double RATE_2_SPECIAL = 0.002; const double RATE_3 = 0.009; // Compute trustee fee double trusteeFee; if (value <= CUTOFF_1) trusteeFee = value * RATE_1; else { // Compute trustee fee for first bracket trusteeFee = CUTOFF_1 * RATE_1; ... } Const variable (constant) cannot be changed } Change at one place, apply to everywhere

  14. Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

  15. Good coding style guideline } (5) Don’t declare two variables whose lifecycles are overlapped

  16. Variable lifecycle int main() { if (100 < 150) { int a = 0; if (6 * 7 < 8) { a = 1; if (9 + 10 < -11) a = 2; } a = 3; } a = 4; return 0; } } Does it compile?

  17. Variable lifecycle int main() { int a = 0; if (100 < 150) { int a = 1; if (6 * 7 < 888) { a = 2; } } cout << a << endl; return 0; } } Does it compile then?

  18. Variable lifecycle int main() { int a = 0; if (100 < 150) { int a = 1; if (6 * 7 < 888) { a = 2; } } cout << a << endl; return 0; } } Does it compile then? } What’s the output?

  19. Variable lifecycle int main() { int a = 0; if (100 < 150) { int a = 1; if (6 * 7 < 888) { a = 2; } } cout << a << endl; return 0; } } Does it compile then? } What’s the output?

  20. Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

  21. What is a function int double double string } Take some numbers, produce a result value

  22. What is a function f(x) = 2x + 3 } What is f(5)?

  23. What is a function f(x, y) = 3x + 2y - xy } What is f(4, 1)?

  24. What is a function int square(int n) { return n * n; } int main() { int a = 3; int b = 6; a = a * b + square(a + b); cout << a << endl; return 0; } } What is the output?

  25. What is a function int square(int n); int main() { int a = 3; int b = 6; a = a * b + square(a + b); cout << a << endl; return 0; } int square(int n) { return n * n; } } When a function is called, it always look back and try to find the function. } If you want to declare it before the callee, you have to declare it first.

  26. Quiz time } 11 questions on the website

  27. Why function? } Save repeating codes } Make the main function more compact

  28. Function } Can we write a function which exchange the values of two variables? void swap(int x, int y) { //TODO: how to do this? } int main() { int a = 3; int b = 6; swap(a, b); // Hope a = 5, b = 3 now return 0; }

  29. Reference } We can use reference (& after the data type) to achieve the task void swap(int &x, int &y) { int t = x; x = y; y = t; } int main() { int a = 3; int b = 6; swap(a, b); cout << "a=" << a << ", b=" << b << endl; // Hope a = 5, b = 3 now return 0; } } x, y are going to control a, b

  30. Have you done the exercise? * * ** ** *** *** **** **** ***** ***** ***** ***** **** **** *** *** ** ** * *

  31. New challenges! * *** ***** *** * ***** *** ******* ***** ***** ******* ******* ********* ********* *********** *********** ************* ********* *********** ************* *** ***

  32. Draw a shape } Observation: For each line, we always print some white spaces, and then some stars, and an endline character. * *** ***** *** * ***** ******* *** ***** ***** ******* ******* ********* ********* *********** *********** ********* ************* *********** ************* *** ***

  33. Draw a shape } Observation: For each line, we always print some white spaces, and then some stars, and an endline character. void printSpaceStarLine(int numSpaces, int numStars) { for (int i = 0; i < numSpaces; i++) cout << " "; for (int i = 0; i < numStars; i++) cout << "*"; cout << endl; }

  34. Draw the pyramid void printSpaceStarLine(int numSpaces, int numStars) { for (int i = 0; i < numSpaces; i++) cout << " "; for (int i = 0; i < numStars; i++) cout << "*"; cout << endl; } int main() { printSpaceStarLine(6, 1); // * printSpaceStarLine(5, 3); // *** printSpaceStarLine(4, 5); // ***** printSpaceStarLine(3, 7); // ******* printSpaceStarLine(2, 9); // ********* printSpaceStarLine(1, 11); // *********** printSpaceStarLine(0, 13); //************* return 0; }

  35. Draw the pyramid void printSpaceStarLine(int numSpaces, int numStars) { for (int i = 0; i < numSpaces; i++) cout << " "; for (int i = 0; i < numStars; i++) cout << "*"; cout << endl; } int main() { int size = 7; for (int i = 0; i < size; i++) printSpaceStarLine(size - 1 - i, 2 * i + 1); return 0; }

  36. Draw the X’mas tree void printSpaceStarLine(int numSpaces, int numStars); int main() { printSpaceStarLine(5, 1); // * printSpaceStarLine(4, 3); // *** printSpaceStarLine(3, 5); // ***** printSpaceStarLine(4, 3); // *** printSpaceStarLine(3, 5); // ***** printSpaceStarLine(2, 7); // ******* printSpaceStarLine(3, 5); // ***** printSpaceStarLine(2, 7); // ******* printSpaceStarLine(1, 9); // ********* printSpaceStarLine(2, 7); // ******* printSpaceStarLine(1, 9); // ********* printSpaceStarLine(0, 11); //*********** printSpaceStarLine(4, 3); // *** printSpaceStarLine(4, 3); // *** return 0; }

  37. Draw the X’mas tree 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; }

  38. Draw the X’mas tree 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() { int size = 4; for (int i = 0; i < 4; i++) printLayer(size + 1 - i, 2 * i + 1); printSpaceStarLine(size, 3); printSpaceStarLine(size, 3); return 0; }

  39. The skeleton code from week 01 #include <iostream> using namespace std; int main() { return 0; } } Why the first two lines?

  40. The skeleton code from week 01 #include <iostream> using namespace std; int main() { return 0; } } Why the first two lines? } #include <iosteam> so that we can use std::cout } Using namespace so that we don’t need to type std:: whenever we have to use cout

  41. ASCII Characters } #include <cctype> } Some functions in this library: } int isalnum(int c); // alphanumeric } int isdigit(int c); // decimal digit } int islower(int c); // lowercase letter } int isupper(int c); // uppercase letter } int isspace(int c); // white-space } int toupper(int c); // convert to uppercase } int tolower(int c); // convert to lowercase } Don’t memorize them!

  42. #include <cctype> example

  43. Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

  44. Global variable } Global variables are those variables declared beyond any functions #include <iostream> int a; void increase() { a++; } int main() { a = 5; increase(); cout << a << endl; return 0; } } What’s the output?

  45. Global variable } Using global variables is discouraged } That is, if there’s a way to avoid global variables, remove them } It is because every function can change global variables. It’s hard to trace why and how the values are changed.

  46. Global variable int a, b; int trickySquare(n); int main() { a = 5; b = trickySquare(a); cout << a << "*" << a << "=" << b << endl; return 0; } } What’s the output?

  47. Global variable int a, b; int trickySquare(n); int main() { a = 5; b = trickySquare(a); cout << a << "*" << a << "=" << b << endl; return 0; } } What’s the output? } Are you sure?

  48. Global variable int a, b; int trickySquare(n) { int result = n * n; a++; return result; } int main() { a = 5; b = trickySquare(a); cout << a << "*" << a << "=" << b << endl; return 0; } } Evil implementation!

  49. Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

  50. Seek for your opinion for (int i = 0; i < 5; i++) A cout << "print 5 lines" << endl; for (int i = 1; i <= 5; i++) B cout << "print 5 lines" << endl; } Correctness: Which one can print 5 lines?

  51. Seek for your opinion for (int i = 0; i < 5; i++) A cout << "print 5 lines" << endl; for (int i = 1; i <= 5; i++) B cout << "print 5 lines" << endl; } Correctness: Which one can print 5 lines? } Which one is more popular?

  52. Array } Array is for storing a sequence of elements } Array declaration: string apple[100]; } Type name[size] } Access an element by index: apple[3]; } Note the range can only be 0 to N-1 if there are N elements

  53. Array

  54. Array int main() { int scores[5]; scores[0] = 10000; scores[1] = 2000; scores[2] = 300; scores[3] = 40; scores[4] = 5; cout << "Sum=" << (scores[0] + scores[1] + scores[2] + scores[3] + scores[4]) << endl; return 0; } } What is the output?

  55. Array int main() { int scores[5]; cout << "Enter student 1's score: "; cin >> scores[0]; cout << "Enter student 2's score: "; cin >> scores[1]; cout << "Enter student 3's score: "; cin >> scores[2]; cout << "Enter student 4's score: "; cin >> scores[3]; cout << "Enter student 5's score: "; cin >> scores[4]; cout << "Sum=" << (scores[0] + scores[1] + scores[2] + scores[3] + scores[4]) << endl; return 0; } } Getting interactive

  56. Array int main() { int scores[5]; for (int i = 0; i < 5; i++) { cout << "Enter student " << (i+1) << "'s score: "; cin >> scores[i]; } cout << "Sum=" << (scores[0] + scores[1] + scores[2] + scores[3] + scores[4]) << endl; return 0; } } Save the repetitions in input section by a loop

  57. Array int main() { int scores[5]; for (int i = 0; i < 5; i++) { cout << "Enter student " << (i+1) << "'s score: "; cin >> scores[i]; } int sum = 0; for (int i = 0; i < 5; i++) sum += scores[i]; cout << "Sum=" << sum << endl; return 0; } } Save the repetitions in input section by a loop } Further use a loop to simplify the processing section

  58. Array } If the index in an array is not within the valid range, the behavior is undefined } For example, a[-1] = 999; } However, sometimes the problem is not that simple: #include <iostream> int main() { int arr[10]; int a = 2; int b = 3; int x = a * b - 7; arr[x] = 1000; return 0; }

  59. Array } If the index in an array is not within the valid range, the behavior is undefined } For example, a[-1] = 999; } However, sometimes the problem is not that simple: int a = 2; int b = 3; int x = a * b - 7; arr[x] = 1000; } C++ doesn’t check the boundary for you. It will run silently. } And it will screw the memory (how?) } That’s why this is one of the most annoying bugs

  60. Array – zero-indexed } From the explanation of how a program accesses the memory through an array, does it give you any hint why C++ array is zero-indexed?

  61. Array – dimensionality } What can be the example of 1D-array? } E.g., int score[100]; } What can be the example of 2D-array? } E.g., int color[100][100]; } How about a 3D-array? } How about an N-D-array?

  62. Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

  63. String / character array } How do I print “Hello” on the screen if single-quote and double-quote are not invented?

  64. String / character array } How do I print “Hello” on the screen if single-quote and double-quote are not invented?

  65. String / character array } How do I print “Hello” on the screen if single-quote and double-quote are not invented? #include <stdio.h> int main() { char str[6]; str[0] = 72; str[1] = 101; str[2] = 108; str[3] = 108; str[4] = 111; str[5] = 0; printf("%s", str); return 0; }

  66. String / character array } How do I print “Hello” on the screen if single-quote and double-quote are not invented? #include <stdio.h> int main() { char str[6]; str[0] = 'H'; str[1] = 'e'; str[2] = 'l'; str[3] = 'l'; str[4] = 'o'; str[5] = 0; printf("%s", str); return 0; }

  67. String / character array } How do I print “Hello” on the screen if single-quote and double-quote are not invented? #include <stdio.h> int main() { char str[6]; str[0] = 'H'; str[1] = 'e'; str[2] = 'l'; str[3] = 'l'; str[4] = 'o'; str[5] = '\0'; printf("%s", str); return 0; }

  68. String / character array } How do I print “Hello” on the screen if single-quote and double-quote are not invented? #include <stdio.h> int main() { char str[6] = "Hello"; printf("%s", str); return 0; }

  69. String / character array } How do I print “Hello” on the screen if single-quote and double-quote are not invented? #include <iostream> using namespace std; int main() { string str = "Hello"; cout << str; return 0; }

  70. String / character array } Change characters in a string #include <iostream> using namespace std; int main() { string str = "HELLO"; str[1] = '3'; str[4] = '0'; cout << str; return 0; }

  71. String / character array } Double quotes are strings (“12345”) } What are single quotes (‘a’, ‘3’, ‘@’)? } Treat them as a symbol for numbers } ‘a’ is 97 } ‘3’ is 51 } ‘@’ is 40

  72. String / character array } Double quotes are strings (“12345”) } What are single quotes (‘a’, ‘3’, ‘@’)? } Treat them as a symbol for numbers } ‘a’ is 97 } ‘3’ is 51 } ‘@’ is 40 } That’s why ‘12’ are invalid – no such symbol! } Escaped characters: } ‘\n’ newline } ‘\a’ used to be beep sound } ‘\t’ tab } ‘\\’ a back-slash character } ‘\0’ null character

  73. Puzzle 2 (warm-up) int main() { int a = 25 + 'Z'; cout << a; }

  74. Puzzle 2 int main() { int a = '-'-'-'; cout << a; }

  75. Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

  76. Overflow } 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 #include <iostream> int main() { int a = 2147483647; a++; cout << a << endl; int b = -2147483648; b--; cout << b << endl; }

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