Exposure C++ Chapter VI Data Type Operations C++ Integer Operations Symbols +Addition -Subtraction *Multiplication /Integer division %Modulus or Remainder.

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
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.
Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Lecture 1 The Basics (Review of Familiar Topics).
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Data types and variables
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Operaciones y Variables
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Exposure C++ Chapter VII Program Input and Output.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
A Note About Projects It is possible that you have prior experience with IDEs that require the creation of a project before any program work can start.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Exposure C++ Chapter V Variables and Constants The Limitation of Constants // PROG0501.CPP // This program demonstrates using four constants. #include.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CPS120: Introduction to Computer Science Operations Lecture 9.
CSC 107 – Programming For Science. The Week’s Goal.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
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.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
What are Operators? Some useful operators to get you started.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
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.
Chapter Topics The Basics of a C++ Program Data Types
PowerPoint Presentation Authors of Exposure Java
Basic Elements of C++.
PowerPoint Presentation Authors of Exposure Java
Chapter 2 Basic Computation
Basic Elements of C++ Chapter 2.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Data Types, Concatenation, PEMDAS, Shortcuts, and Comments
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Summary of what we learned yesterday
C++ Programming Basics
Presentation transcript:

Exposure C++ Chapter VI Data Type Operations

C++ Integer Operations Symbols +Addition -Subtraction *Multiplication /Integer division %Modulus or Remainder Division

// PROG0601.CPP // This program demonstrates integer operations. #include void main() { int Nr1 = 100; int Nr2 = 30; int Result1, Result2, Result3, Result4, Result5; Result1 = Nr1 + Nr2; Result2 = Nr1 - Nr2; Result3 = Nr1 * Nr2; Result4 = Nr1 / Nr2; Result5 = Nr1 % Nr2; cout << Nr1 << " + " << Nr2 << " = " << Result1 << endl; cout << Nr1 << " - " << Nr2 << " = " << Result2 << endl; cout << Nr1 << " * " << Nr2 << " = " << Result3 << endl; cout << Nr1 << " / " << Nr2 << " = " << Result4 << endl; cout << Nr1 << " % " << Nr2 << " = " << Result5 << endl; } PROG0601.CPP OUTPUT = = * 30 = / 30 = % 30 = 10

Integer Division Examples 12 / 3=4 12 / 4=3 12 / 5=2 12 / 8=1 12 / 12=1 12 / 15=0 0 / 12=0 12 / 0=undefined

Modulus Division Examples 12 % 3=0 12 % 4=0 12 % 5=2 12 % 8=4 12 % 12=0 12 % 15=12 0 % 12=0 12 % 0=undefined

Flashback To Elementary School Long Division

// PROG0602.CPP // This program demonstrates incrementers and decrementers. #include void main() { int Nr1 = 100; int Nr2 = 30; cout << "Nr1 = " << Nr1 << endl; cout << "Nr2 = " << Nr2 << endl; Nr1++; // postfix incrementer ++Nr2; // prefix incrementer cout << "Nr1 = " << Nr1 << endl; cout << "Nr2 = " << Nr2 << endl; Nr1--; // postfix decrementer --Nr2; // prefix decrementer cout << "Nr1 = " << Nr1 << endl; cout << "Nr2 = " << Nr2 << endl; } PROG0602.CPP OUTPUT Nr1 = 100 Nr2 = 30 Nr1 = 101 Nr2 = 31 Nr1 = 100 Nr2 = 30

Incrementer and Decrementer Shortcuts K++ is a shortcut for K = K + 1; ++K is a shortcut for K = K + 1; K-- is a shortcut for K = K - 1; --K is a shortcut for K = K - 1;

// PROG0603.CPP // This program demonstrates the difference between // the X++ and ++X incrementers. #include void main() { int X1,X2; X1 = X2 = 10; cout << X1 << " " << X2 << endl; cout << ++X1 << " " << X2++ << endl; cout << X1 << " " << X2 << endl; } PROG0603.CPP OUTPUT

Basic Operation Shortcuts No Shortcut Notation Shortcut Notation K = K + 5K += 5 K = K - 5K -= 5 K = K * 5K *= 5 K = K / 5K /= 5 K = K % 51K %= 5

