Skip to main content

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

  1. 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 is https://github.com/compeng-gg/2025-winter-aps105-eyolfso3.

  2. Open the same codespace you created for lab0. You should not create any other codespaces. Existing codespace in your GitHub repository Fig.1 Existing codespace created in your GitHub repository

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

    Merge Fig.2 Merge pull message

    note

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

  4. You should see new folders called lab3part1, lab3part2, and lab3part3 in your repository.

important

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:

Handy Buttons

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 (C^\circ C) (as an integer), and identifies the substance if the observed boiling point is within tCt ^\circ C of the expected boiling point, where tt is a threshold defined by the user. If the data input is not within tCt ^\circ C of any of the boiling points in the table, the program should output the message: Substance unknown.

SubstanceNormal boiling point (C^\circ C)
Water100
Mercury357
Copper1187
Silver2193
Gold2660

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

  1. Create a new file called part1.c in the lab3part1 folder.

  2. 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. Handy Buttons

    Fig.3 Handy buttons to compile, run, and debug your C program.

    You also compile using the terminal using compile command, and run using meson-build/part1 command. You should cd /workspace/lab3part1 before running these commands.

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

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.

warning

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 α\alpha in the following figure.

Robot Shooting

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.

  1. The speed with which the robot throws the ball is v=20v = 20 m/s.
  2. The robot will stand at a horizontal distance dd between 3 and 30 m (inclusive).
  3. The basketball rim will be at a vertical height HH between 3 and 6 m (inclusive).
  4. The height of the robot is l=2l = 2 m.

The following equations are used to solve the problem:

vx=vcos(α),(1) v_x = v \cdot cos(\alpha), \tag{1} vy,0=vsin(α),(2) v_{y,0} = v \cdot sin(\alpha), \tag{2} ttravel=dvx=dvcos(α),(3) t_{travel} =\frac{d}{v_x} = \frac{d}{v \cdot cos(\alpha)}, \tag{3} y=l+vy,0t12gt2,(4) y = l + v_{y,0} \cdot t - \frac{1}{2} \cdot g \cdot {t}^2, \tag{4}

where ttravelt_{travel} is time taken to travel the horizontal distance dd, yy is the vertical height after tt time, g=9.81m/s2g = 9.81 m/s^2.

Your program should take in from the user the horizontal distance, dd, from the basketball rim, and the vertical distance, HH of the basketball rim. It should check that d=[3,30]d = [3, 30] and H=[3,6]H = [3, 6]. If not, your program should prompt the user to re-enter the values.

Your program should then iterate through α\alpha from α=0°\alpha = 0^{\degree} to 90°90^{\degree} by increments of 1°1^{\degree}. Then, calculate ttravelt_{travel} and yy until you find an α\alpha that leads to a 0.3m0.3 m difference between yy and HH. yy is calculated using Eqn.(4) above by substituting tt with ttravelt_{travel}.

Once your program finds an α\alpha that makes yH0.3|y - H| \leq 0.3, it should print the value of this α\alpha.

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

  1. Create a new file called part2.c in the lab3part2 folder.

  2. 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 the compile and meson-build/part2 to compile and run your code. Before using the commands, you must cd /workspace/lab3part2 to change the directory to part 2.

  3. Once you have the correct output, go to the terminal, cd into the directory of lab3part2, and test your program against the test cases by typing the following commands:

cd /workspace/lab3part2
test
  1. 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

  1. Create a new file called part3.c in the lab3part3 folder.

  2. 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 the compile and meson-build/part3 to compile and run your code. Before using the commands, you must cd /workspace/lab3part3 to change the directory to part 3.

  3. Once you have the correct output, go to the terminal, cd into the directory of lab3part3, and test your program against the test cases by typing the following:

cd /workspace/lab3part3
test
  1. 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.