Skip to main content

Lab 1: Simple Calculations

Objective

The objective of this lab is to develop three simple C programs. The first program will print a series of escape sequences. The second program will calculate the total amount that a customer should pay for a given number of donuts. The third program will convert a given length in centimeters to feet and inches. You should know how to use escape sequences, arithmetic operators, and data types in C.

Grades

This lab is worth 10 marks.

The automarker breakdown is as follows:

  • Part 1: Printing (1 mark)
  • Part 2: iDonuts Shop (3 marks: 2 marks for passing the test cases given in this sheet, 1 mark for passing a hidden test case)
  • Part 3: Convert Centimeters to Feet and Inches (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.

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 Grades under Notes, e.g. TA: TA NAme, Section: PRA010x, Workstation: Pxx, Location: SF 101x. 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 24, 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) lab1 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 1. 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 lab1part1, lab1part2, and lab1part3 in your repository.

Part 1: Printing

Write a C program that prints exactly the following output:

C uses escape sequences for a variety of purposes.
Some common ones are:
to print ", use \"
to print \, use \\
to jump to a new line, use \n

Testing and Submission

  1. Create a new file called part1.c in the lab1part1 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/lab1part1 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/lab1part1
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 1: Part 1"
git push

Part 2: iDonuts Shop

iDonuts shop is making an offer that states "Buy 3 Get 1 Free." This means that if a customer asks for 4 donuts, they will pay only for 3. While if the customer asks only for 3 donuts, they will pay for 3 only. If the customer asks for 9 donuts, they will pay for 7 only.

Write a C program that helps the register to calculate the total amount that the customer should pay given the number of donuts they buying. The program should ask the user to enter the number of donuts that the customer wants to buy and the price for each donut. The tax rate is fixed to 13%. The program should print the total amount that the customer should pay rounded to the nearest cent. Hint: Use the the format specifier of double data type to print the amount rounded to the nearest cent.

Here is a sample execution of the program:

warning

Input is shown in bold.

Enter the price per donut in CAD 1.5 Enter the number of donuts 4 You are getting 1 free donut(s) You should pay $5.08

Enter the price per donut in CAD 2.7 Enter the number of donuts 9 You are getting 2 free donut(s) You should pay $21.36

Testing and Submission

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

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

cd /workspace/lab1part2
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 1: Part 2"
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, in the following image, my program failed test cases 1 and 2. I can view the inputs of test case 2 by opening the file 2-free in the lab1part2/tests/public/inputs/ directory. You need to open 1-free file to view inputs to test case 1. Failed test case inputs Fig.3 View inputs to test case 2 in lab1part2.

Part 3: Convert Centimeters to Feet and Inches

You have a friend visiting from the US who is not familiar with the metric system. Write a C program that helps your friend convert a given length in centimeters to feet and inches. The program should ask the user to enter the length in centimeters and then print the equivalent length in feet and inches. The conversion is as follows: 1 foot = 12 inches and 1 inch = 2.54 cm. The program should print the length in feet and inches rounded to the nearest inch. Hint: Use the the format specifier of double data type to print the amount rounded to the nearest cent. The program should print the length in feet and inches without any fractional part in inches. Hint: Use type cast of int to display the inches.

Enter the length in centimeters (cm) 45 The length is 1ft 5in

Enter the length in centimeters (cm) 183 The length is 6ft 0in

Submission

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

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

cd /workspace/lab1part3
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 1: 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 1", 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.

When your submission is correct, you'll see something that looks as follows:

Correct Submission