----------------------------------------------------------------------------
dynarray.c
----------------------------------------------------------------------------
dynarray.h
This week's assignment is to create a dynamic array. A dynamic array is an array that grows as needed. It supports both negative and positive subscripts, and stores only the array elements that have been assigned. See the attached PDF for a description of the data structure. The attached template, dynarray.c, should be filled out with the implementation code. Link it together with the memory leak code to make sure no memory is leaked. The dynarray.h header file is also attached. Read the comments in the code, and look at how the main() function tests the dynamic array. This should give you an idea of how to implement the interfaces. When complete, upload the dynarray.c file to this assignment. The basic data structure, DynArray, is given in the code but you are welcome to add any elements you feel might be needed.
半 Assignment dynarray A dynamic array is an array that grows as needed, supports both negative and positive subscripts, and stores only the array elements that have been assigned * It is implemented using a doubly chained list of fixed length blocks each storing a fixed number of array element * Each block stores the first subscript and the number of elements that can be stored in the block * The array elements individually are type dyn_t, which can be set to any C type using the DYN_TYPE preprocessor macro origin -128 16 0 16 48 16 1024 16 E-1201) 4. 147 1024 16259 a[14 -114 E-113 a62 a[63 El0 101
1 4 C dynarray No Selection #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> #include <MemLeak.h> 5 /* this setting defines the dyn_t type */ 6 #define DYN_TYPE int #include <dynarray.h> * A DynArray is a dynamic array of dyn_telements that extends in both *the positive and negative directions for any required size. The array 11 + is sparse, so that unset values do not require storage. The basic access function dynSub) returns a pointer to the addressed element 13 and thus can act as an l-value or an r-value. The array is managed as 14 * linked, fixed size elements, whose initial size is controlled by the * Integer passed to the constructor 7 & 9 10 12 15 16 17 struct DynArray { * A reference to the next (in the positive direction) array unit. struct DynArray *rght; * A reference to the next (in the negative direction) array unit. struct DynArray +left; * The base subscript of this unit. 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 int base; * The number of array elements in this unit. int size; * An array of size dyn_t elements dyn_t element[1]; static char erri[] = "memory alloction error %s requesting %d bytes\n"; static void allocErr(char *who, long siz) { fprintf(stderr, erri, who, siz); exit(1); } 42 43 44 45 46 47 * Constructs a DynArray object * @param sz the number of array elements in each linked block of the array (0 implies a default of 32) 48 * @return a reference to the DynArray object 49 50 DynArray * 51 newDynArray(int sz) 52 { 53 DynArray warr = 0; 54 int bytes; 55 // the DynArray structure has 1 array element defined. to allocate storage for sz elements, 56 // add the sizeof(DynArray) to (sz - 1) * sizof (dyn_t). 57 // after allocation, clear the array memory to 8. use memset() (see man 3 memset) initialize // all the other fields as needed. 59 return arri 60 58
int szi C dynarray) main(argc, argv) 60) 61 62 * Destructs a DynArray object. 63 * @param arr a reference to the DynArray object 64 65 void 66 delDynArray (DynArray #arr) 67 { 68 DynArray *sq, *SV; 69 // follow the negative chain and free all the blocks, then follow the positive chain 70 // and free all those blocks 71 } 72 73 *Return the address of the requested dynamic array element; thus, 74 this function can be used either as an r-value or or l-value. 75 * If the block where the requested element should be stored has not 76 yet been allocated, allocate it and chain it into the list of 77 * blocks at the proper location. 78 * @param arr a reference to a dynamic array object 79 * Oparam sub the desired array subscript 80 * @return the address of the array element dynSub[sub] 81 82 dynt 83 dynSub (DynArray Warr, int sub) 84 { 85 86 DynArray #nwa, *pva; 87 // if having trouble, divide the code into two pieces handling the positive 88 // and negative subscripts separately 89 return /* the address of the subscripted array element */ 8; 90 } 91 #define TESTDRIVER 92 #ifdef TESTDRIVER 93 #include <stdio.h> 94 static int 95 verifyStructure (DynArray *arr, int count, int test) 96 { 97 DynArray *prvi 98 int size; 99 int ct: 100 size - arr->size; 101 prv = ; 102 ++test; 103 ct = ; 104 while (arr->rght I ) 105 { 106 ++ct; 107 if (prv != arr->left) 108 fprintf(stderr, "(%) left match failed on block %d\n", test, arr->base); 109 if (size != arr->size) 110 fprintf(stderr, "%d) size match failed on block %d\n", test, arr->base); 111 prv = arr; 112 arr = arr->rght; 113 } 114 if (count ct) 115 fprintf(stderr, "(%) count %d expected to be %d\n", test, ct, count); 116 return test; 117 } 118 int 119 main(int argc, char **arg)
ZIL 122 129 133 135 C dynarray) main(argc, argv) TIB 119 main(int argc, char **argv) 120 { 121 DynArray *arri; int ix, jx; 123 int test: 124 // all of main() is just testing code. examine it to understand how to use the dynSub() API. 125 fprintf(stderr, "%s start test\n", __FILE__); 126 /* cvinit */ 127 test = 1; 128 arri = newDynArray (7); if (@marri) 130 allocErr("newDynArrayi", 7); 131 for (ix = -36; ix < 40; ++ix) 132 { *dynSub(arri, ix) ix; 134 } for (ix = -36; ix < 40; ++ix) 136 { 137 jx = *dynSub(arri, ix); 138 if (jx != ix) 139 { 140 fprintf(stderr, "(xd) array element Xd incorrect value %d\n", test, ix, jx); 141 } 142 } de DynArray(arra); ++test; 145 arri = newDynArray(32); 146 if (@= arri) 147 allocErr("newDynArray2", 32); 148 ix = 1; 149 while (ix < 1000000) 150 { 151 *dynSub(arri, ix) = ix; 152 ix 10; 153 } 154 ix = 1; 155 while (ix <= 1000000) 156 { 157 jx = *dynSub(arri, ix); 158 if (jx !ix) 159 { 160 fprintf(stderr, "(%) array element Xd incorrect value %d\n", test, ix, jx); 161 } 162 ix *= 10; 163 } 164 test = verifyStructure (arri, 5, test); 165 de 1DynArra 11); 166 /* cvterm */ trak(); 168 fprintf(stderr, "%s end test\n", -_FILE__); 169 return; 170 } 171 #endif 172 167
h dynarray) No Selection 1 Hifndef DYNARRAY_INCLUDE_GUARD 2 #define DYNARRAY INCLUDE GUARD 3 #ifdef DYNARRAY_EXPORT 4 #ifdef WIN32 #define DYNARRAY API 6 #else #define DYNARRAY_API 8 #endif 9 Welse 10 #ifdef WIN32 11 #define DYNARRAY_API extern 12 Nelse 13 #define DYNARRAY_API extern 14 #endit 15 # endif 16 #ifndef DYN_TYPE typedef void dyn_t; 18 #else typedef DYN_TYPE dyn_t; 20 4 endif struct DynArray; 22 typedef struct DynArray DynArray; 23 DYNARRAY_API DynArray =newDynArray(int sz); 24 DYNARRAY_API void delDynArray (DynArray *arr); DYNARRAY_API dyn_t *dynSub(DynArray arr, int sub); 26 #undef DYNARRAY_API 27 #endit 28
This week's assignment is to create a dynamic array. A dynamic array is an array that grows as needed. It supports both
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
This week's assignment is to create a dynamic array. A dynamic array is an array that grows as needed. It supports both
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!