CS31 Discussion 1E
Spring 17’: week 04
TA: Bo-Jhang Ho
bojhang@cs.ucla.edu
Credit to former TA Chelsea Ju
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
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 << (i / j) * (j / i); cout << endl; } return 0; }
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 << (i / j) * (j / i); cout << endl; } return 0; }
Today’s Topic
} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger
Problems I observed in Project 2
} Blank lines in between if / else-if / else } Why do we need cin.ignore(10, ‘\n’); ?
} Can we move this line around?
} Use const
double value; string name; ... // value below 1000 (including) // only need to apply 1.3% if (value >= 0 && value <= 1000) { fee = 0.013 * value * 1000; cout << "The trustee fee for " << name << " is $" << fee << "."; } // value is between 1000 to 10000 (including) // 1% more in this category else if (value > 1000 && value <= 10000) { fee = (((1000 * 0.013) + ((value - 1000) * 0.010))) * 1000; cout << "The trustee fee for " << name << " is $" << fee << "."; } // value is over 10000 // 0.9% more in this catetory else (value > 10000) { ...
Project 2 – Logic segment
} If / else if / else block are too separate
double value; string name; ... // value below 1000 (including) // only need to apply 1.3% if (value >= 0 && value <= 1000) { fee = 0.013 * value * 1000; cout << "The trustee fee for " << name << " is $" << fee << "."; } // value is between 1000 to 10000 (including) // 1% more in this category else if (value > 1000 && value <= 10000) { fee = (((1000 * 0.013) + ((value - 1000) * 0.010))) * 1000; cout << "The trustee fee for " << name << " is $" << fee << "."; } // value is over 10000 // 0.9% more in this catetory else (value > 10000) { ...
Project 2 – Logic segment (update 1)
} Let’s put all blocks together
} They should be considered as one entity
double value; string name; ... if (value >= 0 && value <= 1000) { // value below 1000 (including) // only need to apply 1.3% fee = 0.013 * value * 1000; cout << "The trustee fee for " << name << " is $" << fee << "."; } else if (value > 1000 && value <= 10000) { // value is between 1000 to 10000 (including) // 1% more in this category fee = (((1000 * 0.013) + ((value - 1000) * 0.010))) * 1000; cout << "The trustee fee for " << name << " is $" << fee << "."; } else (value > 10000) { // value is over 10000 // 0.9% more in this catetory ...
Project 2 – Logic segment (update 2)
} Let’s put all blocks together
} They should be considered as one entity } We also don’t like comments which separate if blocks
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – Input segment
} Why do we need cin.ignore()?
} What is ‘\n’? } Where can we find newline characters?
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – cin/cout demystified
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – cin/cout demystified
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – cin/cout demystified
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – cin/cout demystified
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – cin/cout demystified
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – cin/cout demystified
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – cin/cout demystified
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – cin/cout demystified
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – If we don’t use cin.ignore()
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – If we don’t use cin.ignore()
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – If we don’t use cin.ignore()
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – If we don’t use cin.ignore()
string name; double value; string category; cout << "Asset name: "; getline(cin, name); cout << "Value (in thousands): "; cin >> value; cin.ignore(10000, '\n'); cout << "Category: "; getline(cin, category);
Project 2 – If we don’t use cin.ignore()
} If we add one more cin.ignore() line before the first cout,
is it correct?
Asset name: It Fashion Jewelry Value (in thousands): 2210 Category: business
One more example
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
Enter first number: 11 Enter second number: 22 Enter third number: 33
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; }
One more example – put a lot spaces
Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=66
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; }
One more example – put a lot spaces
Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=66
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; }
One more example – put a lot spaces
Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=66
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; }
One more example – put a lot spaces
Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=66
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; }
One more example – put a lot spaces
Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=66
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; }
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
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 numbers in one line
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
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 – Put a floating number
One more example – If we change the type to string?
Enter first number: 11 Enter second number: 22 Enter third number: 33 11+22+33=112233
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; }
// 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; ...
Project 2 – Use const
} 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
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
} Take some numbers, produce a result value
int double string double
What is a function
} What is f(5)?
What is a function
} 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
} Why the first two lines?
#include <iostream> using namespace std; int main() { return 0; }
The skeleton code from week 01
} 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
#include <iostream> using namespace std; int main() { return 0; }
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
#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
} What’s the output?
int a, b; int trickySquare(n); int main() { a = 5; b = trickySquare(a); cout << a << "*" << a << "=" << b << endl; return 0; }
Global variable
} What’s the output? } Are you sure?
int a, b; int trickySquare(n); int main() { a = 5; b = trickySquare(a); cout << a << "*" << a << "=" << b << endl; return 0; }
Global variable
} Evil implementation!
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; }
Today’s Topic
} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger
Seek for your opinion
} Correctness: Which one can print 5 lines?
for (int i = 0; i < 5; i++) cout << "print 5 lines" << endl; for (int i = 1; i <= 5; i++) cout << "print 5 lines" << endl;
Seek for your opinion
} Correctness: Which one can print 5 lines? } Which one is more popular?
for (int i = 0; i < 5; i++) cout << "print 5 lines" << endl; for (int i = 1; i <= 5; i++) cout << "print 5 lines" << endl;
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
} What is the output?
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; }
Array
} Getting interactive
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; }
Array
} Save the repetitions in input section by a loop
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; }
Array
} Save the repetitions in input section by a loop } Further use a loop to simplify the processing section
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; }
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: } 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
int a = 2; int b = 3; int x = a * b - 7; arr[x] = 1000;
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 } ‘\t’ tab } ‘\0’ null character } ‘\a’ used to be beep sound } ‘\\’ a back-slash 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
unsigned int 4 32 0 to 4,294,967,295 double 8 64
#include <iostream> int main() { int a = 2147483647; a++; cout << a << endl; int b = -2147483648; b--; cout << b << endl; }
Overflow
} Basic data types
Type Bytes Bits Value range int 4 32
unsigned int 4 32 0 to 4,294,967,295 double 8 64
#include <iostream> int main() { for (unsigned int i = 3; i >= 0; i--) cout << "Print one line" << endl; return 0; }
} How many lines are going to be printed?
Overflow
} Basic data types
Type Bytes Bits Value range int 4 32
unsigned int 4 32 0 to 4,294,967,295 double 8 64
} Will double variable overflow?
Today’s Topic
} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger
Debugger
} Demo time