Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.

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.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Introduction to Computers and Programming Lecture 7:
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
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 Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
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.
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
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
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Lesson 2 McManus COP  Computational ◦ problems involving some kind of mathematical processing  Logical ◦ Problems involving relational or logical.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Input & Output: Console
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 2: Using Data.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
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
Unit 3 Lesson 4 How Data Types Affect Calculations Dave Clausen La Cañada High School.
Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Copyright © – Curt Hill Types What they do.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
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.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
1.2 Primitive Data Types and Variables
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
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.
Bill Tucker Austin Community College COSC 1315
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Bill Tucker Austin Community College COSC 1315
Object Oriented Programming
EPSII 59:006 Spring 2004.
Multiple variables can be created in one declaration
Chapter 2 Variables.
Introduction to Primitive Data types
C++ Data Types Data Type
Fundamental Programming
Names of variables, functions, classes
Chapter 2 Primitive Data Types and Operations
The Fundamentals of C++
Introduction to Primitive Data types
Section 6 Primitive Data Types
Presentation transcript:

Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker2 Brief Review This course has presented the following different ways for programs to utilize values stored within the computer –Named Variables –Named Constants –Literal Values –Objects Attributes

Copyright © 2002 W. A. Tucker3 Brief Review All values, in this course, can be represented by five different data types –int – whole number –double – real number –char – a single character –bool – boolean –string – a “collection” of characters How are these stored in memory? –Every value is stored as a binary number

Copyright © 2002 W. A. Tucker4 Decimal Number System Our comfort zone is with the decimal number system (base 10) This system uses 10 characters, the digits 0 through 9 Look at a typical number such as –5 is called the “hundreds position” –7 is called the “tens position” –3 is called the “units position”

Copyright © 2002 W. A. Tucker5 Decimal Number System = 100 = 10 = x 100 = x 10 = 70 3 x 1 = 3 573

Copyright © 2002 W. A. Tucker6 Binary Number System This system uses only 2 characters, the digits 0 through 1 (binary system) This is how computers work internally since logic circuits are either ON or OFF Look at a typical number such as –What does this mean?

Copyright © 2002 W. A. Tucker7 Binary Number System = 8 = 4 = 2 = x 8 = 8 0 x 4 = 0 2 x 1 = 2 0 x 1 = = 10 10

Copyright © 2002 W. A. Tucker8 Hexadecimal Number System Hexadecimal refers to a number system to the base 16 Since writing or reading a long string of 1’s and 0’s is quite difficult, the hexadecimal system is often used to represent 4 binary digits with just one hexadecimal digit. The hexadecimal digits are 0-9, and A-F where: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

Copyright © 2002 W. A. Tucker9 Sizes A single binary digit is called a bit A collection of 8 bits is called a byte A collection of byes is called a word The word size may vary, computer by computer How many numbers of what size can be stored inside a computer? –n bits can represent a number up to (2 n -1) –n bits can represent 2 n different numbers

Copyright © 2002 W. A. Tucker10 Maximum Number of Numbers n (bits) 2 n n (bits) 2 n

Copyright © 2002 Jade Lindquist11 Maximum Numeric Values Since 0 is a positive number, the maximum positive numeric value that can be stored in a number of n bits is 2 n – 1. For example, the numbers 0-7 (000 2 – ) can be stored in a number consisting of 3 bits.

Copyright © 2002 Jade Lindquist12 Maximum Numbers n (bits) 2 n - 1 n (bits) 2 n

Copyright © 2002 Jade Lindquist13 In Practice Although 16 bits could store a number as large as 65535, in practice, one bit is used to store the sign of the number. Thus a short integer (16 bits or 2 bytes) contains numbers in the range of to because 1 bit is used to store the sign of the number and only 15 bits are available to store the number.

Copyright © 2002 W. A. Tucker14 Whole Numbers (Integers) Integers are represented internally in the computer exactly This means that a 12 is stored in binary as a 12 ( ) The maximum size of an integer depends upon how many bytes are used to store integers For Visual Studio 6.0 on a PC integers take 4 bytes –Largest value is +2,147,483,647 –Smallest value is -2,147,483,648

