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.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
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.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Types and Variables. Computer Programming 2 C++ in one page!
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS150 Introduction to Computer Science 1
Data types and variables
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
CSE202: Lecture 10AThe Ohio State University1 Numerical Error.
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
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.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CPS120: Introduction to Computer Science Lecture 8.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Input & Output: Console
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
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.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Week 1 Algorithmization and Programming Languages.
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.
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
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.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
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.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Chapter Topics The Basics of a C++ Program Data Types
Data Types, Variables & Arithmetic
Programming Fundamentals
Basic Elements of C++.
Multiple variables can be created in one declaration
Fundamental Data Types
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Fundamental Data Types
Presentation transcript:

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 3) Use cin to obtain data

Three types so far Words – string whole numbers – integer floating point - double

Text Types to store a sequence of characters, use a string type string name = "Kevin"; to store a single character, you can use a char type char first = 'K';

Text Types what other differences are there between string and char chars are denoted with single quotes string name = "Kevin"; char x = 'K'; chars require 1 byte of store strings require x + 1 bytes of storage chars are a simple data type compatible with C

Write a program that reads in your name, age, and sex, and subsequently displays this information #include using namespace std; int main() { string name; int age; char sex; cout << "Name: "; cin >> name; cout << "Age: "; cin >> age; cout << "Sex: "; cin >> sex; cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl; return 0; }

Write a program that reads in your name, age, and sex, and subsequently displays this information #include using namespace std; int main() { string name; int age; char sex; cout << "Name: "; cin >> name; cout << "Age: "; cin >> age; cout << "Sex: "; cin >> sex; cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl; return 0; }

Write a program that reads in your name, age, and sex, and subsequently displays this information #include using namespace std; int main() { string name; int age; char sex; cout << "Name: "; cin >> name; cout << "Age: "; cin >> age; cout << "Sex: "; cin >> sex; cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl; return 0; }

Write a program that reads in your name, age, and sex, and subsequently displays this information #include using namespace std; int main() { string name; int age; char sex; cout << "Name: "; cin >> name; cout << "Age: "; cin >> age; cout << "Sex: "; cin >> sex; cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl; return 0; }

Write a program that reads in your name, age, and sex, and subsequently displays this information #include using namespace std; int main() { string name; int age; char sex; cout << "Name: "; cin >> name; cout << "Age: "; cin >> age; cout << "Sex: "; cin >> sex; cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl; return 0; }

Write a program that reads in your name, age, and sex, and subsequently displays this information #include using namespace std; int main() { string name; int age; char sex; cout << "Name: "; cin >> name; cout << "Age: "; cin >> age; cout << "Sex: "; cin >> sex; cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl; return 0; }

Integer types there are actually several flavours of "integer" in C++ by default, all integer literals are ints * compiler dependent TypeRangeSize char byte short *2 bytes int … *4 bytes long … *4 bytes

Integer types Example: #include using namespace std; int main() { int i = 2500; cout << i << endl; short s = 2500; cout << s << endl; return 0; } Same as above, but uses half the memory.

Integer types Example 2: #include using namespace std; int main() { int i = 40000; cout << i << endl; short s = 40000; cout << s << endl; return 0; } Error: Literal is too big to be stored in a short. (runtime error: the compiler does not complain)

Is using a short common? not really Why? 1) Memory is cheap embedded programmers will typically make use of small data types 2) Arithmetic operations between any two integer types results in an int

Integer types Example 3: #include using namespace std; int main() { short a = 1600; short b = 2200; cout << a + b << endl; return 0; } Value of addition expression is of type int.

Why is long the same size as int? compiler dependent the rules state that: a short is guaranteed to be both: at least 16 bits (2 bytes) at least as big as a char an int is guaranteed to be both: at least 16 bits (2 bytes) at least as big as a short a long is guaranteed to be both: at least 32 bits (4 bytes) at least as big as an int

Unsigned Integers each of the integer types can be prepended with the word unsigned An unsigned variable can only store positive numbers This increases the size of the number that can be stored

Unsigned integer types * compiler dependent TypeRangeSize unsigned char *1 byte unsigned short *2 bytes unsigned int *4 bytes unsigned long *4 bytes in C++, unsigned only applies to integers!!

Unsigned Numbers why can we store a larger number with an unsigned type? suppose we have a 32 bit signed number one of the bits (usually the leftmost) indicates the sign of the number 0 means positive 1 means negative the remainder of the bits (31) are used to store the digits

Unsigned Numbers why can we store a larger number with an unsigned type? suppose we have a 32 bit unsigned number all 32 bits can be used to store the digits storing one extra digit doubles the size of the number

Example: which of these statements are ok, and which are in error (assuming stated compiler limits)? #include using namespace std; int main() { int i = ; // 3 billion unsigned int j = ; // 3 billion int k = ; // negative 2 billion unsigned int m = ; // negative 2 billion return 0; }

Floating Point types there are actually several flavours of fps in C++ by default, all floating point literals are doubles * compiler dependent TypeRangeSize float e e+384 bytes double e e+308*8 bytes long double3.3621e e+4932*10 bytes

Floating Point Literals by default, a floating point literal is of type double #include using namespace std; int main() { cout << << endl; } stored as a double

