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

cs31 discussion 1e
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS31 Discussion 1E

Spring 17’: week 04

TA: Bo-Jhang Ho

bojhang@cs.ucla.edu

Credit to former TA Chelsea Ju

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

Today’s Topic

} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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?

slide-10
SLIDE 10

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

Asset name: It Fashion Jewelry Value (in thousands): 2210 Category: business

slide-11
SLIDE 11

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

Asset name:

slide-12
SLIDE 12

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

Asset name: It Fashion Jewelry

slide-13
SLIDE 13

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

Asset name: It Fashion Jewelry Value (in thousands):

slide-14
SLIDE 14

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

Asset name: It Fashion Jewelry Value (in thousands): 2210

slide-15
SLIDE 15

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

Asset name: It Fashion Jewelry Value (in thousands): 2210

slide-16
SLIDE 16

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

Asset name: It Fashion Jewelry Value (in thousands): 2210 Category:

slide-17
SLIDE 17

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

Asset name: It Fashion Jewelry Value (in thousands): 2210 Category: business

slide-18
SLIDE 18

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()

Asset name: It Fashion Jewelry Value (in thousands):

slide-19
SLIDE 19

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()

Asset name: It Fashion Jewelry Value (in thousands): 2210

slide-20
SLIDE 20

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()

Asset name: It Fashion Jewelry Value (in thousands): 2210 Category:

slide-21
SLIDE 21

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()

Asset name: It Fashion Jewelry Value (in thousands): 2210 Category: business

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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

slide-28
SLIDE 28

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

slide-29
SLIDE 29

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

slide-30
SLIDE 30

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

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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

slide-33
SLIDE 33

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

slide-34
SLIDE 34

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

slide-35
SLIDE 35

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

slide-36
SLIDE 36

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

slide-37
SLIDE 37

// 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

slide-38
SLIDE 38

Today’s Topic

} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

slide-39
SLIDE 39

Good coding style guideline

} (5) Don’t declare two variables whose lifecycles are

  • verlapped
slide-40
SLIDE 40

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?

slide-41
SLIDE 41

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?

slide-42
SLIDE 42

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?

slide-43
SLIDE 43

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?

slide-44
SLIDE 44

Today’s Topic

} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

slide-45
SLIDE 45

What is a function

} Take some numbers, produce a result value

int double string double

slide-46
SLIDE 46

What is a function

} What is f(5)?

f(x) = 2x + 3

slide-47
SLIDE 47

What is a function

} What is f(4, 1)?

f(x, y) = 3x + 2y - xy

slide-48
SLIDE 48

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?

slide-49
SLIDE 49

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.

slide-50
SLIDE 50

Quiz time

} 11 questions on the website

slide-51
SLIDE 51

Why function?

} Save repeating codes } Make the main function more compact

slide-52
SLIDE 52

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

slide-53
SLIDE 53

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

slide-54
SLIDE 54

Have you done the exercise?

* ** *** **** ***** * ** *** **** ***** ***** **** *** ** * ***** **** *** ** *

slide-55
SLIDE 55

New challenges!

* *** ***** ******* ********* *********** ************* * *** ***** *** ***** ******* ***** ******* ********* *********** ********* *********** ************* *** ***

slide-56
SLIDE 56

Draw a shape

} Observation: For each line, we always print some white

spaces, and then some stars, and an endline character.

* *** ***** ******* ********* *********** ************* * *** ***** *** ***** ******* ***** ******* ********* *********** ********* *********** ************* *** ***

slide-57
SLIDE 57

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

slide-58
SLIDE 58

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

slide-59
SLIDE 59

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

slide-60
SLIDE 60

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

slide-61
SLIDE 61

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

slide-62
SLIDE 62

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

slide-63
SLIDE 63

The skeleton code from week 01

} Why the first two lines?

#include <iostream> using namespace std; int main() { return 0; }

slide-64
SLIDE 64

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

slide-65
SLIDE 65

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!

slide-66
SLIDE 66

#include <cctype> example

slide-67
SLIDE 67

Today’s Topic

} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

slide-68
SLIDE 68

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?

slide-69
SLIDE 69

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.

slide-70
SLIDE 70

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

slide-71
SLIDE 71

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

slide-72
SLIDE 72

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

slide-73
SLIDE 73

Today’s Topic

} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

slide-74
SLIDE 74

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;

A B

slide-75
SLIDE 75

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;

A B

slide-76
SLIDE 76

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

slide-77
SLIDE 77

Array

slide-78
SLIDE 78

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

slide-79
SLIDE 79

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

slide-80
SLIDE 80

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

slide-81
SLIDE 81

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

slide-82
SLIDE 82

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

slide-83
SLIDE 83

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;

slide-84
SLIDE 84

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?

slide-85
SLIDE 85

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?

slide-86
SLIDE 86

Today’s Topic

} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

slide-87
SLIDE 87

String / character array

} How do I print “Hello” on the screen if single-quote and

double-quote are not invented?

slide-88
SLIDE 88

String / character array

} How do I print “Hello” on the screen if single-quote and

double-quote are not invented?

slide-89
SLIDE 89

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

slide-90
SLIDE 90

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

slide-91
SLIDE 91

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

slide-92
SLIDE 92

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

slide-93
SLIDE 93

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

slide-94
SLIDE 94

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

slide-95
SLIDE 95

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

slide-96
SLIDE 96

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

slide-97
SLIDE 97

Puzzle 2 (warm-up)

int main() { int a = 25 + 'Z'; cout << a; }

slide-98
SLIDE 98

Puzzle 2

int main() { int a = '-'-'-'; cout << a; }

slide-99
SLIDE 99

Today’s Topic

} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

slide-100
SLIDE 100

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

slide-101
SLIDE 101

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() { for (unsigned int i = 3; i >= 0; i--) cout << "Print one line" << endl; return 0; }

} How many lines are going to be printed?

slide-102
SLIDE 102

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

} Will double variable overflow?

slide-103
SLIDE 103

Today’s Topic

} Quick discussion about Project 2 } Coding style part 2 } Function } Global variable } Array } Character / String } Overflow } Debugger

slide-104
SLIDE 104

Debugger

} Demo time