Copyright © 2002 W. A. Tucker15 Real Numbers (double) Real numbers are represented internally in the computer approximately Since the decimal point may move, the term floating point number is often used Real Numbers are stored in logarithm format, with a characteristic and mantisa The number of bytes used for a double value in Visual Studio 6.0 on a PC is 8 bytes –Values are ± 1.7 e ± 308

Copyright © 2002 W. A. Tucker16 Characters Characters are encoded –Except for IBM mainframes, most computers encode characters in ASCII (American Standard Code for Information Interchange) –An upper case letter has a different code than the lower case of the same letter –Numbers, stored as characters, are also stored in ASCII representation –Characters on a Personal Computer using C++ are stored in a single byte of memory This allows for 256 different ASCII characters

Copyright © 2002 W. A. Tucker17 Some ASCII Examples Character ASCII Binary Letter A Letter a Number

Copyright © 2002 W. A. Tucker18 String Values String is a “user defined data type” which is defined as a class All variables of the string “data type” are actually objects of the class string Strings are stored internally in the computer as a collection of characters The length of the string is an attribute (data value) of each object of class string

Copyright © 2002 W. A. Tucker19 Arithmetic and Data Types The compiler will perform arithmetic on values based on their data types –Arithmetic with all integers produces an integer result –Arithmetic with all doubles produces a double result –Arithmetic with mixed data types will produce a result equal to the most complex data type in the expression

Copyright © 2002 W. A. Tucker20 Mixed Type Arithmetic The compiler will “promote” a data type so that the computer can do arithmetic with same type values –This promotion is only done when it is required –In the following example the division is performed as integer division, with an integer result, but the result (which has no decimals) is then promoted to double prior to the multiplication. –EX:int a, b; double c, result; result = a / b * c;

Copyright © 2002 W. A. Tucker21 Explicit Type Casting The compiler can be instructed to change the type of a value, for one instance, to produce different results. –The previous example can be changed to keep the decimal value by inserting an explicit type cast statement. The value of b will be promoted to a double forcing the division to produce a double result prior to the multiplication step. EX:int a, b; double c, result; result = a / double (b) * c;

Copyright © 2002 W. A. Tucker22 Type Casting Type casting can also be used to prevent compiler warning messages which would occur when trying to store a complex data type (like double) into a simpler data type (like int) since precision would be lost by deleting all decimal places EX: int result; double a, b; result = int (a / b);

Copyright © 2002 W. A. Tucker23 Type Casting Type casting may also be used to convert characters to their ASCII integer and visa versa: –EX:int ascii; char letter; letter = char (ascii); ascii = int (letter);

Copyright © 2002 W. A. Tucker24 Character Built In Functions There are many built-in functions for character operations that are available by merely adding another compiler directive #include A partial list of these functions can be found on page 356 of the text Some of the most useful are toupper and tolower, that will force a character to be either the upper case ASCII or lower case ASCII code Note that many of the functions return boolean values and are called “is ____”, like asking a question

Copyright © 2002 W. A. Tucker25 String class functions There are several member functions of the string class that are useful when working with strings –A partial list may be found on page 150 The format for calling a member function uses the dot member operator To call the member function length for object lastName of class string and assign the length to the integer size int size; size = lastName.length();

Copyright © 2002 W. A. Tucker26 Using Boolean Functions The use of a boolean function, with an appropriate name, often makes programs more readable Ex: looping a program while the user wants to continue do { // body of program } while (wantToContinue());// loop while the user // wants to continue

Copyright © 2002 W. A. Tucker27 Boolean Functions Many useful functions can be written to return boolean values bool wantToContinue() { char answer; cout << “Do you want to continue(Y/N?); cin >> answer; if (answer == ‘Y’ || answer == ‘y’) return true; else return false; }

Copyright © 2002 W. A. Tucker28 Boolean Return Values Since condition statements equate to a boolean value, they may be returned directly as boolean values bool wantToContinue() { char answer; cout << “Do you want to continue(Y/N?); cin >> answer; return (answer == ‘Y’ || answer == ‘y’) }