+ All Categories
Home > Documents > 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn...

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

Date post: 21-Dec-2015
Category:
View: 213 times
Download: 2 times
Share this document with a friend
22
1 CSC 1401 Computer Programming I Hamid Harroud Hamid Harroud School of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn University University [email protected] http://www.aui.ma/~H.Harroud/CSC1401 Spring 2009
Transcript
Page 1: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

1

CSC 1401 Computer Programming I

Hamid HarroudHamid HarroudSchool of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn

[email protected]

http://www.aui.ma/~H.Harroud/CSC1401

Spring 2009

Page 2: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

Lecture 1: Introduction

Simple Data Types

Lecture 7

Page 3: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

What is a Data Type?

is a set of values and operations that can be performed on those values.

Page 4: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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.

Page 5: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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.

Page 6: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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

Page 7: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

Integer Types in C

Page 8: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

Floating-Point Types in C

Page 9: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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.

Page 10: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

Implementation-Specific Ranges for Positive Numeric Data

Run demo (Fig. 7.2)

Page 11: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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.

Page 12: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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.

Page 13: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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;

Page 14: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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

Page 15: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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.

Page 16: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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.

Page 17: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

Print Part of the Collating Sequence (Fig. 7.3)

Page 18: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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.

Page 19: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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

Page 20: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

Accumulating Weekday Hours Worked (Fig 7.5)

Page 21: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

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

Page 22: 1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma H.Harroud/CSC1401.

Summary

Representation and Conversion of Numeric Types

Representation and Conversion of Type char

Enumerated Types


Recommended