CS201 Introduction to Computing Sabancı University 1 Announcements l Homework 5 Due this...

22
CS201 – Introduction to Computing – Sabancı University 1 Announcements Homework 5 Due this Wednesday (November 17), 19:00 Common Problems and Questions about HW5 Monsters move randomly They do not hit the walls They turn to random directions until no wall Be aware of the flow of the game Read the document for all details Do not use TurnRight in Random Turn member functions Causes the monsters to move slowly Instead Turn the robot to a random direction by updating the private data member directly. Number of things in the playground is not a constant value In the grading process, we will use a different playground

description

CS201 – Introduction to Computing – Sabancı University 3 Character Codes in ASCII set l Each character occupies one byte ä that is why character codes are between 0 and 255 l first 32 characters (with codes ) are non-printable control characters ä such as eof l blank character has the code 32 l Uppercase letters are consecutive and ordered in ASCII set  code for 'A' is 65, 'B' is 66, 'C' is 67, … 'Z' is 90 l Similarly, lowercase letters are also ordered and consecutive  code for 'a' is 97, … 'z' is 122 l Similarly digit characters are ordered and consecutive too  code for '0' is 48, '1' is 49, … '9' is 57 l Do not memorize the codes for letters and digits; you can do character arithmetic (see next slides)

Transcript of CS201 Introduction to Computing Sabancı University 1 Announcements l Homework 5 Due this...

Page 1: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 1

Announcements Homework 5

Due this Wednesday (November 17), 19:00 Common Problems and Questions about HW5

Monsters move randomly• They do not hit the walls• They turn to random directions until no wall

Be aware of the flow of the game• Read the document for all details

Do not use TurnRight in Random Turn member functions• Causes the monsters to move slowly• Instead Turn the robot to a random direction by updating the

private data member directly. Number of things in the playground is not a constant value

• In the grading process, we will use a different playground

Page 2: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 2

Character Data Type (9.1) is a built-in data type to represent a single character from the computer’s

character set most commonly used standard character set is ASCII letters, digits, symbols, punctuation marks, control characters (e.g.

end of line,end of file, etc.) each character in ASCII set has a numeric code (0 .. 255)

• See Table F.3 in page 763 for ASCII table Character variables are defined using the type identifier char

char a, ch, letter; Character literals are represented within single quotes

'A' '3' '.' 'f' Pay attention to the difference between strings with single letter and chars

"A" versus 'A' first one is a string literal, second one is a character literal String variables (objects) has several private data members even if it

has a single character in it. However, char variables occupy just one byte.

Page 3: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 3

Character Codes in ASCII set Each character occupies one byte

that is why character codes are between 0 and 255 first 32 characters (with codes 0 .. 31) are non-printable control

characters such as eof

blank character has the code 32 Uppercase letters are consecutive and ordered in ASCII set

code for 'A' is 65, 'B' is 66, 'C' is 67, … 'Z' is 90 Similarly, lowercase letters are also ordered and consecutive

code for 'a' is 97, … 'z' is 122 Similarly digit characters are ordered and consecutive too

code for '0' is 48, '1' is 49, … '9' is 57 Do not memorize the codes for letters and digits; you can do

character arithmetic (see next slides)

Page 4: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 4

Character Arithmetic If you compare two characters (or a character with an integer)

character codes are used in comparison If you apply an arithmetic operator (+ - * / %) to a

character, integer code of the character is processed this is an implicit type casting (conversion)

And if you process the result of such an operation as character, a reverse conversion is automatically performed you can also process the result as integer (actually this is

the default behavior) Example: the value of 'A' + 2 is

'C' if you process as a char (you do not have to know the codes in order to reach this result)

67 if you process as integer or if you do not mean any type Example: what is the value of 'Z' - 'A' ?

25 (you do not have to know the codes of A and Z to answer this question)

Page 5: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 5

Examples Suppose digitch is a char variable and its content is a digit

character (between '0' and '9'), how can you obtain that digit character’s numeric equivalent in an int variable digitnum?digitnum = digitch - '0';

Write a function that takes a character parameter and returns the uppercase equivalent of it if parameter is a lowercase letter. If parameter is not a lowercase letter, function returns it unchanged.

char toupper (char ch){

if (ch >= 'a' && ch <= 'z') //if lowercase return ch + ('A' - 'a'); //return its uppercasereturn ch; // otherwise return parameter unchanged

}

Page 6: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 6

Escape Sequences in C++ Special symbols for some characters (mostly for control

characters) to be used in character and string literals only. Not to be used while entering input Full list is in Table 4.1 (page 103) or Table A.5 (page 716) Some escape sequences

\n newline character\t tab (used for aligned output)\a bell\" double quote\' single quote\\ backslash

Example follows. What is the output?cout << "\"\\\n\"\"\n\\";

"\""\

Page 7: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 7

Escape Sequences in C++ If you want to represent a nonprintable character (such as new line,

bell) in a string or character literal, you have to use nonprintable escape sequences."bla bla \n bla"'\n'

However, for printable ones, you may use escape sequences whenever needed You may or may not use escape sequence to represent a double

quote character in a char literal• That means '\"' and '"' are the same

You may or may not use escape sequence to represent a single quote character in a string literal

• That means "bla \' bla" and "bla ' bla" are the same However, you have to represent a double quote in a string literal

as an escape sequence. Similarly, you have to represent a single quote in a char literal as an escape sequence.

To represent a backslash, you always have to use escape sequence in both char and string literals

Page 8: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 8

Type Casting Conversion of an expression to another type

