Question
Create a C function to insert a key (element) into the correct index of a dynamically allocated array. Use the format: int set(DynamicArray *arrayTest, int
Create a C function to insert a key (element) into the correct index of a dynamically allocated array.
Use the format: int set(DynamicArray *arrayTest, int index, int key);
Determine which fragment index maps to what, and the cell where the array fragment is being looked for. Use the function arguments, the length of every fragment, and the dynamic array's fragNumber.
Example: The index is 13, fragNumber is 3, fragLength is 10, making up 30 cells with all 3 fragments. If the index is 13, then the key goes into fragments[1], because that's the second array fragment out of 3. The indices go from 0 to 9 for the first array fragment, 10 to 19 for the second array fragment, 20 to 29 for the third array fragment.
If the currently altered index was empty, or if the fragment wasn't allocated, insert the key into the dynamically allocated array. Increment int size in struct DynamicArray so occupied cells can be counted. In the struct's fragSize array, the correct value must be incremented to count the quantity of cells currently being used in a fragment.
If either the index is invalid or a null pointer is passed to the function, return some value and don't change anything about the data structure. An index is only valid if it meets the condition that the index ranges from 0 to (fragNumber * fragLength - 1). Otherwise, the index is out of range and is therefore invalid. Return some message if the element was allocated or if the index was invalid.
So far:
#include
// Struct , Changed this , removed extra DynamicArray on line typedef struct,CHEGGEA typedef struct { int size; // This is the amount of cells that are occupied in all the fragments or arrays int fragLength; // This is how many cells for every fragment int fragNumber; // This is the number of arrays or fragments in the struct
int allocatedFragments; // This is the number of allocated fragments, or fragments not set to NULL int *fragSizes; //This stores the quantity of cells used in every fragment int **fragments; // This is an array of pointers to each fragment
} DynamicArray;
// Declaring the function DynamicArray *createDynamicArray(int fragNumber, int fragLength); int set(DynamicArray *arrayTest, int index, int key);
// Main int main() { int arr1[2] = { 1,2 }; int arr2[2] = { 3,4 }; int arr[5] = { 5,6,7,8,9 }; DynamicArray *ptr; int i, j; createDynamicArray(); //set(*arr2, i, key); free(ptr); }
// Function DynamicArray *createDynamicArray(int fragNumber, int fragLength) { DynamicArray *ptr; int i; if (fragNumber <= 0 || fragLength <= 0) { return NULL; }
ptr = (DynamicArray*)malloc(sizeof(DynamicArray)); ptr->fragments = (int**)malloc(sizeof(int*)*fragNumber); ptr->fragSizes = (int*)malloc(sizeof(int)*fragLength);
for (i = 0; i
for (i = 0; i < fragLength; i++) { ptr->fragSizes[i] = 0; } return ptr; }
int set(DynamicArray *arrayTest, int index, int key){ }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started