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

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Structure of a C program
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to C Programming
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
String Escape Sequences
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Input & Output: Console
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Announcements Final NEXT WEEK (August 13 th Thursday at 16:00) Recitations will be held on August 12 th Wednesday We will solve sample final questions.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
Lecture #5 Introduction to C++
Chapter 2: Java Fundamentals
Week 1 Algorithmization and Programming Languages.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
2nd Midterm Exam December 19th, 2009, Saturday 09:00 – 11:00 Places: if (LastName
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 CS161 Introduction to Computer Science Topic #8.
C++ for Engineers and Scientists Second Edition
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2: Introduction to C++
Announcements Midterm2 Grades to be announced THIS WEEK
Announcements Final Exam will be on August 19 at 16:00
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 2: Introduction to C++
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
2.1 Parts of a C++ Program.
Announcements Midterm2 Grades to be announced NEXT Monday
Announcements Final will be NEXT WEEK on August 17
Chapter 2: Introduction to C++.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Summary of what we learned yesterday
C++ Programming Basics
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

CS201 – Introduction to Computing – Sabancı University 1 Announcements l Homework 5 ä Due this Wednesday (November 17), 19:00 l 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

CS201 – Introduction to Computing – Sabancı University 2 Character Data Type (9.1) l 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 ( ) See Table F.3 in page 763 for ASCII table Character variables are defined using the type identifier char char a, ch, letter; l Character literals are represented within single quotes 'A' '3' '.' 'f' l 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.

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)

CS201 – Introduction to Computing – Sabancı University 4 Character Arithmetic l 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) l 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)

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'; l 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 uppercase return ch; // otherwise return parameter unchanged }

CS201 – Introduction to Computing – Sabancı University 6 Escape Sequences in C++ l 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\\"; "\ "" \

CS201 – Introduction to Computing – Sabancı University 7 Escape Sequences in C++ l 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' l 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

CS201 – Introduction to Computing – Sabancı University 8 Type Casting l Conversion of an expression to another type ä among built-in types like double, int, char string cannot be used in type casting ä syntax type_name (expression) ä first expression is evaluated, then conversion is done l 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

CS201 – Introduction to Computing – Sabancı University 9 Type Casting Examples l real division using two integers int a, b; double div;... div = double (a) / b; l Conversion from double to int cout << int (3.9); ä double value is truncated ä output is 3 l Conversion from signed to unsigned integers cout << unsigned int (-100); ä output is NOT 100, output is ä the bit sequence to represent –100 is the same as the one of

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 l Complex example: what is the value of the following expression? 3 + double (62 * (int ('C') - int ('A'))) /

CS201 – Introduction to Computing – Sabancı University 11 Cast operators in C++ l Four cast operators in C++, but only one is enough  static_cast ( expression )  functionally not different than type_name (expression) l Example cout (3) / 4; output is 0.75 l Easier to spot casts in the code, but causes longer code

CS201 – Introduction to Computing – Sabancı University 12 Switch statement (Section 7.3.2) l An alternative to if-else, but much less powerful l Useful when you have several choices l 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

CS201 – Introduction to Computing – Sabancı University 13 Switch statement (Section 7.3.2) l 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: invalid case : invalid l Advantage ä more efficient than if-else; only one statement is executed l Disadvantage ä code may become hard to read ä listing all possible values may not be convenient l Better to see in an example

CS201 – Introduction to Computing – Sabancı University 14 Switch example l Input a digit between 0 and 9 and display its name. Display “not a digit” if not between 0 and 9. l 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.

CS201 – Introduction to Computing – Sabancı University 15 Three phases of creating an executable program l Sections from book: 7.2.2, 7.2.3, 7.2.4, l 3 steps ä preprocessing ä compilation ä linking

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 #include s 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

CS201 – Introduction to Computing – Sabancı University 17 Preprocessing l 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. l 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

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 l Example #define LENGTH 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)

CS201 – Introduction to Computing – Sabancı University 19 Compiler l 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 l 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

CS201 – Introduction to Computing – Sabancı University 20 Linker l Linking combines object files and libraries to create a single executable program (the file with.exe extension) l 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

CS201 – Introduction to Computing – Sabancı University 21 END of 2 nd MIDTERM

CS201 – Introduction to Computing – Sabancı University 22 Next l Structs (Section 7.4) l Arrays and vectors (Chapter 8) ä including searching and sorting (partially Chapter 11) l Recursion, scope rules, global and static variables ä partially Chapter 10