Assignment 3: Watch where you point!

Due Thursday, September 29, before midnight

The goals for this assignment are:

  • Use malloc and free

  • Work with 2D arrays

  • 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 A03.

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

2. Minesweeper

Implement a program, minesweeper.c, that generates a minesweeper board. In Minesweeper, each cell can either contain a bomb or not. Cells without bombs are called safe cells.

Your program should take three command line arguments — m, n, and p — and generate a m x n grid of cells where each cell has probability p of containing a bomb. Then, you program shoul print out the grid using X for bombs and . (period) for safe cells. Finally, your program should output an additional 2D grid with the number of neighboring bombs for each cell (above, below, left, right, NW, NE, SW, SE).

$ make minesweeper
gcc -g -Wall -Wvla -Werror minesweeper.c -o minesweeper
$ ./minesweeper 3 5 0.4
X X . . .
. . . . .
. X . . .

X X 1 0 0
3 3 2 0 0
1 X 1 0 0

Requirements/Hints:

  • Use malloc and free to allocate a 2D array to store the grid.

  • Use rand() and srand(time(0)) to create random boards

  • Your program must use command line arguments!

To read in command line arguments, define main with argc and argv parameters as follows. The first command line argument is always the program name. argc is the number of arguments. You can use atoi and atof to convert from strings to numeric types (int and float respectively)
int main(int argc, char** argv) {
  if (argc != 4) {
    printf("usage: %s m n p\n", argv[0]);
  }

  return 0;
}

3. Sorted Restaurants

Implement a program, sorted_restaurants.c, that allows users to add restaurants to a list sorted by rating, from highest rated to lowest rated.

Your program should have the same features as last week’s restaurant directory, but should use a linked list to store the snacks, rather than an array.

$ make sorted_restaurants
gcc -g -Wall -Wvla -Werror sorted_restaurants.c -o sorted_restaurants
$ ./sorted_restaurants
Enter a number of restaurants: 3
Enter a name: Sushi Atsumi
Open time: 11
Close time: 23
Stars: 3.5
Enter a name: The Thirsty Horse
Open time: 17
Close time: 2
Stars: 4.5
Enter a name: Magic Bean Coffee
Open time: 6
Close time: 15
Stars: 4.1

Welcome to Sorted Sally's Restaurant Directory.

0) The Thirsty Horse    open: 17:00     close: 2:00     stars: 4.5
1) Magic Bean Coffee    open: 6:00      close: 15:00    stars: 4.1
2) Sushi Atsumi         open: 11:00     close: 23:00    stars: 3.5

Requirements/Hints:

  • Implement a function insert_sorted. This function should create a new snack struct and return the first item in the list. See the book, Data structures in C for example implementations of insert.

  • Implement a function clear. This function should clear and free all items in the list.

4. Matching braces (Due Next Thurs - Oct 5)

Implement a program, match_braces.c, that takes a filename as a command line argument and uses a stack to check for matched braces and report any errors.

$ make match_braces
gcc -g -Wall -Wvla -Werror match_braces.c -o match_braces
$ ./match_braces
usage: ./match_braces 
$ ./match_braces prog.c
Cannot open file: prog.c
$ ./match_braces prog1.c
Found matching braces: (21, 32) -> (23, 5)
Found matching braces: (25, 30) -> (28, 9)
Found matching braces: (17, 53) -> (29, 5)
Unmatched brace on Line 31 and Column 1
Found matching braces: (40, 47) -> (43, 5)
Found matching braces: (36, 18) -> (47, 1)
Unmatched brace on Line 34 and Column 34

Requirements/Hints:

  • Implement the functions push, pop, clear. Implementing print is also recommended to help you debug your assignment.

  • Use to input the filename.

  • Print the usage if the user inputs an incorrect number of command line arguments

  • Print an error if your program cannot open the file

  • Use fgetc to read the file one character at a time. Update the current line number when you encounter \n. Update the current column as you read in each character.

  • To check for matched braces, push to the stack when you encounter a '{' and then pop from the stack when you encounter '}'. If the symbols pushed and popped do not match, there is an error.

  • The output of the program should match the line and column numbers that VS Code reports when you check for matched braces.

To open and read the file, use a file pointer (FILE*). See File Input/Output for examples.

5. 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 A03
$ git status
$ git add *.c
$ git status
$ git commit -m "A03 complete"
$ git status
$ git push
$ git status