Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University"— Presentation transcript:

1 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma http://www.aui.ma/~H.Harroud/CSC1401 Spring 2009

2 Lecture 1: Introduction Simple Data Types Lecture 7

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

4 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.

5 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.

6 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 * 2 exponent e.g. 4.0 = 0010 * 2 0001 = 2 * 2 1

7 Integer Types in C

8 Floating-Point Types in C

9 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.

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

11 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.

12 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 * 10 9999999 may become a negative value on some machines.

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

14 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

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

16 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.

17 Print Part of the Collating Sequence (Fig. 7.3)

18 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.

19 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);

20 Accumulating Weekday Hours Worked (Fig 7.5)

21 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 :: 30-59 SCH Junior :: 60-89 SCH Senior :: 90 or more SCH

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


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

Similar presentations


Ads by Google