Assignment 3: If you malloc something, set it free

Due Wednesday, February 8th, before midnight

The goals for this assignment are:

  • Work with pointers

  • Work with malloc/free

  • Work with arrays

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/master

Your repository should now contain a new folder named A03.

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

2. Can’t get a word in entwise

"We must not be hasty."
-- Treebeard

The Ents in Lord of the Rings were a noble race of tree herders, whose language was "long and sonorous". Ents live a very long time and so speak very…​carefully…​and…​.very…​s.l.o.w.l.y.

Write a program ,slow.c, that turns a phrase into ent-speech. Your program should ask for the pause length and a phrase to change. Your program should output an ent-phrase. Ent-phrases contain '.' between each letter in the input.

Two examples of the running program are shown below. User input is shown in bold.

$ make slow
gcc -g -Wall -Wvla -Werror slow.c -o slow
$ ./slow
Pause length: 3
Text: Surprise!

$ ./slow
Pause length: 5
Text: Hmmm?

Requirements and Hints:

  • You can assume that the user will enter a string that fits into a 32 character buffer, e.g. char buff[32];

  • Watch out that any string you create ends with a terminating null character, e.g. '\0'

  • Use malloc/free to allocate a string that has enough space to store your "slow" speech!

3. Dynamic songs

Implement a program, dynamic_songs.c, that reads songs from a text file into an array. Unlike last week’s songs, your program only needs the following features:

  1. The ability to open a CSV file (songlist.csv) and read its contents into an array created with malloc.

  2. The ability to read in additional information: tempo, valence, and energy.

  3. The ability to run with memory leaks or valgrind errors (remember to call free and fclose!)

  4. The ability to set the contents of our dynamic array of songs, based on the file

  5. The ability to print out the list of songs

The CSV this week contains the duration of songs in milliseconds, not seconds!

The format of the CSV file is

<Num Songs>,,,,,,
Title, Artist, Duration (ms), Danceability, Energy, Tempo, Valence
Canned Heat,Jamiroquai,331760,0.7,0.865,128.04,0.78

From the first line, you should only read the first token to get the number of songs. The second line is a header that you should skip. All subsequent lines are data that you should read into your array.

The last 4 columns are based on Spotify audio analysis traits

  • Danceability (float) Danceability describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable.

  • Energy (float) Energy is a measure from 0.0 to 1.0 and represents a perceptual measure of intensity and activity. Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy.

  • Tempo (float) The overall estimated tempo of a track in beats per minute (BPM). In musical terminology, tempo is the speed or pace of a given piece and derives directly from the average beat duration.

  • Valence (float) A measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry). >= 0⇐ 1

You can read songlist.CSV with nano but you can also load it into Excel or Google sheets to view the file.
$ make dynamic_songs
gcc -g -Wall -Wvla -Werror dynamic_songs.c -o dynamic_songs
$ ./dynamic_songs
Welcome to Dynamic Donna's Danceability Directory.

0) Canned Heat                  artist: Jamiroquai              duration: 5:31 D: 0.700 E: 0.865 T: 128.040 V: 0.780
1) Eye of the Tiger             artist: Survivor                duration: 4:05 D: 0.817 E: 0.599 T: 108.873 V: 0.548
2) The Veldt                    artist: deadmau5                duration: 2:50 D: 0.736 E: 0.759 T: 128.039 V: 0.485
3) Golden                       artist: Harry Styles            duration: 3:28 D: 0.448 E: 0.838 T: 139.863 V: 0.254
4) Thunderstruck                artist: AC/DC                   duration: 4:52 D: 0.502 E: 0.890 T: 133.520 V: 0.259
5) Nonsense                     artist: Madeon                  duration: 3:45 D: 0.543 E: 0.781 T: 87.009 V: 0.695
6) One More Time                artist: Daft Punk               duration: 5:20 D: 0.613 E: 0.697 T: 122.746 V: 0.476
7) Around the World             artist: Daft Punk               duration: 7:09 D: 0.956 E: 0.795 T: 121.294 V: 0.841
8) Lose Yourself to Dance       artist: Daft Punk               duration: 5:53 D: 0.832 E: 0.659 T: 100.163 V: 0.674
9) 10%                          artist: KAYTRANADA              duration: 3:06 D: 0.794 E: 0.757 T: 107.990 V: 0.615
10) Easy                        artist: Groove Armada           duration: 5:52 D: 0.657 E: 0.860 T: 124.150 V: 0.590
11) Halcyon and On and On       artist: Orbital                 duration: 9:27 D: 0.577 E: 0.729 T: 126.988 V: 0.177
12) Enjoy the Silence           artist: Depeche Mode            duration: 4:17 D: 0.641 E: 0.812 T: 112.777 V: 0.822
13) Rasputin                    artist: Boney M.                duration: 5:51 D: 0.639 E: 0.893 T: 126.143 V: 0.966
14) Who's Theme                 artist: Nujabes                 duration: 4:57 D: 0.603 E: 0.485 T: 169.734 V: 0.357
15) Burn the Witch              artist: Radiohead               duration: 3:40 D: 0.541 E: 0.847 T: 148.937 V: 0.620
16) Hold Me Now                 artist: Thompson Twins          duration: 4:45 D: 0.785 E: 0.517 T: 108.108 V: 0.859
17) Crystallize                 artist: Lindsey Stirling        duration: 4:18 D: 0.440 E: 0.623 T: 140.006 V: 0.056

Requirements and Hints:

  • Your program must use malloc/free to allocate the array of songs.

  • Your program should print the songs in an attractive table, although the formatting does not need to match the sample output exactly.

  • You can assume that all songs names and artists fit into an 128 character buffer.

  • Feel free to re-use code from A02 (such as your printed table)!

Because the song names and artists can have spaces in them, read in the file line by line using fgets and then use strtok to split each line using comma as a deliminator ,. Because strtok splits a line into strings, you will need to convert some of the tokens by numeric types. Use atoi to convert a string to a integer. Use atof to a convert a string to a float.
The data for this question comes from Spotify. If you would like to see the statistics for your own favorite songs, try the developer console here! You will need to ID of the song, which you can get from your Spotify account by right clicking on a song.

4. Grading Rubric

Assignment rubrics

Grades are out of 4 points.

  • (1 point) slow

    • (0.1 points) style and header comment

    • (0.5 points) no memory errors

    • (0.4 points) correct behavior: asks the user for input and creates the new string

  • (3 points) dynamic songs

    • (0.15 points) style and header comment

    • (0.05 points) defines a struct to hold the information for each song

    • (0.5 points) correctly handles the duration and prints results in a table

    • (0.8 points) reads and parses songlist.csv

    • (1.5 points) creates an array to hold the data from the file (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