Assignment 2: Learn to C

Due Friday, January 31, before midnight

The goals for this assignment are:

  • Work with strings

  • Work with structs

  • Work with arrays

1. Update your repository

Do a fetch upstream to obtain the basecode for this assignment.

The first time you do a fetch, you need to set the URL of the upstream repository.

  • git remote add upstream git@github.com:brynmawr-cs223-s25/cs223-s25-assignments.git

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

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

2. Bad password

Write a program, password.c, that asks the user for a word and creates a bad password from it. You can assume that all characters are lowercase.

$ make password
gcc password.c -o password
$ ./password
Enter a word: elephant
Your bad password is 313ph@nt
$ ./password
Enter a word: hello
Your bad password is h311o
$ ./password
Enter a word: rhythm
Your bad password is rhythm

Your bad password algorithm should

  • Replace 'e’s with '3’s

  • Replace 'l’s with '1’s

  • Replace 'a’s with '@'s

3. Snackbar

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!

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

5. Submit your Work

Push you work to Github to submit your work.

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

6. Grading Rubric

Assignment rubrics

Grades are out of 4 points.

  • (1 point) password

    • (0.1 points) style and header comment

    • (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 and header comment

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

    • (0.5 points) no memory errors

  • (2 points) snackbar

    • (0.2 points) style and header comment

    • (0.8 points) correct behavior

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