Assignment 3: If you malloc something, set it free

Due Friday, February 7th, 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/main

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. Repeat! Repeat! Repeat!

Implement a program, repeat.c, that asks the user for a string s and an integer n and then creates a new string that repeats s n times. For example, if the user inputs "ha" and the number 3, the program creates a string "hahaha".

$ make repeat
gcc repeat.c -o repeat
$ ./repeat
Enter a word: ha
Enter a count: 3
Your word is hahaha
$ ./repeat
Enter a word: ha
Enter a count: 1000
hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha
hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha
hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha
hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha
hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha
hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha
[truncated]
$ ./repeat
Enter a word: hi
Enter a count: 1000000000000
Cannot allocate new string. Exiting...

Requirements/Hints: * Your program can assume that the user enters words that are smaller than 32 characters. * Your program should use malloc and free. * Your program should check that malloc is successful.

3. Dynamic snackbar

Implement a program, dynamic_snackbar.c, that allows users to add snacks to the snackbar. Unlike last week’s snackbar, your program only needs two features:

  • The ability to add a new snack to the list of snacks

  • The ability to print out the current list of snacks

$ make dynamic_snackbar
gcc dynamic_snackbar.c -o dynamic_snackbar
$ ./dynamic_snackbar
Enter a number of snacks: 3
Enter a name: Slurm
Enter a cost: 1.50
Enter a quantity: 3
Enter a name: Beans
Enter a cost: 5
Enter a quantity: 1
Enter a name: Carrots
Enter a cost: 2
Enter a quantity: 10

Welcome to Dynamic Donna's Snack Bar.

0) Slurm                cost: $1.50     quantity: 3
1) Beans                cost: $5.00     quantity: 1
2) Carrots              cost: $2.00     quantity: 10

Requirements/Hints:

  • You must use malloc to create an array large enough to hold all the snacks

  • Use scanf to get the number of snacks and their attributes

You do not need to type in the attributes every time. You can put the inputs in a file and then use redirection to load it.

4. Wampus

In the file, wampus.c, implement a program that randomly places a Wampus within a NxM grid. Then, fill in the remaining cells with their distance from the Wampus (using the Manhattan distance).

$ ./wampus
Number of rows: 2
Number of columns: 3
3 2 1
2 1 W
$ ./wampus
Number of rows: 1
Number of columns: 1
W
$ ./wampus
Number of rows: 3
Number of columns: 4
1 W 1 2
2 1 2 3
3 2 3 4
$ ./wampus
Number of rows: 6
Number of columns: 3
1 W 1
2 1 2
3 2 3
4 3 4
5 4 5
6 5 6

Requirements/Hints:

  • Use rand() to generate a cell (i,j) for the Wampus.

  • Set a seed so that rand() produces different boards each time.

  • Your program should ask the user for a width and height for the grid

  • Your program should print the grid with its values

  • Your program should use malloc/free to create the grid

  • You can assume that the user will only enter positive values for rows and columns

5. Grading Rubric

Assignment rubrics

Grades are out of 4 points.

  • (1 point) repeat

    • (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

  • (1 point) wampus

    • (0.1 points) style and header comment

    • (0.5 points) no memory errors

    • (0.4 points) correct behavior: uses a command line argument to get the size and outputs the correct board

  • (2 points) dynamic snackbar

    • (0.15 points) style and header comment

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

    • (0.8 points) correctly loads data and prints results in a table

    • (1 points) creates an array using malloc 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