Date post: | 21-Dec-2015 |
Category: |
Documents |
View: | 213 times |
Download: | 2 times |
1
CSC 1401 Computer Programming I
Hamid HarroudHamid HarroudSchool of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn
http://www.aui.ma/~H.Harroud/CSC1401
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 = 0000 1101
Doubles are represented by two sections:
mantissa and exponent. real number = mantissa * 2exponent
e.g. 4.0 = 0010 * 20001 = 2 * 21
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= 0.33333……
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 10.0. 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., 1000.0 + 0.0000001234 is equal to 1000.0
Arithmetic Underflow: an error in which a very small computational result is represented as zero.
e.g., 0.00000001 * 10-1000000 is equal to 0
Arithmetic Overflow: a computational result is too large. e.g., 999999999 * 109999999 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.
int k = 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
Character values can be compared by the equality operatorsequality operators == and !=, or by the relational operatorsrelational 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. A widely used standard is called American Standard Code American Standard Code
for Information Interchange (ASCII)for Information Interchange (ASCII). (See Appendix A in the textbook)
The printable charactersprintable characters have codes from 32 to 126, and others are the control characterscontrol 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 SCHSophomore :: 30-59 SCHJunior :: 60-89 SCHSenior :: 90 or more SCH
Summary
Representation and Conversion of Numeric Types
Representation and Conversion of Type char
Enumerated Types