#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include
ID: 3738356 • Letter: #
Question
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int *array;
int effectiveSize;
int maximumSize;
} DynamicArray;
void CreateArray(DynamicArray *pDynamicStruct, int initialSize)
{
pDynamicStruct->array = (int *)malloc(sizeof(int) * initialSize);
//Or
//pDynamicStruct->array = (double *) calloc(initialSize,sizeof(double));
pDynamicStruct->effectiveSize = 0;
pDynamicStruct->maximumSize = initialSize;
}
//TODO: Fill in this function
// This function expands the array to maximumSize * 2
void ExpandArray(DynamicArray *pDynamicStruct)
{
//1) Create new memory space by calling malloc or calloc, save the address to a temp pointer
// You want maximumSize * 2 new indices. Don't forget, malloc is number of BYTES, so include sizeof(int)
//2) Copy the data from the old pointer (pDynamicStruct->array) to your new pointer.
// You may use a for loop or if you want an advanced solution, use memcpy
//3) Free the old pointer that is stored in the pDynamicStruct->array variable
//4) Update the pDynamicStruct's array and maximumSize variables with their new values (the temp pointer and new size)
}
//TODO: Fill in this function
// This function adds a new value to the expanding array
void PlaceValue(DynamicArray *pDynamicStruct, int value)
{
//1) Place value in the array to the index effectiveSize is pointing to
//2) Increment effective size
//3) If effective size == maximum size, expand the array
}
void PrintArray(const DynamicArray *pDynamicStruct)
{
int i;
for (i = 0; i < pDynamicStruct->effectiveSize; i++)
{
printf("%d ", pDynamicStruct->array[i]);
}
printf(" ");
}
int main(void)
{
int i;
DynamicArray dynamicArray;
CreateArray(&dynamicArray, 5);
//TODO: Delete from here.....
dynamicArray.array[dynamicArray.effectiveSize++] = 5;
dynamicArray.array[dynamicArray.effectiveSize++] = 10;
dynamicArray.array[dynamicArray.effectiveSize++] = 15;
dynamicArray.array[dynamicArray.effectiveSize++] = 20;
//TODO: ...to here and replace with repeated calls to PlaceValue()
PrintArray(&dynamicArray);
system("pause");
}
Explanation / Answer
void ExpandArray(DynamicArray *pDynamicStruct)
{
//1) Create new memory space by calling malloc or calloc, save the address to a temp pointer
// You want maximumSize * 2 new indices. Don't forget, malloc is number of BYTES, so include sizeof(int)
DynamicArray pDynamicStructNew = (int *)malloc(sizeof(int) * pDynamicStruct->maximumSize * 2);
//2) Copy the data from the old pointer (pDynamicStruct->array) to your new pointer.
// You may use a for loop or if you want an advanced solution, use memcpy
for(int i=0l i<effectiveSize;i++) {
pDynamicStructNew->array[i] = pDynamicStruct->array[i];
}
//3) Free the old pointer that is stored in the pDynamicStruct->array variable
free(pDynamicStruct->array);
//4) Update the pDynamicStruct's array and maximumSize variables with their new values (the temp pointer and new size)
pDynamicStruct->array = pDynamicStructNew->array;
pDynamicStruct->maximumSize = pDynamicStruct->maximumSize*2;
}
void PlaceValue(DynamicArray *pDynamicStruct, int value)
{
//1) Place value in the array to the index effectiveSize is pointing to
pDynamicStruct->array[effectiveSize] = value;
//2) Increment effective size
effective++;
//3) If effective size == maximum size, expand the array
if(effectiveSize == maximumSize)
ExpandArray(&pDynamicStruct);
}
int main(void)
{
int i;
DynamicArray dynamicArray;
CreateArray(&dynamicArray, 5);
//TODO: Delete from here.....
/* *** dynamicArray.array[dynamicArray.effectiveSize++] = 5;
dynamicArray.array[dynamicArray.effectiveSize++] = 10;
dynamicArray.array[dynamicArray.effectiveSize++] = 15;
dynamicArray.array[dynamicArray.effectiveSize++] = 20; *** */
// Remove above statements to add values in the array, instead call PlaceValue() as below to insert elements in the array
//TODO: ...to here and replace with repeated calls to PlaceValue()
PlaceValue(&dynamicArray, 5);
PlaceValue(&dynamicArray, 10);
PlaceValue(&dynamicArray, 15);
PlaceValue(&dynamicArray, 20);
PrintArray(&dynamicArray);
system("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.