CPS 125: Digital Computation and Programming - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

CPS 125: Digital Computation and Programming

Description:

CPS 125: Digital Computation and Programming. Data Types, ... mantissa. exponent. 0. 2. 0.75. 1 -1. 1. sign. int vs. double. Advantages of int: fast, precise ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 37
Provided by: Che6158
Category:

less

Transcript and Presenter's Notes

Title: CPS 125: Digital Computation and Programming


1
CPS 125 Digital Computation and Programming
  • Data Types, Operators, and Simple Functions

2
Outline
  • Representation of Data Types
  • Arithmetic Expressions
  • Case Study
  • Functions Implementing Additional Operators
  • Simple User-Defined Functions
  • Common Programming Errors

3
Representations of Data Types
  • Representation for int
  • Representation for double
  • real number mantissa 2exponent
  • mantissa (0.5, 1)
  • e.g. 3.0 0.75 22
  • -0.5 -1 2-1
  • Representation for char ASCII

Binary Number
e.g. 5 0000 0101
mantissa
exponent
sign
0
2
0.75
1
-1
1
4
int vs. double
  • Advantages of int fast, precise
  • Advantages of double
  • Can represent fractional numbers
  • Can represent a larger range of numbers
  • Minimum range of positive values of type int
  • 1 to 32,767
  • Minimum range of positive values for double
  • 10-37 to 1037
  • long or long int a wider range of integers

5
Numerical Inaccuracies
  • Round-off (representational) errors
  • The computer can only represent the real numbers
    with infinite precision using a finite number of
    bits
  • The computer cannot represent the scientific
    notation of a real number exactly e.g. 1/3, 1/10
  • Magnified by arithmetic operations
  • Cannot compare two real numbers with equal to
    operator

6
Numerical Inaccuracies
  • Cancellation error
  • e.g. 1000.0 0.0000001234 1000.0
  • The computation
  • (11111113 11111111) 7.511111 9.511111
  • The mathematically equivalent computation
  • 11111113 (7.511111 - 11111111) 10.000000
  • Underflow a computation results in a number that
    is too close to zero to be represented
  • Overflow a computation results in a number that
    is too large, i.e., it exceeds the greatest
    representable value

7
Arithmetic Expressions
  • To manipulate type int and double data
  • Binary operators
  • Addition and subtraction -
  • Multiplication
  • Division /
  • Remainder (or mod)
  • Only for integer division
  • Unary operators
  • Negation and plus

8
Integer Division
  • Division computes the integral part and remainder
    returns the integer remainder of the result of
    dividing operand 1 by operand 2
  • e.g. 7 / 2 3, 7 2 1
  • Use longhand division to determine results
  • m n, if m is positive, result is between 1 and
    n-1
  • m (m / n) n (m n)
  • Divisor cannot be 0
  • If divisor is negative, results of / and will
    be implementation dependent

9
Data Type of Expression
  • If all operands are in the same data type, the
    expression would be in this type, too
  • If at least one operand is in the type double,
    the expression would be in type double
  • If an expression has operands with different data
    types, it is called a mixed-type expression

10
Mixed-Type Assignment
  • Assignment statement variable expression
  • First, evaluate the expression
  • Second, assign the value of expression to
    variable
  • int m, n, z
  • double p, x, y
  • m 3 n 2 p 2.0
  • x m / p
  • y m / n
  • z x y

11
Rules for Evaluating Expressions
  • Parenthesis rule
  • All parenthesized sub-expressions must be
    evaluated separately
  • Nested parenthesized sub-expressions must be
    evaluated inside out
  • Operator precedence rule
  • 1. unary , - 2. , /, 3. binary , -
  • Associativity rule
  • Unary operator right associative
  • Binary operator left associative

12
Examples
  • PI radius radius
  • (p2 p1) / (t2 t1)
  • z (a b / 2) w - y
  • -a ( c b ( c a ) / c b / a ) a b /
    2

13
Mathematical Formula in C
  • Always specify multiplication explicitly with
  • e.g. b2 4ac
  • Use parenthesis to control the order of operator
    evaluation
  • e.g.
  • Two arithmetic operators can be written in
    succession if the second is unary operator
  • a - (b c)

14
Conversion of Data Types
  • Automatic conversion
  • In an expression, if one operand is double, then
    the other operand is converted to double and the
    result is a double
  • In an assignment statement variable expression,
    if variable is double (int) and expression is int
    (double), then the expression will be converted
    to double (int) and assigned to variable