// PROG0604.CPP // This program demonstrates using shortcut notation with // each one of the 5 basic operations. #include // Necessary for program input/output void main() { int Nr = 100; cout << "Nr " << Nr << endl << endl; Nr += 10; cout << "Nr += 10 " << Nr << endl << endl; Nr -= 20; cout << "Nr -= 20 " << Nr << endl << endl; Nr *= 2; cout << "Nr *= 2 " << Nr << endl << endl; Nr /= 5; cout << "Nr /= 5 " << Nr << endl << endl; Nr %= 7; cout << "Nr %= 7 " << Nr << endl << endl; } PROG0604.CPP OUTPUT Nr 100 Nr += Nr -= Nr *= Nr /= 5 36

The Danger of Combining Shortcuts // PROG0605.CPP // This program demonstrates complexity caused // by combining incrementers. #include void main() { int X = 10; ++X += X++; cout << "X = " << X << endl << endl; } PROG0605.CPP OUTPUT X = 23

// PROG0606.CPP // This program shows complexity with combining // incrementers. #include void main() { int X = 10; ++X += (X X); cout << "X = " << X << endl << endl; } The output is intentionally not shown. This is because it can be different on different computers. Also, if you move int x=10; before the void main(), it can actually change the answer. The point is do not combine shortcuts.

C++ Shortcut Warning C++ shortcut operations should not be combined with any other type of operation or any other type of statement. The results of many combinations is non-standard. It can fluctuate with different compilers and results are unpredictable. Proper Usage: K++; cout << K << endl; Problematic Usage: cout << K++ << endl;

C++ Real Number Operations +Addition -Subtraction *Multiplication /Real number division (no remainder)

// PROG0607.CPP // This program demonstrates real number operations implemented // with the float type. #include void main() { float Nr1 = 1000; float Nr2 = 3.3; float Result1, Result2, Result3, Result4; Result1 = Nr1 + Nr2; Result2 = Nr1 - Nr2; Result3 = Nr1 * Nr2; Result4 = Nr1 / Nr2; cout << Nr1 << " + " << Nr2 << " = " << Result1 << endl; cout << Nr1 << " - " << Nr2 << " = " << Result2 << endl; cout << Nr1 << " * " << Nr2 << " = " << Result3 << endl; cout << Nr1 << " / " << Nr2 << " = " << Result4 << endl; } PROG0607.CPP OUTPUT = = * 3.3 = / 3.3 =

// PROG0608.CPP // This program demonstrates real number operations // implemented with the double type. #include void main() { double Nr1 = 1000; double Nr2 = 3.3; double Result1, Result2, Result3, Result4; Result1 = Nr1 + Nr2; Result2 = Nr1 - Nr2; Result3 = Nr1 * Nr2; Result4 = Nr1 / Nr2; cout << Nr1 << " + " << Nr2 << " = " << Result1 << endl; cout << Nr1 << " - " << Nr2 << " = " << Result2 << endl; cout << Nr1 << " * " << Nr2 << " = " << Result3 << endl; cout << Nr1 << " / " << Nr2 << " = " << Result4 << endl; } PROG0608.CPP OUTPUT = = * 3.3 = / 3.3 =

Mathematical Precedence Parentheses Exponents Multiplication & Division Addition & Subtraction

Hidden Operators in Mathematics MathematicsC++ Source Code 5XY5*X*Y 4X + 3Y4*X + 3*Y 6(A + B)6 * (A + B) A + B(A + B) / (A - B) A - B

// PROG0609.CPP // This program demonstrates math precedence in C++. #include void main() { double A,B,C, Result; A = 1000; B = 100; C = 2.5; cout << "A = " << A << " B = " << B << " C = " << C << endl << endl; Result = A + B * C; cout << "A + B * C = " << Result << endl; Result = (A + B) * C; cout << "(A + B) * C = " << Result << endl; Result = A / B * C; cout << "A / B * C = " << Result << endl; Result = A * B / C; cout << "A * B / C = " << Result << endl; } PROG0609.CPP OUTPUT A = 1000 B = 100 C = 2.5 A + B * C = 1250 (A + B) * C = 2750 A / B * C = 25 A * B / C = 40000

