Skip to main content

Lab 2: More Complex Calculations

Objective

The objective of this lab is to develop three simple C programs. The first program will calculate the escape velocity of a planet given its circumference and acceleration due to gravity. The second program will decipher a code that swaps the 1st and 4th digits of a 4-digit number and replaces the 2nd and 3rd digits with their 9's complement. The third program will calculate the electrostatic force between two charges using Coulomb's Law.

Grades

This lab is worth 10 marks.

The automarker breakdown is as follows:

  • Part 1: Escaping A Planet (2 marks: 1 mark for passing the test cases given in this sheet, 1 mark for passing a hidden test case)
  • Part 2: Deciphering a Code (2 marks for passing the test cases given in this sheet)
  • Part 3: Coulomb's Law (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 5.

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 January 31, 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 2. 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 lab2part1, lab2part2, and lab2part3 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: Escaping A Planet

In order for an object to escape a planet's gravitational pull, it must attain a minimum initial velocity called the escape velocity. The escape velocity varies from planet to planet but it is the same for all objects on a given planet. Assume that we are analyzing the data that a small probe has collected while exploring some mystery planet. The probe has managed to obtain the circumference of the planet and the acceleration due to gravity at the surface. The probe must now determine what initial velocity it requires for takeoff in order to remove itself from the planet's gravitational force.

Write a C program that will determine this velocity. Your program should first prompt the user for the circumference of the planet and then the acceleration due to gravity on the planet. From this information your program should determine the radius, mass, and escape velocity of the planet using the following equations.

vescape=2Gmrv_\mathrm{escape} = \sqrt{\frac{2Gm}{r}} a=Gmr2a = \frac{Gm}{r^2}

In these equations, mm kg is the planet's mass, rr km is the planet's radius, GG is the gravitational constant approximated by 6.6726×1011m3kg1s26.6726 \times 10^{-11} {\mathrm{m}^3 \mathrm{kg}^{-1} \mathrm{s}^{-2}}, and aa m/s2\mathrm{m}/\mathrm{s}^2 is the acceleration due to gravity on the surface of the planet.

note

The circumference of a circle is 2πr2\pi r where rr is the radius of the circle.

Your program should print messages for the user and produce output as shown below. This follows the same format as described in previous examples. Inputs are shown in bold.

Circumference (km) of planet: 38000 Acceleration due to gravity (m/s^2) on planet: 9.8

Calculating the escape velocity... Planet radius: 6047.9 km Planet mass: 5372.0 x 10^21 kg Escape velocity: 10.9 km/s

Note: All results are rounded to one decimal place for printing. Your program must handle the unit conversions as well as printing the planet's mass in terms of 102110^{21} kg. Assume that this will always produce reasonable results. Assume that the planet is perfectly spherical.

Testing and Submission

  1. Create a new file called part1.c in the lab2part1 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/lab2part1 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:

cd /workspace/lab2part1
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 2: 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 lab2part1/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, and push them. After you push all three parts, you can see your grade on the CompEng.gg.

Part 2: Deciphering a Code

You want to use a 4-digit combination lock on a safe where you put your belongings when you go skiing. As a matter of good practice, you want to change the combination every time you go. Since you do not want to worry about people finding out what your combination is, you write it on a paper using a coding scheme. If anyone reads your piece of paper, they will still not know your combination.

The scheme you use takes the real combination and swaps the 1st and the 4th digit of the combination, and replaces each of the 2nd and 3rd digits by their 9's complement. In other words, a combination of the form abcd is encoded as d(9-b)(9-c)a.

For example, if the actual combination is 0428, the encrypted combination will be 8570 (note that 0 and 8 are swapped, 4 is replaced by 9-4, and 2 is replaced by 9-2). The nice thing about his coding scheme is that you can apply it again on the encrypted combination to calculate the real one.

Since you are now taking APS 105, your task is to write a C program to implement this code-decode scheme. The user (you!) will, then, enter an encrypted combination and the program will output the real combination. A sample output from an execution of the program appears below:

Enter an encrypted 4-digit combination: 8021 The real combination is: 1978

Note: You may assume that the entered combination is a valid 4-digit integer number. When reading the 4-digit combination from user input, you are not allowed to scan in individual characters; instead, you should scan in a single integer using scanf.

Testing and Submission

  1. Create a new file called part2.c in the lab2part2 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/lab2part2 to change the directory to part 2.

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

cd /workspace/lab2part2
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 2: Part 2"
git push

Part 3: Coulomb's Law

Write a C program that calculates the electrostatic force between two charges using Coulomb's Law. The law is mathematically expressed as

F=kq1q2r2F = k \cdot \frac{|q_1 \cdot q_2|}{r^2}

where FF is the electrostatic force, kk is the Coulomb's constant 8.9875×109Nm2/C28.9875 \times 10^9 N\cdot m^2/C^2, q1q_1 and q2q2 are the values of two point charges in Coulomb (C) and rr is the distance between the charges in meters (m).

Your program should accept the values of q1q_1 and q2q_2 as input, along with the units in which they are expressed. Assume the user will enter the unit as either nC for nano Coulomb or uC for micro Coulomb. Hint: You can read teh charge and individual characters from the input using scanf with multiple format specifiers.

The user also enters the distance between the charges as a floating point number in meters. The user will not enter the unit of the distance.

You need to convert the value of q1q_1 or q2q_2 to Coulomb to calculate the force using Coulomb's law. You are allowed to include the math.h library. The equation outputs force in Newton (N).

Print the force rounded to 2 decimal places. If the force is strictly less than 1 micro Newton (1μN1\mathrm{\mu}N), use nN unit and print that it is (less than 1uN). Otherwise, if the force is strictly less than 1 milli Newton (1mN1mN), use uN unit and print that it is (less than 1mN). Otherwise, if the force is strictly less than 1 Newton, use mN unit and print that it is (less than 1N). Otherwise, use N unit and print that it is (1N or greater).

Here is a sample output from an execution of the program:

Example 1:

Enter the value of the two charges separated by a space: 2nC 3uC Enter distance between charges in metres: 2 The force between charges is 13.48uN (less than 1mN)

Example 2:

Enter the value of the two charges separated by a space: 3.2nC -7.2nC Enter distance between charges in metres: 13.78 The force between charges is 1.09nN (less than 1uN)

Submission

  1. Create a new file called part3.c in the lab2part3 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/lab2part3 to change the directory to part 3.

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

cd /workspace/lab2part3
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 2: 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 2", 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.