Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)

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

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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
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.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
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
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
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
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Mathematical Calculations in Java Mrs. G. Chapman.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 7: Expressions and Assignment Statements
Visual Basic Variables
Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2.
Lecture 3 Expressions Richard Gesick.
Chapter 2: Basic Elements of Java
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Data Types and Expressions
Chapter 2 Primitive Data Types and Operations
Data Types and Expressions
Data Types and Expressions
Data Types and Arithmetic in C
Presentation transcript:

Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)

Objectives In this chapter, you will: Learn about the binary representation of integers Discover similarities between integer and character data Learn about methods for converting data from one type to another Become familiar with integer division and its uses Programming with Visual C++: Concepts and Projects2

Objectives (continued) Learn to use the mod operator (%) Use the mod operator (%) and integer division to convert a character to its binary representation Programming with Visual C++: Concepts and Projects3

The Binary Number System We are used to counting in base-10 (the decimal number system) For integers in base 10 each placeholder represents the number of groups (0-9) of some power of 10 Programming with Visual C++: Concepts and Projects4

The Binary Number System (continued) Programming with Visual C++: Concepts and Projects5

The Binary Number System (continued) Computers use base-2 (the binary number system) For integers in base-2, each placeholder represents the number of groups (0-1) of some power of 2 Programming with Visual C++: Concepts and Projects6

The Binary Number System (continued) Programming with Visual C++: Concepts and Projects7

Integral Data Types Integral data types are represented as binary integers – Boolean example 0 = false, 1 = true – Character Requires one byte for ASCII characters example: = ‘A’ – Integer Requires 4 bytes for int data example: = 65 Programming with Visual C++: Concepts and Projects8

Integral Data Types (continued) Different types require different amounts of storage Boolean and character data may be assigned to an integer variable Data should not be assigned to a type that requires less storage – Example: Assigning an integer to a char – The result is loss of information Programming with Visual C++: Concepts and Projects9

Integral Data Types (continued) Programming with Visual C++: Concepts and Projects10

Integral Data Types (continued) Programming with Visual C++: Concepts and Projects11 A single character ( ‘A’ ) may be assigned to an integer variable directly This is because the char data type is a subset of the int data type

Integral Data Types (continued) Programming with Visual C++: Concepts and Projects12

Integral Data Types (continued) Programming with Visual C++: Concepts and Projects13 Although character data can be assigned to an integer variable, the reverse is not true A standard 32-bit integer would not necessarily fit into the 8-bits required to represent a single ASCII character Data loss can occur if this is allowed

Integral Data Types (continued) Programming with Visual C++: Concepts and Projects14

Data Type Conversion Data often has to be converted from one type to another For example, the ToString() method was used in previous projects to transform an integer or double into a string of text An explicit type conversion is a statement that calls a method (like ToString() ) to convert data from one type to another Programming with Visual C++: Concepts and Projects15

Data Type Conversion (continued) Explicit type conversion methods – Unique to Visual C++ ToString() Convert methods Programming with Visual C++: Concepts and Projects16

Data Type Conversion (continued) – Standard C++ Typecasting – (datatype) variable_name Programming with Visual C++: Concepts and Projects17

Data Type Conversion (continued) Implicit type conversion happens automatically – Involves data type promotion like and integer ( int ) being promoted to a double Programming with Visual C++: Concepts and Projects18

Data Type Conversion (continued) Data types may be arranged in order of number of bytes of storage they require Data of type requiring less storage can be assigned to variables with data types requiring more (promotion) It is unsafe to assign data to variable of a type that requires less storage (demotion) Programming with Visual C++: Concepts and Projects19

Data Type Conversion (conversion) Programming with Visual C++: Concepts and Projects20

Integer Arithmetic Division operator (/) has several forms – Real number division – Integer division The result of division is always a real number unless both operands are of integer data types (integer division) The result of integer division – Is always an integer – Any remainder is dropped (truncated) – Example: 7 / 4 is the integer 1, not the real number 1.75 Programming with Visual C++: Concepts and Projects21

Integer Arithmetic (continued) Unless you know the data type of each variable in Example 3-6 you cannot know the result If change is an integer then change / 25 uses integer division If quarters is an integer then change must be as well Programming with Visual C++: Concepts and Projects22

Integer Arithmetic (continued) Programming with Visual C++: Concepts and Projects23 Integer division occurs when sum (478) is divided by 10. The result is 47 (NOT 47.8) The integer 47 is assigned to a double (average) as 47.0

Integer Arithmetic (continued) To avoid integer division when it is not appropriate – Convert either integer operand to a float or double before the division takes place – Convert methods will work for this – C typecasting will also work Programming with Visual C++: Concepts and Projects24

The Mod Operator (%) The mod operator stands for modulus It has nothing to do with percentages It yields the integer remainder from integer division Examples: 7 % 4 is 3, 6 % 6 is 0 Programming with Visual C++: Concepts and Projects25

The Mod Operator (%) (continued) Programming with Visual C++: Concepts and Projects26

The Mod Operator (%) (continued) Programming with Visual C++: Concepts and Projects27

The Mod Operator (%) (continued) Programming with Visual C++: Concepts and Projects28 The remainder of 35 % 25 is 10

Summary Integral types ( bool, char and int ) are all represented in the same way as binary numbers Data of one data type may be converted to another data type in several ways – Explicit type conversion – Implicit type conversion Data type promotion is safe Data type demotion is unsafe Programming with Visual C++: Concepts and Projects29

Summary (continued) Integer division is a special type of division – Both operands must be integers – The result is always an integer – Remainders are truncated Type conversion can be used to override integer division The mod operator (%) is used to capture the remainder from integer division Programming with Visual C++: Concepts and Projects30