[SOLVED] CMSC 216 Exercise 3

30.00 $

Programming resource
Digital learning resource
Category:
✓ Practical programming resource
✓ Suitable for guided study and reference
✓ Tutor guidance available when needed

Description

5/5 - (4 votes)

Academic integrity statement

Please carefully read the academic honesty section of the course syllabus. We take academic integrity matters

seriously. Please do not post assignment solutions online (e.g., Chegg, github) where others can see your

work. Posting code online can lead to an academic case where you will be reported to the Offifice of Student

Conduct.

5

Specififications

For this exercise, you will implement functions that support a photo album application. The prototypes for

the functions are in fifile photo album.h.

  1. Photo *create photo(int id, const char *description)

Returns a dynamically-allocated Photo struct initialized based on the provided parameters. If parameter

description is not NULL, assume it points to a nul-terminated string. In this case, the function dynami

cally allocates memory to hold description’s string and sets the Photo struct’s description fifield to point

to this allocated memory. If parameter description is NULL, no memory allocation takes place and the

Photo struct’s description fifield is initialized to NULL. The function returns NULL if a memory allocation

fails. You don’t have to worry about freeing memory if any memory allocation fails (e.g., one memory

allocation is successful, but a second one fails).

  1. void print photo(Photo *photo)

Prints the photo’s id and description. If the description is NULL, the message description message is

”None”. The function does nothing if the photo parameter is NULL. See the public tests for information

regarding output format.

  1. void destroy photo(Photo *photo)

Deallocates all dynamically-allocated memory associated with parameter photo. The function does

nothing if parameter photo is NULL.

  1. void initialize album(Album *album)

1Initializes the album size to 0. Assume this function is not called on an album that has already been

initialized. The function does nothing if parameter album is NULL.

  1. void print album(const Album *album)

Prints the contents of the album. If the album has no photos, the message ”Album has no photos.” is

printed. The function does nothing if parameter album is NULL. See the public tests for information

regarding output format.

  1. void destroy album(Album *album)

Deallocates all dynamically-allocated memory associated with parameter album and sets the album’s

size to 0. (The Album struct itself is not deallocated, so “clear album” may be a better name for this

function.) The function does nothing if parameter album is NULL.

  1. void add photo to album(Album *album, int id, const char *description)

Appends (to the end of the array) a photo if there is space (if the album size is less than MAX ALBUM SIZE).

photo is added if a photo cannot be created. The function does nothing if parameter album is NULL or

the album has no space for the photo.

You want to look at the public tests in order to understand the functionality associated with the functions

above.

6

Requirements

  1. Use the C code development strategy described at http://www.cs.umd.edu/~nelson/classes/resources/

cdebugging/development_strategy/.

  1. Your code must be written in the fifile photo album.c.
  2. Do not add a main function to the photo album.c fifile.
  3. Use the provided makefifile to build public tests.
  4. Your program should be written using good programming style as defifined at

http://www.cs.umd.edu/~nelson/classes/resources/cstyleguide/

  1. You only have to implement the functions described above. You can have additional functions, but

defifine them as static.

  1. Do not change the photo album.h fifile provided.
  2. You are encouraged to defifine your own tests (fifiles similar to the public01.c, public02.c, etc. fifiles pro

vided).

  1. You don’t need to use the macros SUCCESS and FAILURE defifined in fifile photo album.h.
  2. To check for memory problems, you can use the CMSC216 memory tool (my memory checker), val

grind, or gcc -fsanitize=address. Remember to only use one at a time, otherwise you may get confusing

information. If you want to disable the CMSC216 memory tool in the public tests, comment out the

function calls start memory check() and stop memory check().

Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.