Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this programming assignment, you will implement lonely party arrays (arrays that are broken into fragments that get allocated and deallocated on an as-needed basis,

In this programming assignment, you will implement lonely party arrays (arrays that are broken into fragments that get allocated and deallocated on an as-needed basis, rather than being allocated all at once as one big array, in order to eliminate unnecessary memory bloat). This is an immensely powerful and awesome data structure, and it will ameliorate several problems we often encounter with arrays in C

typedef struct LonelyPartyArray {

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

} LonelyPartyArray; 

This header file also contains definitions for UNUSED, LPA_SUCCESS and LPA_FAILURE, which you will use in some of the required functions described below.

Functions that need to be created using the pre-defined struct are listed below

Values for function parameters will be passed from a pre-made main() function

LonelyPartyArray *createLonelyPartyArray(int num_fragments, int fragment_length); 

Description: This function will create a LonelyPartyArray (LPA) that can accommodate up to num_fragments different array fragments, each of length fragment_length. So, the maximum number of integers this LPA can hold is num_fragments * fragment_length.

Start by dynamically allocating space for a new LonelyPartyArray struct. Initialize the structs num_fragments and fragment_length members using the arguments passed to this function. Initialize num_active_fragments and size to the appropriate values. Dynamically allocate the fragments array to be an array of num_fragments integer pointers, and initialize all those pointers to NULL. (Eventually, that array will store pointers to any individual array fragments you allocate when adding elements to this data structure.) Dynamically allocate the fragment_sizes array, and initialize all values to zero (since none of the fragments currently hold any elements).

If one or both of the parameters passed to this function are less than or equal to zero (i.e., if they are not both positive integers), you should immediately return NULL. If any of your calls to malloc() fail, you should free any memory you have dynamically allocated in this function call up until that point (to avoid memory leaks), and then return NULL.

Output: If the function successfully creates a new LPA, print the following: -> A new LonelyPartyArray has emerged from the void. (capacity: , fragments: )

Do not print the quotes, and do not print around the variables. Terminate the line with a newline character, . is the maximum number of integers this LPA can contain (num_fragments * fragment_length). is simply num_fragments. Note that in testing, we will never create a lonely party array whose capacity would be too large to store in a 32-bit int.

Returns: A pointer to the new LonelyPartyArray, or NULL if any calls to malloc() failed. LonelyPartyArray *destroyLonelyPartyArray(LonelyPartyArray *party);

Description: Free all dynamically allocated memory associated with this LonelyPartyArray struct, and return NULL. Be careful to avoid segfaulting in the event that party is NULL.

Output: -> The LonelyPartyArray has returned to the void. (Output should not include the quotes. Terminate the line with a newline character, .) If party is NULL, this function should not print anything to the screen.

Returns: This function should always return NULL.

int set(LonelyPartyArray *party, int index, int key); 

Description: Insert key into the appropriate index of the LonelyPartyArray. 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. For example, if index is 14 and the LPA has num_fragments = 3 and fragment_length = 12, then key would go into the second array fragment (fragments[1]) at index 2 (fragments[1][2]), since the first fragment (fragments[0]) would be used for indices 0 through 11, the second fragment would be used for indices 12 through 23, and the third fragment would be used for indices 24 through 35, for a total of 36 cells across the 3 fragments. For additional indexing examples, please refer to the test cases included with this assignment.

If the fragment where we need to insert key is NULL, then you should dynamically allocate space for that fragment (an array of fragment_length integers), initialize each cell in that new fragment to UNUSED (which is #defined in LonelyPartyArray.h), update the structs num_active_fragments member, and store key at the appropriate index in the new allocated array.

If the index being modified was previously empty (i.e., UNUSED), and/or if the fragment was not yet allocated, then after inserting key into the lonely party array, be sure to increment the structs size member so that it always has an accurate count of the number of cells in the LPA that are currently occupied, and be sure to increment 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 particular fragment.

If index is invalid (see note below), or if a NULL party pointer is passed to this function, simply return LPA_FAILURE without attempting to modify the data structure in any way and without causing any segmentation faults.

Note on invalid index values: In our set(), get(), and delete() functions, index is considered valid if it falls in the range 0 through (num_fragments * fragment_length - 1), even if index refers to a cell marked as UNUSED or a fragment that has not been allocated yet. An invalid index is one that falls outside that specified range.

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 allocation of a new fragment in memory, print: -> Spawned fragment

. (capacity: , indices: ..)

is the index where the newly allocated fragments address is stored in the structs fragments array, and is the length of the new array (i.e., the number of integers it can hold), and and correspond to the lowest and highest index values that would map to this particular fragment.

If party is non-NULL and index is invalid (see definition of invalid index above), print: -> Bloop! Invalid access in set(). (index: , fragment: ,

offset: ) is value of index passed to this function; is the invalid fragment array index that index would have mapped to, had that fragment been within

bounds for this lonely party array; and is the precise index within that fragment that index would have mapped to. For example, if num_fragments = 2, fragment_length = 10, and index = 21, then the valid indices for this LPA would be 0 through 19, and index, being invalid, would produce this error message with = 2 and = 1. See testcase16.c and output16.txt for examples of how to handle output for negative index values. For additional examples, please refer to the test cases included with this assignment.

IfpartyisNULL,print:-> Bloop! NULL pointer detected in set().Output should not contain any quotes or angled brackets. Terminate the line with a newline character, .

Returns: If this operation is successful, return LPA_SUCCESS. If the operation is unsuccessful (either because index is invalid or the function receives a NULL party pointer, or because any calls to malloc() fail), return LPA_FAILURE. LPA_SUCCESS and LPA_FAILURE are #defined in LonelyPartyArray.h.

int get(LonelyPartyArray *party, int index); 

Description: Retrieve the value stored at the corresponding index of the LonelyPartyArray. As with the set() function, 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.

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 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 index is invalid (see definition of invalid index in the set() function description) and party is non-NULL, print: -> Bloop! Invalid access in get(). (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 get().

Returns: If index is valid (see definition of invalid index in the set() function description), return the value stored at the appropriate index in the lonely party array (even if it is marked as UNUSED). If the operation is unsuccessful (either because index is invalid or because the function receives a NULL party pointer), return LPA_FAILURE.

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.

int printIfValid(LonelyPartyArray *party, int index); 

Description: Print the value stored at the corresponding index of the LonelyPartyArray. As with the set(), get(), and delete() 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.

Output: Simply print the appropriate integer to the screen, followed by a newline character, . This function should not print anything if index is invalid (as defined in the set() function description), if index refers to a cell whose value is set to UNUSED, or if party is NULL.

Returns: Return LPA_SUCCESS if this function prints a value to the screen. Otherwise, return LPA_FAILURE.

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. int getSize(LonelyPartyArray *party);

Description: This function simply returns the number of elements currently in the LPA (not including any elements marked as UNUSED). This should be a near-instantaneous function call; it should not loop through the cells in the LPA at all.

We provide this function to discourage other programmers from ever accessing party->size directly if they try to compile code that uses our fancy LPA data structure. That way, if we release this data structure to the public but then end up updating it a few months later to rename the size member of the LonelyPartyArray struct to something else, the programmers who have been using our code and end up downloading the latest version can get it working right out of the box; they dont have to go through their own code and change all instances of party->size

to something else, as long as we still provide them with a getSize() function that works as intended.3

Output: This function should not print anything to the screen. Returns: Return the number of elements currently in the LPA, or -1 if party is NULL.

int getCapacity(LonelyPartyArray *party); 

Description: This function should simply return the maximum number of elements that party can hold, based on the values of its num_fragments and fragment_length members. For example, for the lonely party array shown on pg. 4 of this PDF, getCapacity() would return 110, because when all of its array fragments are allocated, it will be able to hold up to 110 integer elements.

Note that in testing, we will never create a lonely party array whose capacity would be too large to store in a 32-bit int, so you dont need to worry about type casting when calculating this return value.

Output: This function should not print anything to the screen. Returns: Return the capacity of the LPA (as just described), or -1 if party is NULL.

int getAllocatedCellCount(LonelyPartyArray *party); 

Description: This function should return the maximum number of elements that party can hold without allocating any new array fragments. For example, for the lonely party array shown on pg. 4 of this PDF, getAllocatedCellCount() would return 40, because the non-NULL fragments are able to hold up to 40 integer elements in total.

Output: This function should not print anything to the screen. Returns: Return the number of allocated integer cells (as just described), or -1 if the party

pointer is NULL. long long unsigned int getArraySizeInBytes(LonelyPartyArray *party);

Description: This function should return the number of bytes that would be used if we were using a standard array rather than a LonelyPartyArray struct. For example, for the LPA struct shown on pg. 4 of this PDF, a traditional array representation would have 110 integer cells, which would occupy 110 * sizeof(int) = 440 bytes, and so this function should return 440 for that struct. For additional examples, please refer to the test cases included with this assignment.

3 Note, by the way, that it is common to use the term size to refer to the number of elements that have been inserted into a data structure, while length often refers to the number of cells in an array, whether those cells have been used or not. (I.e., length refers to the maximum capacity of an array, in terms of the number of elements it can hold.)

Note: This number could get quite large, and so as you perform the arithmetic here, you should cast to long long unsigned int. (For details, see Section 3 on pg. 7.)

Note: You should use sizeof() in this function (rather than hard-coding the size of an integer as 4 bytes), and cast the sizeof() values to long long unsigned int as appropriate.

Note: If your system does not use 32-bit integers for some reason, using sizeof() in this function could cause output mismatches on some test cases. For this function to work, you will need to ensure that youre testing on a system that uses 32-bit integers (which you almost certainly are). To check that, you can compile and run SanityCheck.c (included with this assignment), or simply run the test-all.sh script.

Output: This function should not print anything to the screen. Returns: The number of bytes (as just described), or 0 if the party pointer is NULL.

long long unsigned int getCurrentSizeInBytes(LonelyPartyArray *party); 

Description: This function should return the number of bytes currently taken up in memory by the LPA. You will need to account for all of the following:

The number of bytes taken up by the LPA pointer itself: sizeof(LPA*)

The number of bytes taken up by the LPA struct (which is just the number of bytes taken

up by the four integers and two pointers within the struct): sizeof(LPA)

The number of bytes taken up by the fragments array (i.e., the number of bytes taken

up by the pointers in that array).

The number of bytes taken up by the fragment_sizes array (i.e., the number of bytes taken up by the integers in that array).

The number of bytes taken up by the active fragments (i.e., the number of bytes taken up by all the integer cells in the individual array fragments).

The getArraySizeInBytes() and getCurrentSizeInBytes() functions will let you see, concretely, the amount of memory saved by using a lonely party array instead of a traditional C- style array.4

Note: This number could get quite large, and so as you perform the arithmetic here, you should cast to long long unsigned int. (For details, see Section 3 on pg. 7.)

Note: You should use sizeof() in this function (rather than hard-coding the sizes of any data types), and cast the sizeof() values to long long unsigned int as appropriate.

Output: This function should not print anything to the screen. Returns: The number of bytes (as just described), or 0 if the party pointer is NULL.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions

Question

Differentiate 3sin(9x+2x)

Answered: 1 week ago

Question

Compute the derivative f(x)=(x-a)(x-b)

Answered: 1 week ago

Question

Develop successful mentoring programs. page 418

Answered: 1 week ago