Assignment 4: Watch where you point!

Due Wednesday, February 15th, before midnight

The goals for this assignment are:

  • Use malloc and free

  • Work with pointer-based data structures

  • Use gdb and valgrind.

All programs must run without memory errors and leaks!

1. Update your repository

Do a fetch upstream to obtain the basecode for this assignment.

Using the command line

  • Open terminal and change your current directory to your assignment repository.

  • Run the command git fetch upstream

  • Run the command git merge upstream/main

Your repository should now contain a new folder named A04.

The fetch and merge commands update your repository with any changes from the original.

2. Most danceable

Implement a program, danceable.c, that allows users to easily access the most danceable songs in songlist.csv.

Unlike last week, songlist.csv does not contain the number of songs in the file! The format of the CSV file is as follows, containing a line for the header followed by all songs.
Title, Artist, Duration (ms), Danceability, Energy, Tempo, Valence
Canned Heat,Jamiroquai,331760,0.7,0.865,128.04,0.78
....

Your program should re-use the code to load songs from a CSV file except instead of storing the songs in an array, your program should store the songs in a linked list.

When the user asks for the most danceable song, your program should find the song with the highest danceability, show the user its information, and then delete it from the linked list.

$ make danceable
gcc -g -Wall -Wvla -Werror danceable.c -o danceable
$ ./danceable
0) Eye of the Tiger             Survivor        (4:05s) D: 0.817 E: 0.599 T: 108.873 V: 0.548
1) Thunderstruck                AC/DC           (4:52s) D: 0.502 E: 0.890 T: 133.520 V: 0.259
2) Canned Heat                  Jamiroquai      (5:31s) D: 0.700 E: 0.865 T: 128.040 V: 0.780
3) The Veldt                    deadmau5        (2:50s) D: 0.736 E: 0.759 T: 128.039 V: 0.485
4) Lose Yourself to Dance       Daft Punk       (5:53s) D: 0.832 E: 0.659 T: 100.163 V: 0.674

Dataset contains 5 songs

=======================
Press 'd' to show the most danceable song (any other key to quit): d

---------------------------------------- Most danceable ------------------------------------
Lose Yourself to Dance          Daft Punk       (5:53s) D: 0.832 E: 0.659 T: 100.163 V: 0.674
--------------------------------------------------------------------------------------------

0) Eye of the Tiger             Survivor        (4:05s) D: 0.817 E: 0.599 T: 108.873 V: 0.548
1) Thunderstruck                AC/DC           (4:52s) D: 0.502 E: 0.890 T: 133.520 V: 0.259
2) Canned Heat                  Jamiroquai      (5:31s) D: 0.700 E: 0.865 T: 128.040 V: 0.780
3) The Veldt                    deadmau5        (2:50s) D: 0.736 E: 0.759 T: 128.039 V: 0.485

Dataset contains 4 songs

=======================
Press 'd' to show the most danceable song (any other key to quit): d

---------------------------------------- Most danceable ------------------------------------
Eye of the Tiger                Survivor        (4:05s) D: 0.817 E: 0.599 T: 108.873 V: 0.548
--------------------------------------------------------------------------------------------

0) Thunderstruck                AC/DC           (4:52s) D: 0.502 E: 0.890 T: 133.520 V: 0.259
1) Canned Heat                  Jamiroquai      (5:31s) D: 0.700 E: 0.865 T: 128.040 V: 0.780
2) The Veldt                    deadmau5        (2:50s) D: 0.736 E: 0.759 T: 128.039 V: 0.485

Dataset contains 3 songs

=======================
Press 'd' to show the most danceable song (any other key to quit): d

---------------------------------------- Most danceable ------------------------------------
The Veldt                       deadmau5        (2:50s) D: 0.736 E: 0.759 T: 128.039 V: 0.485
--------------------------------------------------------------------------------------------

0) Thunderstruck                AC/DC           (4:52s) D: 0.502 E: 0.890 T: 133.520 V: 0.259
1) Canned Heat                  Jamiroquai      (5:31s) D: 0.700 E: 0.865 T: 128.040 V: 0.780

Dataset contains 2 songs

=======================
Press 'd' to show the most danceable song (any other key to quit): d

---------------------------------------- Most danceable ------------------------------------
Canned Heat                     Jamiroquai      (5:31s) D: 0.700 E: 0.865 T: 128.040 V: 0.780
--------------------------------------------------------------------------------------------

0) Thunderstruck                AC/DC           (4:52s) D: 0.502 E: 0.890 T: 133.520 V: 0.259

Dataset contains 1 songs

=======================
Press 'd' to show the most danceable song (any other key to quit): d

---------------------------------------- Most danceable ------------------------------------
Thunderstruck                   AC/DC           (4:52s) D: 0.502 E: 0.890 T: 133.520 V: 0.259
--------------------------------------------------------------------------------------------

Dataset contains 0 songs

=======================
Press 'd' to show the most danceable song (any other key to quit): d

Dataset contains 0 songs

=======================
Press 'd' to show the most danceable song (any other key to quit): q

Requirements/Hints:

  • Implement helper functions for printing, removing nodes, clearing the list, and inserting to the list.

  • Make sure your program runs without memory leaks (check with valgrind!)

  • Make sure to handle the case where the list is empty.

  • Make sure to clean up your memory if the list is non-empty when the user quits!

3. Submit your Work

Before submitting, check that

  • Your programs run on goldengate using the make command!

  • valgrind does not report any errors!

Push you work to github to submit your work.

$ cd A04
$ git status
$ git add *.c
$ git status
$ git commit -m "A04 complete"
$ git status
$ git push
$ git status

4. Grading Rubric

Assignment rubrics

Grades are out of 4 points.

  • (4 points) Most danceable

    • (0.1 points) style and header comment

    • (0.4 points) defines a struct, reads and parses songlist.csv correctly, and has correct user input

    • (1.5 points) correctly handles deletion and insertion to a linked list

    • (2.0 points) handles file and linked list nodes correctly (no memory errors!)

Code rubrics

For full credit, your C programs must be feature-complete, robust (e.g. run without memory errors or crashing) and have good style.

  • Some credit lost for missing features or bugs, depending on severity of error

  • -5% for style errors. See the class coding style here.

  • -50% for memory errors

  • -100% for failure to checkin work to Github

  • -100% for failure to compile on linux using make