// PROG0610.cpp // This program demonstrates that characters are stored as numbers. #include void main() { cout << "'a' - 'A' = " << 'a' - 'A' << endl; cout << "'b' - 'B' = " << 'b' - 'B' << endl; cout << "'z' - 'Z' = " << 'z' - 'Z' << endl; cout << "'a' + 'A' = " << 'a' + 'A' << endl; cout << "'b' + 'B' = " << 'b' + 'B' << endl; cout << "'z' + 'Z' = " << 'z' + 'Z' << endl; } PROG0610.CPP OUTPUT 'a' - 'A' = 32 'b' - 'B' = 32 'z' - 'Z' = 32 'a' + 'A' = 162 'b' + 'B' = 164 'z' + 'Z' = 212

// PROG0611.cpp // This program demonstrates operations with character variables. #include void main() { char Char1, Char2; Char1 = 'A'; Char2 = Char1 + 5; cout << Char1 << " + 5 = " << Char2 << endl; Char2 = Char1 + 10; cout << Char1 << " + 10 = " << Char2 << endl; Char1++; cout << "Char1++ = " << Char1 << endl; } PROG0611.CPP OUTPUT A + 5 = F A + 10 = K Char1++ = B

String Concatenation Concatenation is the appending of a second string to a first string. "Hello" + "World" = "HelloWorld" "Hello" + " " + "World" = "Hello World" "100" + "200" = "100200" The plus operator ( + ) is used both for arithmetic addition and string concatenation. The same operator performs two totally different operations. In computer science when an operator is used for more than one operation, the operator is said to be overloaded.

// PROG0612.cpp // This program demonstrates concatenation with strings. #include #include "APSTRING.H" void main() { apstring String1 = "Good"; apstring String2 = "Morning"; apstring String3; String3 = String1 + ' ' + String2; cout << String1 << " + " << String2 << " = " << String3 << endl; } PROG0612.CPP OUTPUT Good + Morning = Good Morning

Type Casting Type casting is the intentional assignment of one “indicated” data type to another data type. Indication is important. double Result; Result = 5/3; // integer division double Result; Result = (double) 5/3; // real division // due to type casting

// PROG0613.CPP // This program demonstrates type casting in C++. #include void main() { char Var1; int Var2; float Var3; Var1 = 'A'; Var2 = 65; Var3 = ; cout << Var1 << endl; cout << Var2 << endl; cout << Var3 << endl; cout << endl; cout << "A becomes " << (int) Var1 << endl; cout << "65 becomes " << (char) Var2 << endl; cout << "65/3 without casting becomes " << Var2/3 << endl; cout << "65/3 with casting becomes " << (float) Var2/3 << endl; cout << " becomes " << (int) Var3 << endl; } PROG0613.CPP OUTPUT A A becomes becomes A 65/3 without casting becomes 21 65/3 with casting becomes becomes 3

// PROG0614.CPP // This program demonstrates a second syntax style for type casting. #include void main() { char Var1; int Var2; double Var3; Var1 = 'A'; Var2 = 65; Var3 = ; cout << Var1 << endl; cout << Var2 << endl; cout << Var3 << endl; cout << endl; cout << "A becomes " << int (Var1) << endl; cout << "65 becomes " << char (Var2) << endl; cout << "65/3 without casting becomes " << Var2/3 << endl; cout << "65/3 with casting becomes " << float (Var2/3) << endl; cout << " becomes " << int (Var3) << endl; } PROG0614.CPP OUTPUT A A becomes becomes A 65/3 without casting becomes 21 65/3 with casting becomes becomes 3

// PROG0615.CPP // This program demonstrates integer overflow problems. #include void main() { int Number = 20000; cout << "Number: " << Number << endl; Number += 10000; cout << "Number: " << Number << endl; Number += 10000; cout << "Number: " << Number << endl; Number = 32767; cout << "Number: " << Number << endl; Number++; cout << "Number: " << Number << endl; } PROG0615.CPP OUTPUT Number: Number: Number: Number: Number:

The Odometer and Integer Memory + 1 = =

How Positive Numbers Give Negative Results The first bit in a number is the sign bit It determines if a number is positive or negative 0 = Positive 1 = Negative =

Overflow Problems Overflow is a situation where the assigned value of a variable exceeds the allocated storage space. The resulting value that is stored will be inaccurate and can change from positive to negative or negative to positive. Avoid overflow problems by using a data type that can handle the size of the assigned values. It is important to save computer memory. However, do not be so stingy with memory that overflow problems occur.

