Assignment 2: If you malloc something, set it free

Due Thursday September 24, before midnight

The goals for this assignment are:

  • Work with malloc/free

  • Work with arrays / 2D arrays

  • Work with a C-style systems API

1. Set Up

Create a new folder /hw2 whereever your local gitub hw repo is on your computer. To submit this homework, you will add and commit your files, and push them to your homework repo.

2. 1. 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 an m x n grid of cells where each cell has probability p of containing a bomb. To do this, generate a random floating-point value between 0 and 1. If that value is less than p, place a bomb in the cell.

Then, your program should 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.

  • You should create dynamic "array of arrays" for your grids.

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

  • Your program must use command line arguments!

Below is example code for seeding and then generating random numbers.

#include 
#include 

int main(void) {
    srand(time(0));   // do this ONCE

    printf("%d\n", rand());
    printf("%d\n", rand());
    printf("%d\n", rand());
}

3. Flattening

Given a 2D array, copy it into a 1D array in row-major order, and then print the flattened array. For example, this 2D array:

int matrix[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};

Becomes this when it’s printed:

1 2 3 4 5 6 7 8 9 10 11 12

Call your flatten() function from your main function on the following 2D arrays. Your function should print out the flattened array for each.

int matrix1[2][3] = {
    {4, 7, 1},
    {9, 2, 6}
};

int matrix2[4][2] = {
    {5, 8},
    {3, 1},
    {7, 4},
    {6, 9}
};

int matrix3[3][5] = {
    {2, 4, 6, 8, 10},
    {1, 3, 5, 7, 9},
    {11, 12, 13, 14, 15}
};

int matrix4[2][5] = {
    {12, 3, 17, 8, 1},
    {6, 14, 2, 9, 11}
};

int matrix5[4][4] = {
    {1, 5, 9, 13},
    {2, 6, 10, 14},
    {3, 7, 11, 15},
    {4, 8, 12, 16}
};

4. Submit your Work

Push you work to Github to submit your work.

You do not need to include a make file.

Please also include a readme.txt where you answer these questions:

  • How long did the homework take you?

  • What did you learn from the homework?

$ cd /hw2
$ git add *.c readme.txt
$ git commit -m "assignment 2 complete"
$ git push origin main

5. Grading Rubric

Assignment rubrics

Grades are out of 10 points.

  • (6 points) Minesweeper

    • (1 point) style and comments

    • (2 points) no memory errors

    • (3 points) correct behavior: accepts command line arguments and produces good output

  • (4 points) Flattening

    • (1 point) style and comments

    • (1 point) no memory errors

    • (2 points) correct behavior: flattens 2D arrays to 1D arrays correctly and prints them out

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.