among built-in types like double, int, char • string cannot be used in type casting

syntaxtype_name (expression)

first expression is evaluated, then conversion is done Examples

cout << double (3) / 4;• output is 0.75• first 3 is converted into double (3.0), then divided by 4

cout << 3 / 4;• without casting, integer division, output is 0

cout << double (3 / 4);• first 3 / 4 is evaluated to 0, then result is converted• output is 0

Page 9: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 9

Type Casting Examples real division using two integers

int a, b;double div;...div = double (a) / b;

Conversion from double to intcout << int (3.9); double value is truncated output is 3

Conversion from signed to unsigned integerscout << unsigned int (-100); output is NOT 100, output is 4294967196 the bit sequence to represent –100 is the same as the

one of 4294967196

Page 10: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 10

Type Casting Examples Conversion from int to char

int i = 72;cout << char (i);

• output is H• it is the character with code 72

Complex example: what is the value of the following expression?3 + double (62 * (int ('C') - int ('A'))) / 5

2

124

124.0

24.8

27.8

Page 11: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 11

Cast operators in C++ Four cast operators in C++, but only one is enough

static_cast < type_name> (expression) functionally not different than type_name (expression)

Examplecout << static_cast <double> (3) / 4;

• output is 0.75 Easier to spot casts in the code, but causes longer code

Page 12: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 12

Switch statement (Section 7.3.2) An alternative to if-else, but much less powerful Useful when you have several choices Syntax

switch (selector_expression){ case label1: statement list; break; case label2: statement list; break;... default: statement list;}

•First selector is evaluated•labels are searched. If the value of selector is listed, then execution jumps there•all statements are executed until break•if no label bears the value of selector, statements after default are executed

Page 13: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 13

Switch statement (Section 7.3.2) Restrictions

selector expression must be integer, char or boolean, cannot be real (double, float) or string or something else

case labels must be given in literals (constant values)• no expressions

case a*2: invalid• no ranges or lists

case 1, 2, 3: invalidcase 1 .. 10: invalid

Advantage more efficient than if-else; only one statement is executed

Disadvantage code may become hard to read listing all possible values may not be convenient

Better to see in an example

Page 14: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 14

Switch example Input a digit between 0 and 9 and display its name. Display

“not a digit” if not between 0 and 9. See displaydigit.cpp

CAUTION: Do not forget break after each statement list, otherwise execution unconditionally continues until the next break or the end of the switch statement.

Page 15: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 15

Three phases of creating an executable program

Sections from book: 7.2.2, 7.2.3, 7.2.4, 7.2.5 3 steps

preprocessing compilation linking

Page 16: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 16

Preprocessing The preprocessor processes all #include directives (and other preprocessor

commands – all preprocessor commands start with #) creates a translation unit which is the input of the next step (compilation) all #include’d files are pasted in the translation unit recursively

• e.g. replaces #include "foo.h" with contents of file foo.h, and does this recursively, for all #includes that foo.h includes and so on

exceptions• there are some preprocessor directives that causes conditional preprocessing

#ifndef _FOO_H //if _FOO_H is not defined#define _FOO_H //define it and … header file for Foo goes here //include header file#endif

if _FOO_H is not defined, then it is defined and file is included, otherwise file is not included

• Definitions are in a global symbol table prevents multiple inclusion of the same header file in a program

Page 17: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 17

Preprocessing Where are the include files located?

standard ones are in INCLUDE directory under ....\Microsoft Visual Studio 10.0\VC\include

Project project name Properties Configuration Properties VC++ Directories

• You can specify more directories to search for header files here, but this only affects the current project.

• It is possible to add more include file folders that affect all projects; but the method for it is too complex for CS201.

You get an error if the include file is not found in these directories adding an include file to the project is not sufficient

The folder that you installed Visual Studio

Page 18: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 18

Preprocessing #define identifier token

Another compiler directive Generally written at the beginning of program (not in a

function) or in a header file All instances of identifier in all functions are replaced with

token during preprocessing• Token is any character sequence

Example#define LENGTH 10...cout << LENGTH << endl; Displays 10 on screen The above statement is compiled as

cout << 10 << endl; Because LENGTH is replaced with 10 before compilation

(during preprocessing)

Page 19: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 19

Compiler The compiler has a translation unit as input and produces

compiled object code as output The object code is platform/architecture specific the source code is (in theory at least) the same on all

platforms We may need to compile several cpp files

dice.cpp, randgen.cpp, prompt.cpp, robots.cpp, … because some functions and class implementations are

in those files• if you are using them in your program, you have to compile

them together with your main cpp file• add to the same project

you may generate libraries out of those files• ready-to-use object code• need not to compile, but add the .lib file to the project

Page 20: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 20

Linker Linking combines object files and libraries to create a

single executable program (the file with .exe extension) May create errors even if compilation is successful

unresolved external symbol ... undefined Symbol ... Primary reason would be missing libraries or

implementation files (like date.cpp, prompt.cpp)• header files provide the definition, but body is missing!

If you have such errors, try to find where those symbols are implemented and include those libs or cpps in your project

Page 21: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 21

END of 2nd MIDTERM

Page 22: CS201  Introduction to Computing  Sabancı University 1 Announcements l Homework 5  Due this Wednesday (November 17), 19:00 l Common Problems and Questions.

CS201 – Introduction to Computing – Sabancı University 22

Next Structs (Section 7.4) Arrays and vectors (Chapter 8)

including searching and sorting (partially Chapter 11) Recursion, scope rules, global and static variables

partially Chapter 10