Assignment 2: Learn to C

Due Friday, September 12th, before midnight

The goals for this assignment are:

  • Work with strings

  • Work with structs

  • Work with arrays

  • Work with malloc/free

Update your repository

We will use the same repository as Assignment 1

First, you must accept the pull requst on your repository on Github (screenshot).

$ cd cs223-f25-classwork
$ git pull

Your repository should now contain a new folder named A02.

1. Snackbar

  • Due at end of lab **

Write a program, snackbar.c, that maintains a list of snacks available for purchase.

$ make snackbar
gcc snackbar.c -o snackbar

$ ./snackbar
Welcome to Steven Struct's Snack Bar.

How much money do you have? 5

0) Coco Puffs           cost: $1.50     quantity: 4
1) Manchego cheese      cost: $15.50    quantity: 6
2) Magic beans          cost: $0.50     quantity: 0

What snack would you like to buy? [0,1,2] 1
You can't afford it!

$ ./snackbar
Welcome to Steven Struct's Snack Bar.

How much money do you have? 5

0) Coco Puffs           cost: $1.50     quantity: 4
1) Manchego cheese      cost: $15.50    quantity: 6
2) Magic beans          cost: $0.50     quantity: 0

What snack would you like to buy? [0,1,2] 2
Sorry, we are out of Magic beans

$ ./snackbar
Welcome to Steven Struct's Snack Bar.

How much money do you have? 5

0) Coco Puffs           cost: $1.50     quantity: 4
1) Manchego cheese      cost: $15.50    quantity: 6
2) Magic beans          cost: $0.50     quantity: 0

What snack would you like to buy? [0,1,2] 0
You bought Coco Puffs
You have $3.50 left

Requirements:

  • Your program should define a struct snack that stores a name, cost, and quantity

  • Your program should define at least three snacks and store them in an array

  • Your program should be similar to the given output but feel free to customize it!

2. 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.

3. Shift Cypher

Write a program, cypher.c, that asks the user for a word and then encodes it using a shift cypher. A shift cypher replaces each letter with a letter that is X positions from it in the alphabet. For example, is the letter is 'a' and the shift is 2, we replace 'a' with a 'c'. You can assume that all inputs are lowercase and do not contain special characters.

$ make cypher
gcc cypher.c -o cypher
$ ./cypher
Enter a word: elephant
Enter a shift: 2
Your cypher is gngrjcpv
$ ./cypher 
Enter a word: gngrjcpv
Enter a shift: -2
Your cypher is elephant
$ ./cypher 
Enter a word: hello
Enter a shift: 7
Your cypher is olssv
$ ./cypher 
Enter a word: a
Enter a shift: -2
Your cypher is y
$ ./cypher 
Enter a word: z
Enter a shift: 2**
Your cypher is b

Hints:

  • Recall that characters as represented as digits in ASCII. A straight-forward implementation can add offsets to each character of the word.

4. 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;
}

Submit your Work

Push you work to Github to submit your work.

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

Grading Rubric

Assignment rubrics

Grades are out of 4 points.

  • (1 point) minesweeper

    • (0.1 points) style

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

    • (0.5 points) no memory errors

  • (1 point) cypher

    • (0.1 points) style

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

    • (0.5 points) no memory errors

  • (1 points) snackbar

    • (0.1 points) style

    • (0.4 points) correct behavior

    • (0.5 points) no memory errors

  • (1 points) dynamic_snackbar

    • (0.1 points) style

    • (0.4 points) correct behavior

    • (0.5 points) 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

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