Assignment 1: Learn to C!
Due Thursday, September 15, before midnight
The goals for this assignment are:
-
Work with printf and scanf
-
Practice C syntax: variables, functions, loops, structs, arrays, strings
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 remote add upstream git@github.com:brynmawr-cs223-f22/assignments.git
-
Run the command
git fetch upstream
-
Run the command
git merge upstream/main
Your repository should now contain a new folder named A01
.
The command git remote add upstream
links your fork with the original repository. We
only need to do this step once.
The fetch
and merge
commands update your repository with any changes from the original.
2. 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.
3. Restaurants
Write a program, restaurants.c
, that maintains a list of restaurants.
$ make restaurants
gcc restaurants.c -o restaurants
$ ./restaurants
Welcome to Steven Struct's Restarant List.
What hour is it (24 hr clock)? 10
0) Sushi Atsumi open: 11:00 close: 23:00 stars: 3.5
1) The Thirsty Horse open: 17:00 close: 02:00 stars: 4.5
2) Magic Bean Coffee open: 06:00 close: 15:00 stars: 4.1
What restaurant do you want to visit? [0,1,2] 2
Excellent choice. Magic Bean Coffee will be open for 5 more hours
$ ./restaurants
Welcome to Steven Struct's Restarant List.
What hour is it (24 hr clock)? 10
0) Sushi Atsumi open: 11:00 close: 23:00 stars: 3.5
1) The Thirsty Horse open: 17:00 close: 02:00 stars: 4.5
2) Magic Bean Coffee open: 06:00 close: 15:00 stars: 4.1
What restaurant do you want to visit? [0,1,2] 0
Sushi Atsumi isn't open until 11 o'clock!!
$ ./restaurants
Welcome to Steven Struct's Restarant List.
What hour is it (24 hr clock)? 3
0) Sushi Atsumi open: 11:00 close: 23:00 stars: 3.5
1) The Thirsty Horse open: 17:00 close: 02:00 stars: 4.5
2) Magic Bean Coffee open: 06:00 close: 15:00 stars: 4.1
What restaurant do you want to visit? [0,1,2] 1
The Thirsty Horse closed at 2 o'clock!!
Requirements:
-
Your program should define a
struct restaurant
that stores a name, opening hour (int), closing hour (int) and stars rating (float or double) -
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!
-
Your program should handle establishments that are open over midnight, like The Thirsty Horse.
4. Wordle
Write a program, wordle.c
, that implements the game
Wordle. Your program should choose a
random 5-letter word from a list and then give the user 6 tries to
guess the word. Each guess, your program should tell the user what letters
are
-
In the correct place
-
In the word, but in the wrong place
-
Not in the word
$ make wordle
$ ./wordle
Welcome to Wordle!
Please enter a 5-letter word: human
h is NOT in the word.
u is NOT in the word.
m is in the word (but not in the correct spot)!
a is in the word (but not in the correct spot)!
n is NOT in the word.
Please enter a 5-letter word: manic
m is in the word AND in the correct spot!
a is in the word AND in the correct spot!
n is NOT in the word.
i is NOT in the word.
c is in the word (but not in the correct spot)!
Please enter a 5-letter word: maces
m is in the word AND in the correct spot!
a is in the word AND in the correct spot!
c is in the word (but not in the correct spot)!
e is NOT in the word.
s is NOT in the word.
Please enter a 5-letter word: macro
m is in the word AND in the correct spot!
a is in the word AND in the correct spot!
c is in the word (but not in the correct spot)!
r is in the word (but not in the correct spot)!
o is in the word AND in the correct spot!
Please enter a 5-letter word: marco
m is in the word AND in the correct spot!
a is in the word AND in the correct spot!
r is in the word AND in the correct spot!
c is in the word AND in the correct spot!
o is in the word AND in the correct spot!
You guessed it! The word is marco
Here is another example:
Welcome to Wordle!
Please enter a 5-letter word: apple
a is NOT in the word.
p is NOT in the word.
p is NOT in the word.
l is in the word (but not in the correct spot)!
e is in the word (but not in the correct spot)!
Please enter 5-letter word: blues
b is NOT in the word.
l is in the word (but not in the correct spot)!
u is NOT in the word.
e is in the word (but not in the correct spot)!
s is NOT in the word.
Please enter a 5-letter word: monad
m is NOT in the word.
o is NOT in the word.
n is NOT in the word.
a is NOT in the word.
d is NOT in the word.
Please enter a 5-letter word: nomad
n is NOT in the word.
o is NOT in the word.
m is NOT in the word.
a is NOT in the word.
d is NOT in the word.
Please enter a 5-letter word: totem
t is NOT in the word.
o is NOT in the word.
t is NOT in the word.
e is in the word (but not in the correct spot)!
m is NOT in the word.
Please enter a 5-letter word: totem
t is NOT in the word.
o is NOT in the word.
t is NOT in the word.
e is in the word (but not in the correct spot)!
m is NOT in the word.
Sorry, you ran out of tries. The word is leigh
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
chooseWord()
function (defined inwords.h
) to choose a random word. Your basecode has this code in it already. -
Your output should be similar to the sample output, although you can customize it!
-
Your program should print out whether the user guessed the word correctly or not. If the user doesn’t guess the word, you should tell the user the word.
-
Your program should give the user 6 tries to guess the word
-
For testing: print out the random word, or comment out the line
srand(time(0))
so that the same word is always chosen. -
Your code MUST not use boolean types (vanilla C only!!)
Feel free to implement bonus features:
-
Re-ask the user for a word if they enter something that is not 5 characters
-
Don’t allow the user to enter words that contain characters that we know aren’t in the word
-
Surprise us!
5. 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