Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the struct and array creation function listed below, use the two following function definitions to modify the given array. typedef struct LonelyPartyArray { int

Using the struct and array creation function listed below, use the two following function definitions to modify the given array.

typedef struct LonelyPartyArray {

int size; int num_fragments; int fragment_length; int num_active_fragments; int **fragments; int *fragment_sizes;

} LonelyPartyArray; 

LonelyPartyArray *createLonelyPartyArray(int num_fragments, int fragment_length){ if( (num_fragments>0)&&(fragment_length>0) ){ LonelyPartyArray *createdArray; createdArray = (LonelyPartyArray *)malloc( sizeof(LonelyPartyArray) ); if(createdArray==NULL){ return NULL; } createdArray->fragment_length = fragment_length; createdArray->num_fragments = num_fragments; createdArray->num_active_fragments = 0; createdArray->size = 0; createdArray->fragments = (int ** )calloc(num_fragments,sizeof(int *)); if(createdArray->fragments==NULL){ free(createdArray); return NULL; } createdArray->fragment_sizes = (int*)calloc(num_fragments,sizeof(int)); if(createdArray->fragment_sizes == NULL){ free(createdArray->fragments); free(createdArray); return NULL;

-------

Function #1: int delete(LonelyPartyArray *party, int index); 

Description: Set the value stored at the corresponding index of the lonely party array to UNUSED. As with the set() and get() functions, based on the index parameter passed to this

function, as well as the number of fragments in the LPA and the length of each fragment, you will have to determine which fragment index maps to, and precisely which cell in that array fragment is being sought.

If the cell being sought is not already set to UNUSED, then after writing UNUSED to that cell, decrement the structs size member so that it always has an accurate count of the total number of cells that are currently being used, and decrement the appropriate value in the structs fragment_size array so that it always has an accurate count of the number of cells currently being used in each fragment.

If deleting the value at this index causes the fragment containing that value to become empty (i.e., all of its cells are now UNUSED), deallocate that array, set the appropriate pointer in the structs fragments array to NULL, and update the structs num_active_fragments member. You should never loop through a fragment to see if all of its cells are unused. Instead, you should rely on the fragment_sizes array to keep track of whether or not a fragment is ready for deallocation.

Keep in mind that index could try taking you to a fragment that has not yet been allocated. Its up to you to avoid going out of bounds in an array and/or causing any segmentation faults.

Output: There are three cases that should generate output for this function. (Do not print the quotes, and do not print around the variables. Terminate any output with .)

If calling this function results in the deallocation of a fragment in memory, print: -> Deallocated fragment

. (capacity: , indices: ..) For explanations of

, , , and , see the set() function description above.

If index is invalid (see definition of invalid index in the set() function description) and party is non-NULL, print: -> Bloop! Invalid access in delete(). (index: , fragment: , offset: ) For explanations of , , and , see the set() function description above. Note that if a cell is marked as UNUSED, but index is within range, you should not generate this error message.

IfpartyisNULL,print:-> Bloop! NULL pointer detected in delete().

Returns: Return LPA_FAILURE if this operation refers to an invalid index (as defined in the set() function description), if this function receives a NULL party pointer, if this operation refers to a cell that lies within an unallocated fragment, or if this operation refers to a cell whose value was already set to UNUSED when the function was called. Otherwise, return LPA_SUCCESS.

Function #2: LonelyPartyArray *resetLonelyPartyArray(LonelyPartyArray *party); 

Description: Reset the lonely party array to the state it was in just after it was created with createLonelyPartyArray(). Be sure to avoid any memory leaks or segmentation faults.

You will need to deallocate any array fragments that are currently active within party. You will also need to reset all the values in the structs fragments and fragment_sizes arrays. However, you should not re-allocate the fragments or fragment_sizes arrays; simply reset the values contained in those already-existing arrays.

You will also need to reset the structs size and num_active_fragments members. You should not, however, change the values of num_fragments or fragment_length.

Output: There are two cases that should generate output for this function. (Do not print the quotes, and do not print around the variables. Terminate any output with .)

If party is non-NULL, print: -> The LonelyPartyArray has returned to its nascent state. (capacity: , fragments: ) Here, is the maximum

number of integers the lonely party array can hold, and is the value of the structs num_fragments member.

If party is NULL, be sure to avoid segmentation faults, and simply return from the functionafterprintingthefollowing:-> Bloop! NULL pointer detected in resetLonelyPartyArray().

Returns: This function should always return party.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions