324_Book

324_Book

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: a_t *)calloc(len, sizeof(data_t)); if (!data) { free((void *) result); return NULL; /* Couldn’t allocate storage */ } result->data = data; } else result->data = NULL; return result; } /* * Retrieve vector element and store at dest. * Return 0 (out of bounds) or 1 (successful) */ int get_vec_element(vec_ptr v, int index, data_t *dest) { if (index < 0 || index >= v->len) return 0; *dest = v->data[index]; return 1; } /* Return length of vector */ int vec_length(vec_ptr v) { return v->len; } code/opt/vec.c Figure 5.4: Implementation of Vector Abstract Data Type. In the actual program, data type data t is declared to be int, float, or double . 5.3. PROGRAM EXAMPLE 211 code/opt/combine.c 1 2 3 4 5 6 7 8 9 10 11 12 /* Implementation with maximum use of data abstraction */ void combine1(vec_ptr v, data_t *dest) { int i; *dest = IDENT; for (i = 0; i < vec_length(v); i++) { data_t val; get_vec_element(v, i, &val); *dest = *dest OPER val; } } code/opt/combine.c Figure 5.5: Initial Implementation of Combining Operation. Using different declarations of identity eleme...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online