Assignment 1: Learn to C!

Due Friday, January 28, before midnight

The goals for this assignment are:

  • Work with printf and scanf

  • Practice C syntax: variables, functions, loops, structs, arrays, strings

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

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

3. Rock, paper, scissors

Write a program, rpc.c, that implements the game [rock, paper, scissors](https://en.wikipedia.org/wiki/Rock_paper_scissors). Your program should have an AI and human player. Each turn, each makes a choice and then wins a point based on the following rules

  • Rock bashes scissors

  • Paper covers rock

  • Scissors cut paper

$ make rpc
gcc rpc.c -o rpc
$ ./rpc
Welcome to Rock, Paper, Scissors!
How many rounds do you want to play? 6

Which do you choose? rock, paper, or scissors? nope
AI chose scissors
You entered an invalid choice: nope
AI score: 0, Player score: 0

Which do you choose? rock, paper, or scissors? rock
AI chose scissors
Rock bashes scissors
AI score: 0, Player score: 1

Which do you choose? rock, paper, or scissors? paper
AI chose paper
AI score: 0, Player score: 1

Which do you choose? rock, paper, or scissors? paper
AI chose rock
Paper covers sock
AI score: 0, Player score: 2

Which do you choose? rock, paper, or scissors? scissors
AI chose scissors
AI score: 0, Player score: 2

Which do you choose? rock, paper, or scissors? scissors
AI chose scissors
AI score: 0, Player score: 2
Player wins!

Requirements and hints:

  • Your program should implement at least one function. Try to keep your program DRY (e.g. Don’t Repeat Yourself!)

  • Use the rand() function (defined in stdlib.h) to make the AI’s choice. If the choice is 0, interpret it as "rock"; 1 as "paper"; and 2 as "scissors".

  • Use the strcmp function (defined in string.h) to compare the player’s choice against either rock, paper, or scissors.

  • Your output should be similar to the sample output, although you can customize it!

  • You can assume the user enters an integer for the number of rounds

The code srand(time(0)); sets up the rand() function to produce different random numbers every time you run the program. Comment this line out, to run with the same random numbers. This can be useful for testing.

4. Submit your Work

Push you work to github to submit your work.

$ cd A01
$ git status
$ git add *.c
$ git status
$ git commit -m "assignment 1 complete"
$ git status
$ git push
$ git status