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.
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: 鄭筱親 陳昱豪.
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.
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.
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
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.
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.
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.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
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.
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.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CPS120: Introduction to Computer Science Operations Lecture 9.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
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.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
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.
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.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 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 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Data Types, Variables & Arithmetic
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 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Summary of what we learned yesterday
Variables and Constants
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 Due 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 (in the text) 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 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’;

12 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

13 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

14 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

15 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;

16 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; }

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

18 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 To displaying bool values (true/false) put the following statements –cin.setf (ios::boolalpha); // before reading –cout.setf (ios::boolalpha); // before writing

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

20 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)

21 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

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

23 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

25 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 (no numbers, strings or characters) –Only use one value for each enumerated declaration –Integers and Enumeration  Wrong: myColor = i; // where i an integer  Correct: myColor = color(i); //casting and use assuming I is not of range