CMSC216 Excercise 3-Photo Album Solved

35.00 $

Description

5/5 - (2 votes)

1 Objectives

To practice dynamic memory allocation.

2 Overview

First, copy the directory photo album from the grace 216public exercises directory. As usual, that folder con- tains the .submit file that allows you to submit.

You can discuss this exercise with your classmates, but you may not exchange any code nor write code together.

5 Specifications

For this exercise, you will implement functions that support a photo album application. The prototypes for the functions are in file 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 field to point to this allocated memory. If parameter description is NULL, no memory allocation takes place and the Photo struct’s description field 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).
  2. 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.
  3. void destroy photo(Photo *photo)Deallocates all dynamically-allocated memory associated with parameter photo. The function does nothing if parameter photo is NULL.

4. void initialize album(Album *album)

1

Initializes 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.
  2. 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.) ThefunctiondoesnothingifparameteralbumisNULL.
  3. 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

7

  1. UsetheCcodedevelopmentstrategydescribedathttp://www.cs.umd.edu/~nelson/classes/resources/ cdebugging/development_strategy/.
  2. Your code must be written in the file photo album.c.
  3. Do not add a main function to the photo album.c file.
  4. Use the provided makefile to build public tests.
  5. Your program should be written using good programming style as defined at
       http://www.cs.umd.edu/~nelson/classes/resources/cstyleguide/
    
  6. You only have to implement the functions described above. You can have additional functions, but define them as static.
  7. Do not change the photo album.h file provided.
  8. You are encouraged to define your own tests (files similar to the public01.c, public02.c, etc. files pro- vided).
  9. You don’t need to use the macros SUCCESS and FAILURE defined in file photo album.h.
  10. 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().
  • Exercise3-l9zm9e.zip