15
Conversion of Data Types
  • Explicit conversion cast
  • With the highest precedence
  • int k 5, m 4
  • double x
  • x k / m
  • x (double) k / (double) m
  • x (double) k / m
  • x (double) (k / m)

16
Case Study Quality control in manufacturing flat
washers
  • Problem The High Plains Hardware recently
    installed both a camera to photograph metal parts
    as they pass on a conveyor belt and image
    processing software to compute the area of each
    part. You have been asked to establish an initial
    quality control check for ½-inch flat washers.
    The program will compute the difference between
    the expected and observed areas and will print
    the difference as a percentage of the expected
    area.

17
Case Study
  • Analysis
  • Constants PI 3.14159
  • WASHER_DIAMETER (1.0/2.0)
  • OUTER_DIAMETER (17.0/16.0)
  • Input double observed_area
  • Output double pct_differ
  • Program Variables double washer_radius
  • double outer_radius
  • double rim_area

18
Case Study
  • Analysis (cont.)
  • Relevant formulas
  • area of a circle p radius2
  • radius of a circle ½ diameter
  • percentage difference of 2 areas
  • (expected area observed area) / expected
    area 100

19
Case Study
  • Design
  • Initial Algorithm
  • Get observed area of washer rim
  • Compute expected area of rim
  • Compute percentage difference between expected
    and observed area
  • Display percentage difference
  • Algorithm refinement
  • Step 2 3

20
Case Study
  • Implementation
  • Testing
  • Input a few different observed sizes, some of
    which are smaller and some of which are larger
    than the expected size 0.6903 sq. in.
  • Display information is not enough, so after
    testing, change the last printf statement
  • Question how to revise the program to handle a
    5/16-in. flat washer when outer diameter is 5/8
    in.?

21
Mathematical Functions
  • Capabilities not offered by arithmetic operators
  • include ltmath.hgt or ltstdlib.hgt
  • Function can be thought of a black box
  • One or more input arguments (type conversion)
  • One single output (return value)
  • How-To is hidden in the box
  • Predefined functions in standard libraries
  • Code reuse

22
ltstdlib.hgt
23
ltmath.hgt
24
ltmath.hgt
25
ltmath.hgt
26
ltmath.hgt
27
Examples of Math Functions
  • Use pow and sqrt to compute the roots of a
    quadratic equation in x of the form
  • ax2 bx c 0
  • If we know the lengths of two sides (b and c) of
    a triangle and the angle between them in degrees
    (a), we can compute the length of the third side
    (a) using the following formula
  • a2 b2 c2 2bc cos(a)

28
Simple User-Defined Functions
  • We have seen
  • scanf, printf
  • sqrt, ceil
  • User generated functions
  • Function receive data, process it, return at
    most one value
  • Function may call other functions
  • Function could be activated by a function call

29
Why Functions
  • Separate program into logical pieces
  • Separate What from How
  • Code reuse
  • Program would be easier to
  • Create
  • Understand
  • Modify
  • Maintain

30
Function Prototype
  • Declared before it could be referenced
  • Inserted before the main function
  • Form ftype fname(argument list)
  • Example void instruct()

31
Function Definition
  • Syntax ftype fname(argument list)
  • executable statements
  • Header line
  • Body appears between and
  • Return statement is part of body when the return
    type is not void

32
  • void instruct() / Display user instructions /
  • int main()
  • / variable declarations /
  • instruct()
  • / executable statements /
  • void instruct()
  • printf(This program demonstrate the use of the
    \n)
  • printf(math library function sqrt (square
    root). \n)

33
Calling A Function
  • double x, y, z
  • z findMax(x, y)
  • Calling statement transfers the control to the
    function, and at the end of function execution,
    the control will be returned to the calling
    function

double findMax(double x, double y)
return statement
34
  • / Program comment /
  • preprocessor directives
  • user-defined function prototypes
  • int main()
  • variable declarations
  • executable statements / calling functions here
    /
  • return value
  • user-defined function definitions

35
  • / print a block letter H /
  • void print_h()
  • printf( \n)
  • printf( \n)
  • printf(\n)
  • printf( \n)
  • printf( \n)

36
Common Programming Errors
  • Use include for every standard library from
    which you are using functions
  • Place function prototype preceding main function,
    and function definition after it
  • Make sure the argument list of a function call
    has the right number, order and type as declared
    in prototype
  • Try to avoid division by zero errors
Write a Comment
User Comments (0)
About PowerShow.com