Download presentation

Presentation is loading. Please wait.

Published byAngelina Brennan Modified over 2 years ago

1
Numeric Types & Ranges

2
ASCII Integral Type

3
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

4
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 calculation k + x /*Evaluates to 6.5 */ Expression evaluated before assignment z = k / m; /*Evaluates to 1, assigns 1.0 */ n = x * y; /* Evaluates to 3.5, assigns 3. */

5
Explicit Conversion Convert data type using a cast int 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;

6
Enumerated Types Used to improve program readability typedef 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.

7
Figure 8.1 Elements of Array x

8
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};

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

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

11
Array Search Flowchart Set flag to false Set counter to 0 flag = true or counter = # of elements current element = target? increment counter set flag = true flag= true? return -1 return counter N N N Y Y Y

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

13
Two-dimensional Array Two-dimensional array char tictac[3][3];

14
Initializing a Two-dimensional Array char tictac[3][3] = { {X', O', X'}, {O', X', O'}, {O', X', X'} }

15
Parallel Arrays #define NUM_STUDENTS 50 int id[NUM_STUDENTS]; double gpa[NUM_STUDENTS];

16
Arrays with Multiple Dimensions ANSI requires C compilers to allow arrays of six dimensions. Two- and three- dimensional arrays are most common int enroll[MAXCRS][5][4]; course campus year

17
Figure 8.20 Three-Dimensional Array enroll

18
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

19
Figure 8.4 Data Area for Calling Module and Function do_it

20
Figure 8.8 Diagram of a Function That Computes an Array Result

21
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 */ or void fill_array (int *list, int n, int, in_value);

22
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 */ or fill_array(&x[0], 5, 1);

23
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) {…}

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google