Up to now you have created static arrays, a fixed size. Dynamic arrays can use structs and malloc() to change their size. When the array is full: Allocate a new block of memory. Copy the data from one pointer to the other. Free the old pointer. Assign the new pointer to the dynamic array struct
You only have to implement the functions to initialize the dynamic array and to expand the dynamic array. Follow the comments to see what you need to code. The comments that need code have TODO: written in them memcpy(void *dest, void *src, int bytes) A useful function for copying memory between pointers. Parameter 1: destination pointer you are copying to. Parameter 2: source pointer you are copying from. Parameter 3: number of bytes to copy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//The dynamic array struct. This maintains my pointer to memory, effective size and maximum size
typedef struct
{
double *arrayPointer;
int effectiveSize;
int maximumSize;
} DynamicArray;
//Initialize the dynamic array
void CreateArray(DynamicArray *mArray, int initialSize)
{
//TODO: Use malloc to create an array of the initial size. Assign to the arrayPointer variable
//TODO: Set effective size to 0 and maximum size to the initial size
}
//Expand the array to a new size
void ExpandArray(DynamicArray *mArray, int newSize)
{
//TODO: Create a new pointer (double *newArray) and set it with a malloc call of the new size
//TODO: Using either memcpy or a for loop, copy all of the data from the old array to the new one.
//You are only copying to mArray->maximumSize, not the new size.
//TODO: Using the free function, release the previous mArray->arrayPointer
//TODO: Update mArray with the new pointer and the new maximum size. Effective size does not change.
}
//Add a new value from the user to the array
void AddValue(DynamicArray *mArray)
{
//Get the input
double input;
printf("Enter a new value: ");
scanf_s("%lf", &input);
//Assign the input to the array. Increase effective size
mArray->arrayPointer[mArray->effectiveSize] = input;
mArray->effectiveSize++;
//If effective size is now the same as maximum size we need to expand.
if (mArray->effectiveSize == mArray->maximumSize)
{
//Output that we are expanding
printf("Expanding array from %d to %d\n", mArray->maximumSize, mArray->maximumSize * 2);
//Double the size of the array
ExpandArray(mArray, mArray->maximumSize * 2);
}
}
//Print the array
void PrintArray(const DynamicArray *mArray)
{
int i;
//Walk through the array up to effective size and print the values
for (i = 0; i < mArray->effectiveSize; i++)
{
printf("%.2lf ", mArray->arrayPointer[i]);
}
printf("\n");
}
int main(void)
{
int i;
//Create my dynamic array of size 5
DynamicArray mArray;
CreateArray(&mArray, 5);
//Add five values to it
for (i = 0; i < 5; i++)
{
AddValue(&mArray);
}
//Print the array
PrintArray(&mArray);
//Add five more values
for (i = 0; i < 5; i++)
{
AddValue(&mArray);
}
//Print the array
PrintArray(&mArray);
system("pause");
}
the picture is how its suppose to look like.
Please help as i am stuck and dont know what to do