Floating Point Literals you can store the number as a float by suffixing the number with f or F #include using namespace std; int main() { cout << 12.84F << endl; } stored as a float

Floating Point Literals you can store the number as a long double by suffixing the number with l or L #include using namespace std; int main() { cout << 12.84L << endl; } stored as a long double

Approximate guidelines for choosing fp type Number of significant digits required Data Type 7 or lessfloat 15 or lessdouble 19 or lesslong double

Examples bank accounts with less than $ double bank accounts with less than $5000 float pi to 17 decimal places long double

Types and Arithmetic our previous rules need an update: 1. if the operator has the same types, then the value of the expression has the same type 2. if one of the operands is a floating point number and the other an integer, then the integer is promoted to a floating point number. The value of the expression is a floating point number (double)* 3. the precedence rules from before still apply

Conversion Rules: * 1. If both operands are an integer type If both operands are character, short, or int data types, the result of the expression is an int value. When one of the operands is a long integer, the result is a long integer. 2. If any one operand is a floating-point value then: when one or both operands are floats, the result is a float when one or both operands are doubles, the result is a double when one or both operands are long doubles, the result is a long double. * from C++ for Engineers and Scientists (Bronson) pp. 79

Examples: Operand 1Operand 2Result intshortint short int long intintlong int double floatdouble float long intfloat

Promotion and Demotion refers to the conversion of one type to another promotion conversion of integer to floating point number ie. an int to a double conversion of smaller integer type to a larger integer type ie. a short to a long conversion of a smaller floating point type to a larger floating point type ie. a float to a long double

Promotion and Demotion refers to the conversion of one type to another demotion conversion of floating point to integer ie. a double to an int conversion of larger integer type to a smaller integer type ie. a long to a short conversion of a larger floating point type to a smaller floating point type ie. a long double to a float

Promotion and Demotion promotion typically occurs automatically no compiler warning generated #include using namespace std; int main() { double x = 10; cout << x << endl; }

Promotion and Demotion demotion typically occurs automatically a compiler warning is generated in only some cases typically, when a fp # is demoted to an integer #include using namespace std; int main() { int x = 10.0; cout << x << endl; }

Why does C++ warn us when demoting a floating-point # to an integer? #include using namespace std; int main() { int x = 10.6; cout << x << endl; } When a floating point number is converted to an integer, the digits after the decimal are lost.

What happens if I want to demote a floating-point # to an int do I have to live with a compiler warning? no use a cast operator Syntax: static_cast ( ); data type expression Data type refers to the type of value to convert to. The expression is any expression with a 'convertible' value.

#include using namespace std; int main() { int x = 10.6; cout << x << endl; } Example:

#include using namespace std; int main() { int x = static_cast ( expression ); cout << x << endl; } Example: We are casting to an int.

#include using namespace std; int main() { int x = static_cast ( expression ); cout << x << endl; } Example: We are casting to an int.

#include using namespace std; int main() { int x = static_cast ( expression ); cout << x << endl; } Example: The expression we wish to convert is 10.6

#include using namespace std; int main() { int x = static_cast ( 10.6 ); cout << x << endl; } Example: The expression we wish to convert is 10.6

#include using namespace std; int main() { int x = static_cast ( 10.6 ); cout << x << endl; } Example:

Why no compiler warning that time? by using the cast operator, you are acknowledging to the compiler that you wish to make the conversion You "assume the risks" involved with casting loss of precision, etc.

When else is casting useful? 2) Conversion of an expression while it is easy to convert integer literals to floating point literals (and vice versa), it is not so easy to do the same to variables, or arithmetic expressions

Example: compute (15 % 4) / (10 % 6) as a decimal result The answer is 3 / 4 = 0.75 #include using namespace std; int main() { cout << (15 % 4) / (10 % 6) << endl; return 0; }

Example: compute (15 % 4) / (10 % 6) as a decimal result The answer is 3 / 4 = 0.75 #include using namespace std; int main() { double result = (15 % 4) / (10 % 6); cout << result << endl; return 0; }

Example: compute (15 % 4) / (10 % 6) as a decimal result The answer is 3 / 4 = 0.75 #include using namespace std; int main() { cout (15 % 4) / (10 % 6) << endl; return 0; }

Casting could we not have simply added a term to the formula? #include using namespace std; int main() { cout << (15 % 4) * 1.0 / (10 % 6) << endl; return 0; }

Casting the problem with this approach is that it is not immediate whether the 1.0 is part of the original equation, or is being used strictly for conversion purposes #include using namespace std; int main() { cout << (15 % 4) * 1.0 / (10 % 6) << endl; return 0; }

Casting this makes it very obvious what is going on #include using namespace std; int main() { cout (15 % 4) / (10 % 6) << endl; return 0; }

C-Style Casting the casting that you've seen so far is C++ casting casting was allowed in C as well predecessor to C++ C-style cast syntax: ( ) data typeexpression or ( ) data typeexpression

Casting C-style casting #include using namespace std; int main() { cout << double(15 % 4) / (10 % 6) << endl; return 0; }

Casting C-style casting #include using namespace std; int main() { cout << (double)(15 % 4) / (10 % 6) << endl; return 0; }

C vs. C-style Casting C casting is more compact however, not part of new standards support for this type of cast could disappear to make sure your code complies with future versions of C++ compilers, use C++ casting