// PROG0616.CPP // This program demonstrates precision problems with float. #include void main() { float Number; Number = 9.1; cout << "Number 9.1 = " << Number << endl; Number = 9.12; cout << "Number 9.12 = " << Number << endl; Number = 9.123; cout << "Number = " << Number << endl; Number = ; cout << "Number = " << Number << endl; Number = ; cout << "Number = " << Number << endl; Number = ; cout << "Number = " << Number << endl; Number = ; cout << "Number = " << Number << endl; Number = ; cout << "Number = " << Number << endl; }

PROG0616.CPP OUTPUT Number 9.1 = 9.1 Number 9.12 = 9.12 Number = Number = Number = Number = Number = Number =

// PROG0617.CPP // This program demonstrates precision with float and double. #include #include // required for setprecision function void main() { float FNr; double DNr; cout << "BEFORE USING SETPRECISION(15)" << endl << endl; FNr = ; cout << "Float = " << FNr << endl; DNr = ; cout << "Double = " << DNr << endl << endl; FNr = ; cout << "Float = " << FNr << endl; DNr = ; cout << "Double = " << DNr << endl << endl; cout << setprecision(15);

PROG0617.CPP Continued cout << "AFTER USING SETPRECISION(15)" << endl << endl; FNr = ; cout << "Float = " << FNr << endl; DNr = ; cout << "Double = " << DNr << endl; cout << endl; FNr = ; cout << "Float = " << FNr << endl; DNr = ; cout << "Double = " << DNr << endl; DNr = ; cout << "Double = " << DNr << endl; DNr = ; cout << "Double = " << DNr << endl; DNr = ; cout << "Double = " << DNr << endl; DNr = ; cout << "Double = " << DNr << endl; }

PROG0617.CPP OUTPUT BEFORE USING SETPRECISION(15) Float = Double = Float = Double = AFTER USING SETPRECISION(15) Float Number: Double Number: Float = Double = Double = Double = Double = Double =

// PROG0618.CPP // This program demonstrates the various integer and real C++ data types #include void main() { char Int1;// 1 byte unsigned char Int2; // 1 byte int Int3; // 2 bytes unsigned int Int4; // 2 bytes long Int5;// 4 bytes -2,147,483,647..2,147,483,647 unsigned long Int6; // 4 bytes 0..4,294,967,295 float Float1; // 4 bytes 3.4e e38 double Float2; // 8 bytes 1.7e e308 long double Float3; // 10 bytes 3.4e e4932 Int1 = 127; Int2 = 255; Int3 = 32767; Int4 = 65535; Int5 = ; Int6 = ; Float1 = 3.4e38; Float2 = 1.7e308; Float3 = 1.1e4932;

PROG0618.CPP CONTINUED cout << (int) Int1 << endl; // type casting is needed cout << (int) Int2 << endl; // type casting is needed cout << Int3 << endl; cout << Int4 << endl; cout << Int5 << endl; cout << Int6 << endl; cout << endl; cout << Float1 << endl; cout << Float2 << endl; cout << Float3 << endl; } PROG0618.CPP OUTPUT e38 1.7e e4932

C++ has many simple (single-value) data types. You are expected to understand and use only the following data types: char// (1 byte character or integer) int// (2 byte integer) double// (8 byte real number) bool// (will be explained later) For an APCS Examination, understand means that you can handle questions that include the specified topics. This is primarily true for the multiple choice examination. The word use means that you can write C++ source code that includes the specified topic. APCS Examination Alert

C++ is an industrial strength language. This language is designed for, and used for, many industry-wide applications. This means that C++ is expected to be used by professional programmers who can handle the many complexities of a major programming language. Computer science students, new to programming, can easily get in trouble because C++ does not provide many guards against student peculiarities. Student may also be using C-style programming that is considered obsolete by modern computer science standards. Even though C++ will compile older, C-style features, you should not use features that have not been introduced. Please check with your teacher about the inclusion of any program keyword, operator or style that you are not sure about. Frequently, there is the temptation to use some shortcut, to use some other method because it was shown in a magazine, presented on the Internet, or provided by a friend. Regardless of the source, you are well advised to follow your teacher’s guidelines closely. Failure to follow suggestions may result in computer lock-ups that can mean very time-consuming cold-boots. C++ Warning