1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University

Slides:



Advertisements
Similar presentations
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
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.
Scope and Casting. Scope Region of the program where a particular name can be referenced Formal parameters and local variables –can be accessed from within.
Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.
Chapter 3 Data Representation.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Chapter 3 Data Representation. Chapter goals Describe numbering systems and their use in data representation Compare and contrast various data representation.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Data types and variables
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.
Instructor: Sanchita Mal-Sarkar Course: CIS 265
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.
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.
Objectives You should be able to describe: Data Types
Simple Data Type Representation and conversion of numbers
2.2 Errors. Why Study Errors First? Nearly all our modeling is done on digital computers (aside: what would a non-digital analog computer look like?)
Chapter 7 Simple Date Types Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
Computer Science 111 Fundamentals of Programming I Number Systems.
IT253: Computer Organization
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
COMPSCI 210 Semester Tutorial 1
Number Systems Spring Semester 2013Programming and Data Structure1.
chap7 Chapter 7 Simple Data Types chap7 2 Objectives No programming language can predefine all the data types that a programmer may.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Data Representation - Part II. Characters A variable may not be a non-numerical type Character is the most common non- numerical type in a programming.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
5.2 Errrors. Why Study Errors First? Nearly all our modeling is done on digital computers (aside: what would a non-digital analog computer look like?)
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
Computer Systems Architecture Copyright © Genetic Computer School 2008 CSA 1- 0 Lesson 1 Number System.
Round-off Errors and Computer Arithmetic. The arithmetic performed by a calculator or computer is different from the arithmetic in algebra and calculus.
1 Lecture 2  Complement  Floating Point Number  Character Encoding.
CSC 221 Computer Organization and Assembly Language
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
1 Lecture 7: MARS, Computer Arithmetic Today’s topics:  MARS intro  Numerical representations  Addition and subtraction.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
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.
Characters and Strings
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Programming and Data Structure
Number Representation
EPSII 59:006 Spring 2004.
Introduction to Abstract Data Types
Simple Data Types and Function Calls
CS 101 – Sept. 4 Number representation Integer Unsigned √ Signed √
Storing Negative Integers
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Presentation transcript:

1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University Spring 2009

Lecture 1: Introduction Simple Data Types Lecture 7

What is a Data Type? is a set of values and operations that can be performed on those values.

Objectives No programming language can predefine all the data types that a programmer may need. C allows a programmer to create new data types. Simple Data Type: a data type used to store a single value.

Representation and Conversion of Numeric Types int vs. double: why having more than one numeric type is necessary? Can the data type double be used for all numbers? Operations involving integers are faster than those involving numbers of type double. Operations with integers are always precise, whereas some loss of accuracy or round-off error may occur when dealing with double numbers.

Internals Formats All data are represented in memory as binary strings. Integers are represented by standard binary numbers. For example, 13 = Doubles are represented by two sections: mantissa and exponent. real number = mantissa * 2 exponent e.g. 4.0 = 0010 * = 2 * 2 1

Integer Types in C

Floating-Point Types in C

Numerical Inaccuracies Certain fractions cannot be represented exactly in the decimal number system e.g., 1/3= …… The representational error (round-off error) will depend on the number of binary numbers in the mantissa.

Implementation-Specific Ranges for Positive Numeric Data Run demo (Fig. 7.2)

Example: Representational Error for (trial = 0.0; trial != 10.0; trial = trial + 0.1) { …. } Adding 0.1 one hundred times in not exactly The above loop may fail to terminate on some computers. trial < 10.0: the loop may execute 100 times on one computer and 101 times on another. It is best to use integer variable for loop control whenever you can predict the exact number of times a loop body should be repeated.

Manipulating Very Large and Very Small Real Numbers Cancellation Error: an error resulting from applying an arithmetic operation to operands of vastly different magnitudes; effect of smaller operand is lost. e.g., is equal to Arithmetic Underflow: an error in which a very small computational result is represented as zero. e.g., * is equal to 0 Arithmetic Overflow: a computational result is too large. e.g., * may become a negative value on some machines.

Automatic Conversion of Data Types The data of one numeric type may be automatically converted to another numeric types. intk = 5, m = 4; n; double x = 1.5, y = 2.1, z;

Explicit Conversion of Data Types C also provides an explicit type conversion operation called a cast. e.g., int n = 2, d = 4; double frac; frac = n / d; //frac = 0.0 frac = (double) n / (double) d; //frac = 0.5 frac = (double) (n / d); //frac = 0.0

Representation and Conversion of char equality operators relational operators Character values can be compared by the equality operators == and !=, or by the relational operators, and >=. e.g., letter = ‘A’; if (letter < ‘Z’) … Character values may also be compared, scanned, printed, and converted to type int.

ASCII (American Standard Code for Information Interchange) Each character has its own unique numeric code. American Standard Code for Information Interchange (ASCII) A widely used standard is called American Standard Code for Information Interchange (ASCII). (See Appendix A in the textbook) printable characters control characters The printable characters have codes from 32 to 126, and others are the control characters. For example, the digit characters from ‘0’ to ‘9’ have code values from 48 to 57 in ASCII. The comparison of characters (e.g., ‘a’<‘c’) depends on the corresponding code values in ASCII.

Print Part of the Collating Sequence (Fig. 7.3)

Enumerated Types Good solutions to many programming problems require new data types. Enumerated type: a data type whose list of values is specified by the programmer in a type declaration.

Use of Common Operators with Enumerated Types day_t today, tomorrow; today < monday today != wednesday tuesday >= sunday if(today == saturday) tomorrow = Sunday; else tomorrow = (day_t) (today + 1);

Accumulating Weekday Hours Worked (Fig 7.5)

Example Write a program that uses an Enumerated Type to arrange the students level according to Semester Credit Hours. Your program should start by asking the student about her/his SCH and then display his level. Freshman :: 0-29 SCH Sophomore :: SCH Junior :: SCH Senior :: 90 or more SCH

Summary Representation and Conversion of Numeric Types Representation and Conversion of Type char Enumerated Types