Lab 3: Decisions and Simple Loops
Objective
The objective of this lab is to develop three simple C programs. The first program will identify a substance based on its boiling point. The second program will calculate the angle at which a robot should throw a ball to hit a target. The third program will simulate an ATM machine distributing cash to a customer. You will use the math.h
library, if-else statements, while, do-while and for loops to solve these problems.
Grades
This lab is worth 13 marks.
The automarker breakdown is as follows:
- Part 1: Substance Test (3 marks: 2 marks for passing the test cases given in this sheet, 1 mark for passing a hidden test case)
- Part 2: Robot Shooting (4 marks: 3 marks for passing the test cases given in this sheet, 1 mark for passing a hidden test case)
- Part 3: ATM Machine (3 marks: 2 marks for passing the test cases given, 1 mark for passing a hidden test case)
You can always see your public test cases automarker grades here APS105 after every submission. They are out of 7.
The in-person lab marking is worth 3 marks, and will be marked by a TA in-person. You should go to your assigned lab section, your assigned TA and sit on your assigned workstation. You will find this information on Quercus under Lab Subsection. You must sit in your assigned seat for the TA to grade you. Otherwise, they will mistakenly think you are absent and give you a 0. The in-lab grading is due February 8, 2025 in your practical session (the following week after the deadline). Your assigned TA will ask you questions about your code, and will look at your code to ensure the coding style is neat. For this lab, the coding style involves if you are indenting your code properly, if you are using proper variable names, if you are using proper comments, and if you are using proper spacing.
Instructions
-
Go to your GitHub repo from lab 0 by following this link:
https://github.com/compeng-gg/2025-winter-aps105-<UTORID>
, where you'll have to replace<UTORID>
with your UTORid. For example, my repository ishttps://github.com/compeng-gg/2025-winter-aps105-eyolfso3
. -
Open the same codespace you created for lab0. You should not create any other codespaces.
Fig.1 Existing codespace created in your GitHub repository
-
In the terminal, type in the following command to transfer (pull) lab 2 material into your codespace:
pull
This will do a "pull" (which gets files from the "student" repository, containing starter code). This helper command will make sure you commit all your changes before you can start Lab 3. You should always commit (and push) your changes!
Important! If you're using the codespace to save the lecture notes, you'll get "You must commit all your changes (check
git status
)". To resolve this, you may use the following command instead:git pull upstream main
If you get the following error, please press on
Ctrl
+X
for Mac and Windows.Fig.2 Merge pull message
noteIf you are using the Codespace please ignore these steps. For students NOT using the Codespace, first do:
git remote add upstream git@github.com:compeng-gg/2025-winter-aps105-student.git
You only need to do this once, then after you'll use:
git pull
This command gets the starter code for the lab.
-
You should see new folders called
lab3part1
,lab3part2
, andlab3part3
in your repository.
To use the compile, run and debug buttons in the bottom of the screen with the math library in this lab, you should go to .vscode/settings.json file and add "-lm"
to the following line: "C_Cpp_Runner.linkerArgs": ["-lm"]
. It should look like the following figure:
Fig. Configure math library in VS Code.
Part 1: Substance Test
The table below shows the normal boiling points of several substances. Write a C program that prompts the user for the observed boiling point of a substance in degree Celsius () (as an integer), and identifies the substance if the observed boiling point is within of the expected boiling point, where is a threshold defined by the user. If the data input is not within of any of the boiling points in the table, the program should output the message: Substance unknown
.
Substance | Normal boiling point () |
---|---|
Water | 100 |
Mercury | 357 |
Copper | 1187 |
Silver | 2193 |
Gold | 2660 |
Here is a sample output from an execution of the program:
Example 1:
Enter the threshold in Celsius: 10 Enter the observed boiling point in Celsius: 2668 The substance you tested is: Gold
Example 2:
Enter the threshold in Celsius: 30 Enter the observed boiling point in Celsius: 1156 Substance unknown.
You may assume that the entered values by the user are valid integers within the range allowed by the int
type. The test cases will only use threshold values sufficiently small to yield unique answers.
Testing and Submission
-
Create a new file called
part1.c
in thelab3part1
folder. -
Edit your code in
part1.c
, compile it, and run it to produce the output shown above. You can use the handy buttons shown below.Fig.3 Handy buttons to compile, run, and debug your C program.
You also compile using the terminal using
compile
command, and run usingmeson-build/part1
command. You shouldcd /workspace/lab3part1
before running these commands. -
Once you have the correct output, go to the terminal and test your program against the test cases by typing the following commands:
cd /workspace/lab3part1
test
- If your program passes the test cases, you can submit your code by typing the following commands.
git add part1.c
git commit -m "Lab 3: Part 1"
git push
If your program failed the test cases and you want to view the input, you can open the the file that contains the inputs. For example, if you failed test case 1 in lab 2 part 1, you can view the input by opening lab3part1/test/public/inputs/part1-1
file.
You won't be able to see your grade after pushing each part. If you want to, you can create files for part2.c
and part3.c
in their respective directories with empty main
functions, and push them. After you push all three parts, you can see your grade on the CompEng.gg.
Part 2: Robot Shooting
You are a member of team participating in a basketball shooting competition. Kareem and Aaron are in your team. You are required to build a robot to shoot at a basketball rim. The team that scores the most is the winner.
Write a program that finds the angle at which the robot should throw the ball. It is angle in the following figure.
Fig.3 Robot shooting a ball in a projectile problem.
This is a projectile problem, and the following is what the competition organizers give you.
- The speed with which the robot throws the ball is m/s.
- The robot will stand at a horizontal distance between 3 and 30 m (inclusive).
- The basketball rim will be at a vertical height between 3 and 6 m (inclusive).
- The height of the robot is m.
The following equations are used to solve the problem:
where is time taken to travel the horizontal distance , is the vertical height after time, .
Your program should take in from the user the horizontal distance, , from the basketball rim, and the vertical distance, of the basketball rim. It should check that and . If not, your program should prompt the user to re-enter the values.
Your program should then iterate through from to by increments of . Then, calculate and until you find an that leads to a difference between and . is calculated using Eqn.(4) above by substituting with .
Once your program finds an that makes , it should print the value of this .
Use double
to store floating point numbers. You can use the math.h
library.
Here is a sample output from an execution of the program:
Example 1:
Please enter the horizontal distance from the wall between 3 and 30 m: 4 Please enter the target height between 3 and 6 m: 6 The angle should be 46.00
Example 2:
Please enter the horizontal distance from the wall between 3 and 30 m: 14 Please enter the target height between 3 and 6 m: 4 The angle should be 18.00
Example 3:
Please enter the horizontal distance from the wall between 3 and 30 m: 31 Please enter the horizontal distance from the wall between 3 and 30 m: 30 Please enter the target height between 3 and 6 m: 7 Please enter the target height between 3 and 6 m: 6 The angle should be 33.00
Testing and Submission
-
Create a new file called
part2.c
in thelab3part2
folder. -
Edit your code in
part2.c
, compile it, and run it to produce the output shown above. You can use the handy buttons at the bottom or thecompile
andmeson-build/part2
to compile and run your code. Before using the commands, you mustcd /workspace/lab3part2
to change the directory to part 2. -
Once you have the correct output, go to the terminal,
cd
into the directory oflab3part2
, and test your program against the test cases by typing the following commands:
cd /workspace/lab3part2
test
- If your program passes the test cases, you can submit your code by typing the following commands.
git add part2.c
git commit -m "Lab 3: Part 2"
git push
Part 3: ATM Machine
Write a C program that simulates an ATM machine distributing cash to a customer. The program should prompt the user to enter an amount in dollars. Ensure the amount is a multiple of $5. If not, repeatedly prompt the user until a valid amount is entered.
Then, the program should calculate and display the number of bills for the denominations $100
, $50
, $20
, $10
, and $5
that sum to the entered amount, using the largest denominations first.
Only print the denominations used (e.g., if no $50
bills are needed, do not print $50: 0
).
You can assume no negative numbers will be entered, no more than 100 bills of any denomination will be needed, and the entered amount will an integer.
Here is an example of the program's output:
Example 1:
Please enter an amount in dollars (
$
): 450$
100: 4$
50: 1
Example 2:
Please enter an amount in dollars (
$
): 453 The amount should be a multiple of$
5: 432 The amount should be a multiple of$
5: 324 The amount should be a multiple of$
5: 325$
100: 3$
20: 1$
5: 1
Testing and Submission
-
Create a new file called
part3.c
in thelab3part3
folder. -
Edit your code in
part3.c
, compile it, and run it to produce the output shown above. You can use the handy buttons at the bottom or thecompile
andmeson-build/part3
to compile and run your code. Before using the commands, you mustcd /workspace/lab3part3
to change the directory to part 3. -
Once you have the correct output, go to the terminal,
cd
into the directory oflab3part3
, and test your program against the test cases by typing the following:
cd /workspace/lab3part3
test
- If your program passes the test cases, you can submit your code by typing the following commands.
git add part3.c
git commit -m "Lab 3: Part 3"
git push
Verifying Your Submission
Go to CompEng.gg and click on the course list, or go directly to APS105. You should see a submission on the website, showing you the same issue when you tested your code yourself.
Under "Lab 3", you'll see "Pushes", we'll test your code for every push you run.
You may push as many times as you wish before the due date, we'll always take your highest grade.