Assignment 1
Assignment 1 is worth 3% of your final mark, and is due by Saturday February 4 at 11:59 pm. It should be submitted via MarkUs.
There are two Practical Lab sessions for this assignment (Tuesday January 24 and Tuesday January 31).
This assignment should be submitted via MarkUs.
Your assignments must follow the style guidelines discussed in the course style guide. Failure to do so may result in deductions.
This assignment covers topics up through Carter chapter 3. You should not make use of concepts from subsequent chapters (e.g., loops).
Announcements and Updates
This section contains a summary the changes that have been made to this page since it was first posted. You should also keep an eye on the Assignment 1 forum on the Discussion Board for additional announcements and clarifications.
(Jan 25): Minor revision to starter code. Please see the post in the Discussion Board for details.
(Jan 29): The tester program is now available. Details are available here.
Part 1: Ultra-Simple Calculator
Introduction
Your task for this part of the assignment is to write an extremely simple calculator. This calculator will prompt the user for a number, an operator, and a second number. The operator can be any one of addition (+
), subtraction (-
), multiplication (*
), division (/
), or percentage (#
, discussed in detail below). The calculator will then print the resulting equation and the answer.
Here is a sample transcript of the calculator (user input in bold):
Please enter a number: 2
Please enter the operator (+, -, *, /, or #): +
Please enter a number: 3
The resulting equation is "2 + 3 = 5".
This transcript is also available as a separate text file.
The calculator performs four steps:
1) Prompts the user for a number.
2) Prompts the user for an operator (one of +, -, *, /, or #).
3) Prompts the user for another number.
4) Performs the calculation, and prints the equation and result (e.g., "2 + 3 = 5").
Input Details
For the numeric values (Steps 1 and 3), you can assume that the user will enter a valid integer. For Step 2, you can assume that the user will only enter one of the valid operators (+ - * / #
). In both cases, the input will be followed by a carriage return.
Calculation Details
All calculations should be performed using floating point numbers (i.e., using double
s) to maintain arithmetic accuracy.
Division by zero
If the user selects division and then enters a 0
as the second number, you should not perform the calculation (since it would be invalid to do so), and instead print an error message. For example:
Please enter a number: 9
Please enter the operator (+, -, *, /, or #): /
Please enter a number: 0
The resulting equation is "9 / 0".
Cannot compute an answer, since we cannot divide by zero.
Percentage (# operator)
If the user enters #
, you should compute the value that is the specified percentage of the second number. In other words, if the user entered the equation 10 # 50
, this would calculate 10% of 50, which is 0.1 * 50, which is 5. As another example:
Please enter a number: 50
Please enter the operator (+, -, *, /, or #): #
Please enter a number: 5
The resulting equation is "50 # 5 = 2.5".
(You might wonder why we are using the #
symbol for percentage, rather than the traditional %
symbol. The reason is that %
is already used for the modulus operator in C, and we felt that it would be more confusing to reuse the %
symbol in a mathematical-context than to make up a new symbol.)
Submission
Submit the file calculator.c
via MarkUs before the due date. Late submissions will not be accepted.
Part 2: Logic Puzzle
In this section, you will write a program to get some experience with Boolean values and operators in C. Your task is to write a program called coins.c
that helps you solve the simple logic puzzle given below.
The Puzzle
There are four coins, labeled Coin 1, Coin 2, Coin 3, and Coin 4. Each coin can be flipped, and can either land with heads facing up ("heads"), or tails facing up ("tails"). Earlier in the day, someone has flipped each of the coins, and written down the results of those tosses on a sheet of paper. They then placed that paper in a sealed envelope, and randomized the coins. Your task is to figure out the correct orientation (heads or tails) of each coin (i.e., figure out what is written on the sheet of paper). In order to figure out how the coins had landed, you are given the following clues:
Clue 1: Coins 2 and 4 are different.
Clue 2: Exactly one of Coin 2, Coin 3, and Coin 4 is heads.
Clue 3: At least one of Coin 1 and Coin 4 is tails.
Clue 4: Coins 3 and 4 are facing the same direction (i.e., either both are heads or both are tails).
Clue 5: Coin 1 is tails.
You may want to solve the puzzle before starting on the program, since it may make it easier to test your program. The puzzle itself is not intended to be challenging.
The Program
Your job is to translate these clues into a program that can take a guess from the user (e.g., "Coins 2 and 3 are heads, and Coins 1 and 4 are tails"), and tell you whether that guess satisfies all of the clues (in this case, the answer would be "no"). Here is a sample transcript. Characters in bold are entered by the user.
Enter guess for Coin 1 ('H' for heads, 'T' for tails): T
Enter guess for Coin 2 ('H' for heads, 'T' for tails): H
Enter guess for Coin 3 ('H' for heads, 'T' for tails): H
Enter guess for Coin 4 ('H' for heads, 'T' for tails): T
Clue 1: Y
Clue 2: N
Clue 3: Y
Clue 4: N
Clue 5: Y
Your guess is not correct.
This transcript is also available as a text file.
You have been given starter code for this assignment, which contains the skeleton of this program. The starter code is available here. Your job is to fill in the missing parts. You should not make any changes to the starter code except where indicated.
Take a moment to look at the existing code. Notice that the program has four bool
variables (coin1
, coin2
, coin3
, and coin4
) which correspond to the user's guess as to which face of the coin is showing (true
for heads, false
for tails). (Note that the choice of true
to represent heads is completely arbitrary, and we could just as easily have chosen to represent tails using true
. The choice is arbitrary because there is no inherent "trueness" associated with either heads or tails; if we were talking about something that did have an implicit trueness (e.g., the presence or absence of a checkmark on a piece of paper), we would want to choose a representation that was logically consistent with that object.) There are also bool
variables for each clue, which correspond to whether or not the user's guess satisfies that clue. Right now all of the clue variables are set to FILL_ME_IN
, which is a bool
constant with the value false
. We use the dummy variable FILL_ME_IN
so that the starter code will compile without modification (and thus make it easier for you to use); if we did something like the following, the code wouldn't compile:
bool clue1 = ; //FILL THIS IN
Reading the User's Guess
Your first job is to prompt the user for a guess for each coin:
Enter guess for Coin 1 ('H' for heads, 'T' for tails): T
Enter guess for Coin 2 ('H' for heads, 'T' for tails): H
Enter guess for Coin 3 ('H' for heads, 'T' for tails): H
Enter guess for Coin 4 ('H' for heads, 'T' for tails): T
The user will enter either 'H'
(they believe the coin is facing heads up) or 'T'
(they believe the coin is facing tails up). You must read the user's input, and based on that input store the correct Boolean value in the appropriate variable. You can assume that the user's input is valid (i.e., they will only enter a single character, either 'H'
or 'T'
). Remember that there are some complications involved with using scanf()
to read characters.
Printing the Result of Each Clue
Next, we are going to skip ahead, and get our program to print out the status of each clue. Your program should produce the following five lines of output, with either a 'Y'
or an 'N'
, depending of whether or not the guess satisfies a particular clue:
Clue 1: Y
Clue 2: N
Clue 3: Y
Clue 4: N
Clue 5: Y
Note: right now, the answers that your program produces will not be correct, since it will produce 'N'
for every clue, no matter what the user has entered. This is because we have not yet filled in the Boolean expressions that correspond to each clue. We are writing code to print out our answer before we have calculated it in order to make it easier to debug our program once we do start writing the Boolean expressions.
Clues
Now we fill in the Boolean expressions that correspond to each clue. In other words, you must convert the clues into expressions that are true iff (if and only if) the clue is satisfied. For example, a clue (not one of the ones in this puzzle) that reads "Both Coin 6 and Coin 8 are facing heads up" would be translated as
bool example_rule = coin6 && coin8;
This expression is true iff coin6
is true and coin8
is true, which is the equivalent of saying "Coin 6 is facing heads up" and "Coin 8 is facing heads up", which is the equivalent of our clue.
Fill in the expressions one at a time, testing each as you go.
isValidGuess
Next, you must complete the Boolean expression isValidGuess
. It should be true iff all of the clues have been satisfied.
Printing the Result
Finally, you should tell the user if their guess is correct. If the guess is correct, you should print the following:
Your guess is correct.
If the guess is not correct, you should print the following:
Your guess is not correct.
Tester
You will be given a simple tester program that will verify the format of your output, but not your result. Details will be posted here shortly.
Starter Code
As mentioned above, you should not make any changes to the starter code other than those specified in this document. You can add code any place specified by the comments:
/*
* Begin Changes Here.
*/
/*
* End Changes Here.
*/
You will also need to replace the six occurrences of the bool
constant FILL_ME_IN
with the appropriate Boolean expressions.
Submission
Submit the file coins.c
via MarkUs before the due date. Late submissions will not be accepted.