Simple Data Types Chapter 7. 2 7.1 Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types.
True or false A variable of type char can hold the value 301. ( F )
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)
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.
Overview creating your own functions calling your own functions.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS150 Introduction to Computer Science 1
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Input & Output: Console
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
Lecture 19: Simple Data Types. 2 Lecture Contents: t Representation and conversion of numeric types t Representation and conversion of type char t Enumerated.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
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.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Simple Data Types Problem Solving, Abstraction, and Design using.
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.
CPS120: Introduction to Computer Science Operations Lecture 9.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Simple Data Types Built-In and User Defined Chapter 10.
Introduction to Programming
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.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Recap……Last Time [Variables, Data Types and Constants]
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Computing and Statistical Data Analysis Lecture 2
Tokens in C Keywords Identifiers Constants
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Basic Elements of C++.
Multiple variables can be created in one declaration
Basic Elements of C++ Chapter 2.
Chapter 7 Additional Control Structures
Lectures on Numerical Methods
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Summary of what we learned yesterday
Presentation transcript:

Simple Data Types Chapter 7

2 7.1 Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value –Programming practices const type identifier = constant;

3 #define t An additional way to define constants t Used in older C programs prior to introduction of constants #define identifier replacement-text #define pi The same as const float pi = ;

4 7.2 Internal Representations of int and float t float and int used to represent numbers t Stored differently in the computer t int stored as binary 1s and 0s –sign and binary number t float stored in 2 parts plus the sign –sign - characteristic - mantissa –type float number = 2^characteristic * mantissa

5 Value Variations t Three sizes of int –short int –int –long int t Each uses a different amount of the computers memory –long and short provide consistency between compilers –short can save lots of memory

6 Value Variations t Three sizes of float –float –double –long double t Each uses a different amount of the computers memory –double is no less precise than float –long double provides less precision than double

7 Numerical Inaccuracies t Can have errors when using float in some computations t Do to the way floats are stored t Errors will be determined by the number of binary bits used in the mantissa t Arithmetic underflow and arithmetic overflow –Multiplying 2 small or large numbers together respectively

8 Ranges for int and float Constants t See table 7.1 t Definitions for some of these C++ constants are in the limits.h and float.h libraries t The actual values of these constants will vary from computer to computer

9 Mixing Types t The notion of type promotion –promoting a lower type to a higher type example: 3 + x /2 –if x is float constant would be promoted to float as well and actually be 2.0 t Type conversions –int to float (number.0) –float to int (truncation occurs)

10 Mixing Types t Example: int y; float x = 3.89; y = x; y would contain 3

11 Type Casting t Avoid mixing types but if you need to you can cast a type t Type casting allows you to change a type within the program for a specific function form: type (variable) average = sum / float (n); where n is declared as an int

Character Data and Functions t Character literal const char star = ‘*’; char nextLetter; nextLetter = ‘A’;

13 Character Representation t Bits required to store characters is based on the ASCII table (Appendix A) t Each character has an numeric code t 1 byte or 8 bits are typically used to represent characters

14 Relational Operators and Characters t Relational operators can be used with characters t Testing based on the characters ASCII value example:‘0’ < ‘1’ True ‘A’ < ‘B’ True

15 Character Functions t ctype.h library provides many functions to the programmer t Table 7.2 lists many of the functions t Function name on the left and its purpose is listed to the right tolower (c) if c is uppercase, this function returns the corresponding lower case letter

16 collate.cpp // FILE: collate.cpp // PRINTS PART OF THE CHARACTER COLLATING // SEQUENCE #include using namespace std; int main () { const int MIN = 32; const int MAX = 126; char nextChar;

17 collate.cpp // Display sequence of characters. for (int nextCode = MIN; nextCode <= MAX; nextCode++) { nextChar = char (nextCode); cout << nextChar; if (nextChar == 'Z') cout << endl; } return 0; }

18 collate.cpp Program Output Program output … !”#$%&`()*+,./ ;: ?ABCDEFG HIJKLMNOPQRSTUVWXYZ[/]^_’abcdef ghijklmnopqrstuvwxyz{|}~.

Type bool Data and Logical Expressions t Used in assignment statements and logical expressions (True and False) t Complementing expressions <>= ><= >=< ==!= !===

20 Type bool Functions t bool function isdigit if (isdigit (ch)) cout << “You entered a number”; t bool return value from a function if (centsOverflow (cents)) { cents -= 100; dollars ++; }

21 Input and Output with bool t Can NOT be used for input or output –True represented by a numeric 1 –False represented by numeric 0 t Displaying bool values –cin.setf (ios::boolalpha); –cout.setf (ios::boolalpha);

Enumeration Types t Aid program readability t Represent various states example: enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday}; sunday has the value 0 monday has the value 1 and so on user-defined data type

23 Enumeration Type Declarations enum enumeration-type {enumerator-list}; enum classId {freshman, sophomore, junior, senior}; classId newClass; if (newClass == freshman) do something else if (newClass == sophomore)

24 Enumeration Types t Characters t Switch statements t Comparisons t Write functions to read and write enumerated types (not know to compiler) t Discuss color.cpp

25 color.cpp // DISPLAYS THE VALUE OF thisColor void writeColor (color thisColor) { switch (thisColor) { case red: cout << "red"; break; case green: cout << "green"; break;

26 color.cpp case blue: cout << "blue"; break; case yellow: cout << "yellow"; break; default: status = 0; cerr << "*** ERROR: Invalid color value." << endl; }

Common Programming Errors t Omitting pairs of parentheses –m = y2 - y1 / x2 - x1 –Compiler will not complain but calculation will be in error t Unbalanced parentheses –z = sqrt (x + y) / (1 + sqrt (x + y)); t Mixing operators and operand types –float == char

28 Common Programming Errors t Operator Precedence errors –Watch use of parentheses to get correct precedence –! Symbol t Enumeration types –Identifiers can only appear in list –Only use one value for each enumerated declaration