Upload
angelina-brennan
View
225
Download
0
Tags:
Embed Size (px)
Citation preview
Numeric Types & Ranges
ASCII Integral Type
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
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. */
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;
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.
Figure 8.1
Elements of Array x
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};
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;
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.
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
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.
Two-dimensional Array
• Two-dimensional arraychar tictac[3][3];
Initializing a Two-dimensional Array
char tictac[3][3] = {
{‘X', ‘O', ‘X'},
{‘O', ‘X', ‘O'},
{‘O', ‘X', ‘X'}
}
Parallel Arrays
#define NUM_STUDENTS 50
int id[NUM_STUDENTS];
double gpa[NUM_STUDENTS];
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
Figure 8.20
Three-Dimensional Array enroll
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
Figure 8.4
Data Area for Calling Module and Function do_it
Figure 8.8
Diagram of a Function That Computes an Array Result
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);
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);
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) {…}