



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of dynamic arrays in c programming. It explains the use of partially filled arrays and their limitations, and presents a solution using dynamically allocated arrays. The document also covers essential c language features such as malloc, free, assert, and defensive programming.
Typology: Papers
1 / 5
This page cannot be seen from the preview
Don't miss anything!




double average (struct partFillArray * pdata) { double sum = 0.0; int i; for (i = 0; i < 50; i++) /* Error–loop using length / sum = sum + pdata->data[i]; / Error-uninitialized value / return sum / 50; / Error–average is under estimated */ }
double average (struct partFillArray * pdata) { double sum = 0.0; int i; for (i = 0; i < pdata->size; i++) sum = sum + pdata->data[i]; return sum / pdata->size; } struct partFillArray { double data[50]; int size; };
struct dyArray { EleType * data; int size; int capacity; }; void dyArrayInit (struct dyArray * da, int iCap) { da->capacity = iCap; assert (iCap > 0); da->size = 0; da->data = (EleType *) malloc(da->capacity * sizeof(EleType)); assert (da->data != 0); } void dyArrayFree (struct dyArray * da) { free(da->data); da->data = 0; da->capacity = 0; da->size = 0; } int dyArraySize (struct dyArray * da) { return da->size; } void dyArrayAdd (struct dyArray * da, EleType newValue) { if (da->size >= da->capacity) _dyArrayDoubleCapacity(da); da->data[da->size] = newValue; da->size += 1; }
EleType dyArrayGet (struct dyArray * da, int position) { } void dyArrayPut (struct dyArray * da, int position, EleType value) { }
void dyArraySwap (struct dyArray * da, int i, int j) { }