Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Fundamental Programming: Fundamental Programming Introduction to C++
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Data Types Declarations Expressions Data storage C++ Basics.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Recap……Last Time [Variables, Data Types and Constants]
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Data Types, Variables & Arithmetic
Computing and Statistical Data Analysis Lecture 2
Welcome to Visual Programming using C#
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Basic Elements of C++ Chapter 2.
Lecture 3 Expressions Richard Gesick.
Chapter 2 Variables.
Course websites CS201 page link at my website: Lecture slides
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Expressions and Assignment
Topics Designing a Program Input, Processing, and Output
Fundamental Programming
Summary of what we learned yesterday
Topics Designing a Program Input, Processing, and Output
C++ Programming Basics
Chapter 2 Variables.
Presentation transcript:

Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic operations Assignment operator Today we will look at: Standard input (cin) and output (cout) More data types More complex arithmetic operations More on assignment operator

Announcements HW1 will be assigned this week at SUCourse Due October 7 Wednesday at 17:00 For homework assignments and submissions, we are using SUCourse Homework will be explained in recitations Moreover, there will be important explanations about the homework submission procedure in recitations Submission is a tricky process, please do attend the recitations in order not to make a mistake No late homework without penalty One late day is allowed at cost of 10% of full grade Plagiarism is not tolerated Homeworks are to be done personally we use software tools to detect plagiarized homework first case –100 (minus hundred), second fails the course! detailed policy is on the web site of the course Two Midterm Exams + Final Exam Midterm 1: November 7 th Saturday 14:00 – 16:00 Midterm 2: December 5 th Saturday 10:00 – 12:00

Data Types string used it in previous lectures more technically, string is a class char for single character digits, letters, symbols uses up one byte range why? one byte (8 bits) can store 2 8 = 256 different values. stores the code of the character e.g. 65 for ‘A’ character arithmetic is possible (will see later) char literals are in single quotes (strings are in double quotes ") 'z' 'T' '4' '&' bool boolean (will see later)

Numeric Types to represent integer and real numbers int integer numbers no infinity in computers  limited range 2 or 4 bytes (16 or 32 bits) depending on the computer and compiler you use in our case: 4 bytes (32 bits) integer range: – 32, ,767 for 16-bit computers why? –2,147,483, ,147,483,647 for 32-bit computers why?

Numeric Types (cont’d) short int (or just short ) always 16-bit integer long int (or just long ) always 32-bit integer signed versus unsigned integers you can put signed or unsigned keywords before the type we discussed signed integers signed is default unsigned integers use the same amount of bits but ranges are different 16-bit: ,535 (2 16 –1) 32-bit: ,294,967,295 (2 32 –1)

Numeric Types (cont’d) Real numbers in C++ Real literals e21 Real data types (their difference is in precision) float consumes 4 bytes Range: 0 U [ e– e+38] U [ e– e+38] Tapestry does like float double consumes 8 bytes Range: 0 U [ e– e+308] U [ e– e+308] Standard but a bit complex representation see “floating point representation” item in MSDN Library index:

More on C++ types Check out these items in MSDN Library Index Fundamental types more on C++ types LIMITS.H header file (Microsoft Visual Studio\VC98\Include) limits of integer and char types MSDN help on Integer Limits floating limits (FLOAT.H) limits for the floating points numbers (float, double) MSDN help on Floating Limits floating point representation if you are interested in to learn how the real numbers are represented in computers MSDN help

Arithmetic Operations Operators: + - * / % Operands: values that operator combines variables or literals Combination of operators and operands is called expression Syntax and semantics for arithmetic operations: Addition Subtraction MultiplicationDivisionModulus * 421 / 4 is 5 x + yx * / 4.0 is % 2 is 0 d – d * 23.1 * 4x / 4x % – 3 * 2x / yx % y

Arithmetic Operations (cont’d) Mixed type expressions what if one operator is int other is double? integer is converted into double before operation 5.0 * 8 is / 10 is 0 (integer division) 5.0 / 10 is 0.5 (real division) 10 – 8is 2 (integer) 10 – 8.0 is 2.0 (double)

Expressions with several operators You can use parentheses in expressions to group them Open ( and close ) parentheses should match Rule 1: Parenthesized sub-expressions are evaluated first inside to out Rule 2: Within an expression/subexpression if there are several operators, use operator precedence, evaluate * / % before + - Rule 3: If the operators are in the same expression/subexpression and at the same precedence level, then associativity rule applies evaluate operators from left-to-right Examples (5 - 3 * (7 - 3)) * 8 is / 2 * 6 / 2 + (5 - 3 * (2 - 1)) is 17

Expressions with several operators Are the following expressions equivalent? (40 – 32) * 5 / 9 (40 – 32) * (5 / 9) NO, first one is 4, second one is 0 What about this? (40 – 32.0) * 5 / 9 Operations are double operations. Result is Are these operators sufficient? how to calculate square root? Later we’ll study functions like sqrt, cos, sin, pow, … For complicated mathematical operations that you cannot easily do using basic operators Accessible using #include (or )

Integer vs. Real Real values can be assigned to Integer variables, but this is not recommended since we loose precision int a; double r; r = ; a = r; What is the value of a?  125 Real value is truncated before storing in an integer variable Avoid doing this VS 2010 Compiler warns you but does not give error Be careful when passing arguments to parameters as well passing an integer argument to a double parameter causes the same precision problem

Integer vs. Real (More on Precision) What is the difference between red and blue parts in the following program (fahrcels.cpp)? red: integer arithmetic (low precision) blue: real arithmetic (high precision) int main() { int ifahr; double dfahr; cout << "enter a Fahrenheit temperature "; cin >> ifahr; cout << ifahr << “ F = “ << (ifahr - 32) * 5/9 << " Celsius" << endl; cout << "enter another temperature "; cin >> dfahr; cout << dfahr << “ F = “ << (dfahr - 32) * 5/9 << " Celsius" << endl; return 0; } See Figure 3.4 in the textbook.

Overflow See daysecs.cpp Overflow occurs when the result of an integer expression is outside the limits Run the program with 365 days result: seconds  correct result and output days result: seconds  correct result and output days result: seconds  incorrect result and output due to overflow days result: seconds  incorrect result and output due to overflow

More on Assignment operator (4.1 & 4.3.4) Assignment operator is = to store values in variables variable = expression; first the right hand side expression is evaluated using the current values then the evaluated expression is stored in variable Types should be compatible, otherwise a syntax error may occur (e.g. string variable, integer expression), or precision is lost (e.g. integer variable, real expression) Example: what is the value of a after the assignment? int a, b; b = 25; a = 8; a = b – 3 * a + 2; Answer: 3 A rule: Right hand side expression is evaluated before the assignment operator is executed. If you use the left hand side variable in the right hand side expression as well, then the current value is used in the expression. See Program 4.1 in textbook.

More on Assignment operator (4.1 & 4.3.4) Assigning single expression to several variables variable 1 = variable 2 = variable 3 =... variable n = expression; all variables are assigned the same value of expression example: int x, y, z; x = y = z = 5; x, y and z contain 5 Arithmetic assignment operators += -= *= /= %= Combines arithmetic operation and assignment in one operator variable += expression is the same as variable = variable + expression Example: x += 1 is the same as x = x + 1 Same for -= *= /= and %= x -= 1 x *= 3 x /= 2 and x %= 2