23
Numeric Types & Ranges

Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Embed Size (px)

Citation preview

Page 1: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Numeric Types & Ranges

Page 2: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

ASCII Integral Type

Page 3: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Numerical Inaccuracies• Representational error– Round-off error– Caused by coding a real number as a finite number of

digits– Magnified through repeated computations

• Cancellation error– Caused by adding a very large number and a very small

number• Arithmetic underflow– Caused by multiplying very small numbers

• Arithmetic overflow– Caused by multiplying very large numbers

Page 4: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Implicit Conversion

• Assume variables:int k=5, m=4, n;double x=1.5, y=2.1, z;

• Operands of different types– Narrow type converted to wider type before

calculationk + x /*Evaluates to 6.5 */

• Expression evaluated before assignmentz = k / m; /*Evaluates to 1, assigns 1.0 */n = x * y; /* Evaluates to 3.5, assigns 3. */

Page 5: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Explicit Conversion

• Convert data type using a castint n1, d1;

scan_fraction(&n1, &dl);

/*integer division performed. n1=2, d1=4, result=0*/

frac = n1 / d1;

/*Use cast to force floating point division. Result = 1.5*/

frac = (double)n1 / (double)n2;

Page 6: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Enumerated Types

• Used to improve program readabilitytypedef enum {Black, Brown, Red, … White}color_code_t;

• Black is an enumeration constant with the value 0, Brown is 1, Red is 2 ….. White is 9!

• Add typedef enum declarations in header immediately after preprocessor directives.

Page 7: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Figure 8.1

Elements of Array x

Page 8: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Array Declaration& Initialization

int variableName[10];

int variableName[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};

Page 9: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Processing an Array Using a Loop

• Use a counter-controlled loop#define SIZE 11

int square[SIZE], i;

for (i= 0; i < SIZE; ++i)

square[i] = i * i;

Page 10: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Array Search Algorithm• Assume the target has not been found.• Start with the initial array element• Repeat while the target is not found and there are

more array elements– If the current element matches the target

• Set a flag to indicate that the target has been found.– Else

• Advance to the next array element.

• If the target was found:– Return the target index as the search result.

• Else– Return -1 as the search result.

Page 11: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Array Search FlowchartSet flag to false

Set counter to 0

flag = true or counter = # of

elementscurrent

element = target?

increment counter

set flag = true

flag= true?

return -1return

counter

N

NN

Y

YY

Page 12: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Algorithm for Selection Sort

• For each value of fill from 0 to n-2– Find index_of_min, the index of the smallest

element in the unsorted subarray list[fill] through list[n-1]

– If fill is not the position of the smallest element (index of min):• Exchange the smallest element with the one at

position fill.

Page 13: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Two-dimensional Array

• Two-dimensional arraychar tictac[3][3];

Page 14: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Initializing a Two-dimensional Array

char tictac[3][3] = {

{‘X', ‘O', ‘X'},

{‘O', ‘X', ‘O'},

{‘O', ‘X', ‘X'}

}

Page 15: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Parallel Arrays

#define NUM_STUDENTS 50

int id[NUM_STUDENTS];

double gpa[NUM_STUDENTS];

Page 16: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Arrays with Multiple Dimensions

• ANSI requires C compilers to allow arrays of six dimensions.

• Two- and three- dimensional arrays are most commonint enroll[MAXCRS][5][4];

course campusyear

Page 17: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Figure 8.20

Three-Dimensional Array enroll

Page 18: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Array Elements as Function Arguments

• Array elements can be used as input or output arguments.

• Consider the function with the prototype:void do_it (double arg_1, double *arg2_p, double *arg3_p);

• Pass array x elements as arguments:do_it(x[0], &x[1], &x[2]);

input argument output arguments

Page 19: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Figure 8.4

Data Area for Calling Module and Function do_it

Page 20: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Figure 8.8

Diagram of a Function That Computes an Array Result

Page 21: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Arrays as Parameters

• Passing an array as a parameter passes the pointer to the array, not a copy.– Function can modify the array.

• Function prototype that accepts an array:void fill_array (int list[], int n, int, in_value); /* Clearer */

orvoid fill_array (int *list, int n, int, in_value);

Page 22: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Passing an Array Argument

• Because an array argument refers to a memory location, no subscript is required:int x[5];

fill_array(x, 5, 1); /* better */

orfill_array(&x[0], 5, 1);

Page 23: Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite

Preventing Modification

To prevent an array from being modified by a function, use the const keyword in the function declaration:

int get_min_sub(const double data[], int data_size) {…}