Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.

Slides:



Advertisements
Similar presentations
Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
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.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Types and Variables. Computer Programming 2 C++ in one page!
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: 鄭筱親 陳昱豪.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Instructor: Sanchita Mal-Sarkar Course: CIS 265
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
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.
Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.
Basic Elements of C++ Chapter 2.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Chapter 7 Simple Date Types Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Computer Science 111 Fundamentals of Programming I Number Systems.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
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.
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?
Chapter 2: Using Data.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
Simple Data Types Built-In and User Defined Chapter 10.
Introduction to Programming
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
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.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
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.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Unit 6 Data Types and Arrays. Key Concepts Explicit and automatic conversion ASCII Enumerated types Function parameters Arrays Loops and arrays Passing.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Basic Data Types & Memory & Representation
Chapter 3: Understanding C# Language Fundamentals
Fundamental Data Types
Data Type.
Data Type.
Introduction to Abstract Data Types
Simple Data Types and Function Calls
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Fundamental Data Types
Data Type.
Module 2 Variables, Data Types and Arithmetic
Presentation transcript:

Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of Data Types l Enumeration Note: Read the whole Chapter 7.

Representation And Conversion of Numeric Types rType int and double have different internal representations. Type int values are represented as binary numbers with the leftmost bit containing of the sign of the number. While type double are represented by a binary exponent and mantissa. Real number = mantissa*2**exponent. rAdditional integer types are : short, and long. Type short represents a smaller range than type int. Long represents a larger range than type int. (sizeof() can be used to know the size of memory block). rAdditional floating point types are float that represents a smaller range than type double and long double that represents a larger range than type double. rArithmetic with floating-point data may not be precise, because not all real numbers can be represented exactly. Other types of numerical errors include cancellation error, arithmetic overflow error and underflow error.

rType char data are represented by storing a binary code value for each symbol. ASCII is a commonly used character code. rA variable or expression can be explicitly converted to another type by writing the new type’s name in parentheses before the value to convert. Such a cast is very high precedence operation. average=(double)sum/N given sum and N are declared as int and average as double. rYou can declare your own data types in C using the reserved work typedef: Syntax: typedef enum {identifier_list} enum_type; Example: typedef enum {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} day_t; rDefining an enumerated type requires listing the identifiers that are the values of the type. Each value is represented by an integer. Using the enumerated types makes the programs more readable because the type’s values are meaningful for a particular application.

Enumeration Example #include typedef enum{Good, Bad} result; int main() { result x; printf("Enter the result :"); scanf("%d",&x); switch(x) {case Good : printf("Good \n"); break; case Bad: printf("Bad \n"); break; default :printf("Wrong input \n"); } return 0; }

Exercises write a program that display the ranges of numeric data of type int and long. (Hints :use limits.h) int k=5, m=4,n; double x=1.5, y=2.1,z; How are z=k/m and n=x*y are evaluated ? What is the difference between the following statements given frac is of type double and n1 and d1 are of type int. frac= n1/d1; frac= (double) n1/ (double) d1; frac= (double) n1/d1;

Exercises rIf squaring 10**-20 gives a result of 0, the type of error that has occurred is called ………….. rEvaluate the following expressions if x=10.5,y=7.2, m=5, and n=2 l x/(double) m l (double) (n/m) +y rWrite a c program to display the characters from character ‘ ’ to the character ‘Z’. rEvaluate : (int) ‘D’ –(int) ‘A’, (char) ((int) ‘C’ + 2), (int) ‘6’ – (int) ‘7’ rWrite a C program to accumulate the weekday hours worked. l You need to use an enum type loop counter day_t. l Use a function print_day to display a string corresponding to a value of type day_t.

Exercises rWhat does the segment print ? l For(ch=(int) ‘d’;ch<(int) ’n’ ;ch+=3) printf(“%c”,(char)ch); printf(“\n”);  Which of the following can be an enumeration constant ? 1-an integer 2-a floating-point number, an identifier, a string value.  What is wrong with the following enumerated type definition ? typedef enum{2,3,5,7,11,13} prime_t;  Consider the enumerated type definition : typedef enum{fresh,soph,jr,sr} class_t;  What is the value of each of the following ? A-(int) sr, B-(class_t) 0, C- (class_t) ((int)soph+1)