Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.

Slides:



Advertisements
Similar presentations
1 C Fundamentals - I Math 130 Lecture # 2 B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Sp05: With discussion.
Advertisements

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.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CS150 Introduction to Computer Science 1
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
// A simple C++ program # include using namespace std; int main () { cout
Data types and variables
How Create a C++ Program. #include using namespace std; void main() { cout
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Basic Elements of C++ Chapter 2.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
Input & Output: Console
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 2 part #4 Operator
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
CPS120: Introduction to Computer Science Operations Lecture 9.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
CSC 107 – Programming For Science. The Week’s Goal.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
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,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Variables Symbol representing a place to store information
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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 3 Control Statements
ITEC113 Algorithms and Programming Techniques
Fundamental Data Types
Introduction to C++ Programming
Chapter 2 Variables.
C++ Data Types Data Type
CS150 Introduction to Computer Science 1
Fundamental Data Types
Presentation transcript:

Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point Write a program

Rules of precedence for Arithmetic Operations OperatorOperationOrder of evaluation ( )ParenthesesEvaluated first. If the parentheses are nested the innermost pair is evaluated first. If there are several pairs on the same level they are evaluated left to right. * / %Division, mulitiplication, modulus Evaluated second. If there are several they are evaluated left to right. + -Addtition, subtractionEvaluated last. If there are several, they are evaluated left to right

Example - precedence for arithmetic operations algebra: m = a + b + c + d + e C++: m = (a + b + c + d + e) / 5; what if parentheses missing? m = a + b + c + d + e -- 5 algebra: m = mx + b C++: m = m * x + b;

Exercises 1) int a, x, b, c, y; a = 2; x = 5; b = 3; c = 7; y = a * x * x + b * x + c; What is y after this program executes? 2) Evaluate the following c++ expressions: x = * 6 / 2 - 1; y = 2 % * 2 - 2/2; z = (3 * 9 * (3 + (9 * 3/ (3))));

Integer Data types Size and range differ from system to system. Typical: short (2 bytes) unsigned short 0 to signed short to int (4 bytes) unsigned int 0 to signed int to long (4 bytes) unsigned long 0 to signed long to int is shorthand for signed int same operations valid for all why so many? conserve memory. Database of all US citizens, age, social security #.

Determining Size and Range // Determining the size and range of the the signed long integer type. #include using namespace std; int main() { cout << "signed long type is " << sizeof (signed long) << "bytes\n"; cout << "largest value of signed long type is " << LONG_MAX << endl; cout << "smallest value of signed long type is " << LONG_MIN << endl; return 0; } page 45 of book gives other constants. Find out size and range for WPI.

Floating point Data Type Algebraically: number with fractional part x x mantissa (1.5) exponent (-2) mantissa (1.2) exponent (3) sign bit 8 bits exponent23 bits mantissa Implementation: Varies from system to system. Typical:

floating point data type C++: Use when: fractions too big for integer (1 x 10 to the 100th) Precision - number of places to right of decimal point. Limited by size of mantissa

Floating Point Data Types TypeTypical rangeTypical precision float10 e -38 to 10 e 38 6 digits double10 e -308 to 10 e digits long double10 e to 10 e digits

floating point data type implementations differ. Typical double always >= float long double always >= double Constants 3.14 (double) 3.14L (long double) 3.14f (float) float f1 = 3.14; //compiler error

Mixing float and integer types #include using namespace std; int main() { float average; int numberOfGrades = 2; int totalOfGrades = 9; average = totalOfGrades / numberOfGrades; cout << "The average is " << average << endl; return 0; }

mixing float and integer could change totalOfGrades to float to fix. Compiler will promote numberOfGrades Could use cast operator to change totalOfGrades temporarily average = (float) totalOfGrades/ numberOfGrades;

Things to remember about floating point data types Mantissa is limited in size so value may not be exact (1/3 = ) Arithmetic is faster using integer types than floating point types Don’t use equality operators Modulus operator not valid for floating point Don’t use floating point #’s for loop control

Exercises 1) create a floating point variable and initialize it to the value ) create a floating point variable and initialize it to the value 1.6 times 10 to the 12th power 3) Why would the compiler complain about this statement? float f = 32.56; 4) Suppose you have a floating point number and you want to separate the whole part from the fractional part so that you have two integer values. One contains the whole part and one contains the fractional part ie > 5, 77. How would you do it?

Homework Use cin to read in a value double total; cin >> total; To print floating point using cout cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout << setprecision (2);

char data type ASCII set see book 623 A is 65 B is 66 regularity. A + 1 = B A < B is true initialize with ‘ char ch1 = ‘A’; // stores whole number 65 in storage cell named ch1 char ch1; ch1 one byte

char data type routines treat char differently than int char ch1 = ‘A’; int int1 = 65; cout << “ch1 is << ch1 << endl; //displays “ch1 is A” cout << “int1 is “ << int1 << endl; //displays “ch1 is 65” char is shorthand for either unsigned char (0 to 255) or signed char (-127 to 127) varies from system. What is it on ours?

Data Types Exercises Given the following declarations, determine the value of the variable on the left-hand-side of each assignment statement. int int1, int2, int3; float f1=1.0f, f2=2.5f, f3=5.0f; double d1, d2; char ch1, ch2; int1 = 5; d2 = 5.0; ch1 = '5'; int3 = f2; int2 = int1 / int3; d1 = f3 - -f1 * 6 / int * (int1 - d2); ch2 = ch1 - 2; int3 = 'a' - 'A'; ch1 = 'W' + int3;

Operations Arithmetic Relational (a < b) Logical (a && b)

Relational operators Used to compare values result of relational expression is true or false a = 1, b = 2 a == b false a < b true a > b false Used to change execution order of statements

Relational Operators Standard algebraic expression C++ operatorExample of C++ expression Meaning >>a > ba is greater than b <<a < ba is less than b >=a >=ba is greater than or equal to b <=a <= ba is less than or equal to b ===a == ba is equal to b !=a != ba is not equal to b

Slide relational operators (cont) precedence = > higher than == and != a y is the same as (a y) arithmetic operators higher precedence than relational if x = 2, y = 3 y == 2 * x + 3 evaluated as follows: 2 * x (4) (7) 3 == 7? (false)

Common programming errors Compiler will generate an error if the operators ==, !=, >=, or <= have whitespace Cannot reverse => not same as >= using = instead of == common mistake m== 4 desired. m = 4 done. do 4 == m

relational operators exercises Identify which of the following expressions are true: Assume a = 0, b = 1, x = 2, y = 3 1) a == b + x 2) b + x == y 3) x - b != y 4) a * b + x * y != x * y 5) a <= b != x <= y

Logical Operators OperatorNameExampleTrue if !Not!xx is false &&Andx && yx and y are both true ||Orx || yx or y is true

Logical Operators Precedence ! && || !a && b same as (!a) && b Relational operators have higher precedence than the && and || operators a d same as (a d)

OperatorType ()parentheses !not * / %multiplication, division, modulus + -addition, subtraction =relationals == !=equality &&and ||or Precedence chart

Precedence Exercises Exercises 1) Assume a = 1, b = 2, c = 4, d =3 is following expression true or false? a d 2) Create C++ expression a c, or x > y and > z