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
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
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
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
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
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
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
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
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
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
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
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
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
Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger
Good coding style guideline } (5) Don’t declare two variables whose lifecycles are overlapped
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?
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?
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?
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?
Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger
What is a function int double double string } Take some numbers, produce a result value
What is a function f(x) = 2x + 3 } What is f(5)?
What is a function f(x, y) = 3x + 2y - xy } What is f(4, 1)?
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?
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.
Quiz time } 11 questions on the website
Why function? } Save repeating codes } Make the main function more compact
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; }
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
Have you done the exercise? * * ** ** *** *** **** **** ***** ***** ***** ***** **** **** *** *** ** ** * *
New challenges! * *** ***** *** * ***** *** ******* ***** ***** ******* ******* ********* ********* *********** *********** ************* ********* *********** ************* *** ***
Draw a shape } Observation: For each line, we always print some white spaces, and then some stars, and an endline character. * *** ***** *** * ***** ******* *** ***** ***** ******* ******* ********* ********* *********** *********** ********* ************* *********** ************* *** ***
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; }
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; }
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; }
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; }
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; }
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; }
The skeleton code from week 01 #include <iostream> using namespace std; int main() { return 0; } } Why the first two lines?
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
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!
#include <cctype> example
Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger
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?
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.
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?
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?
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!
Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger
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?
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?
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
Array
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?
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
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
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
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; }
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
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?
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?
Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger
String / character array } How do I print “Hello” on the screen if single-quote and double-quote are not invented?
String / character array } How do I print “Hello” on the screen if single-quote and double-quote are not invented?
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; }
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; }
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; }
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; }
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; }
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; }
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
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
Puzzle 2 (warm-up) int main() { int a = 25 + 'Z'; cout << a; }
Puzzle 2 int main() { int a = '-'-'-'; cout << a; }
Today’s Topic } Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger
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; }
Recommend
More recommend