Skip to main content

Lab 4: Loops and Functions

This is a new version of lab 4 released on January 31 at 5:44 PM. The old version had the solutions released by mistake! Enjoy coding!

Objective

The objective of this lab is to develop two simple C programs. The first program is estimating the Pi, and the second program is counting the number of "three-seven numbers" numbers.

Grades

This lab is worth 9 marks, there is no in-person marking due to midterm practice.

The automarker breakdown is as follows:

  • Part 1: Estimating the square root (4 marks: 3 public, 1 private)
  • Part 2: Three-Seven Numbers (6 marks: 4 public, 2 private)

You can always see your public test cases automarker grades here APS105 after every submission. They are out of 10.

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 4. 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 lab4part1 and lab4part2 in your repository.

Part 1: Estimating Pi

You are not allowed to use the math library for this part.

We will estimate the value of π\pi using the Monte Carlo Estimation method by examining the ratio of points falling inside a quarter circle to the total points in the encompassing square.

Circle Quadrant

Fig.4 Circle Quadrant

Monte Carlo Estimation method depends on the following facts:

  1. The ratio of the area of a circle and the area of its encompasing square is circle areasquare area=πr24r2=π4\frac{\text{circle area}}{\text{square area}} = \frac{\pi r^2}{4 r^2} = \frac{\pi}{4}, where rr is the radius of the circle.

  2. The ratio is consistent for a quarter circle within a unit square as shown in Fig.4.

  3. The area of the square in Fig.4 is 1.

  4. The area of the circle in Fig.4 is πr24\frac{\pi r^2}{4}. Since the radius is 1, then it's π4\frac{\pi}{4}.

To estimate the value of π\pi,

  1. Generate (x,y)(x, y) coordinates in the dotted square in Fig.4,

  2. Determine the ratio of the number of points in the circle to the total number of points generated. Recall the equation for a unit circle is x2+y2=1x^2 + y^2 = 1.

  3. Estimate π\pi using π=4×number of coordinates in circletotal number of points generated\pi = 4 \times \frac{\text{number of coordinates in circle}}{\text{total number of points generated}}

To implement the Monte Carlo Estimation,

  1. Use the provided randDouble function below to generate two random numbers for the value of the xx and yy coordinate.
  2. Write a function, inBounds, the prototype of which is given below. The function receives the values of (x,y)(x,y) coordinates and returns true if they fall within the grey shaded region in Fig.4, and false otherwise.
  3. The user can input the number of iterations, for each iteration generate one point and your program should count how many generated points fall in the grey area after all the iterations. The estimated value of π\pi should be rounded to 4 decimal places.
  4. You MUST set your seed to 42 while submitting your code. Otherwise, your code will fail test cases on CompEng.gg.
double randDouble() {
return (double)rand() / ((double)RAND_MAX);
}

bool inBounds(double x, double y) {

}

Here is a sample output from an execution of the functioning program. For the same seed, the random number will be different on different machines. Hence, do not panic if the numbers in this sample output is different on your local computer. You should see the same numbers on CompEng.gg with seed set to 42.

Example 1

Number of monte carlo iterations: 10 Pi: 3.6000

Example 2

Number of monte carlo iterations: 100000 Pi: 3.1374

Testing and Submission

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

If your program failed the test cases and you want to view the input, you can open 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 lab4part1/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: Three-Seven Numbers

You are not allowed to use the math library for this part.

Write a program which counts the number of "three-seven numbers" a user inputs. A "three-seven number" is defined as a number containing exactly three 7s in its digits. The keyword here is exactly.

The program:

  1. Repeatedly prompts the user to enter an integer. The user can enter as many numbers as they wish, one at a time. To stop entering numbers, the user inputs 0.

  2. Checks if each entered number is a "three-seven numbers". If it is, the program increments a counter.

  3. Once the user inputs 0, the program stops accepting inputs and prints out the total count of "three-seven numbers" entered.

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

Example 1

Input an integer: 777 Input an integer (0 to stop): 0 You entered 1 lucky number(s)!

Example 2

Input an integer: 132 Input an integer (0 to stop): 71237 Input an integer (0 to stop): 71771 Input an integer (0 to stop): 1727378 Input an integer (0 to stop): 0 You entered 2 lucky number(s)!

Example 3

Input an integer: 71237 Input an integer (0 to stop): 717717 Input an integer (0 to stop): 1727378 Input an integer (0 to stop): -1742797 Input an integer (0 to stop): 0 You entered 2 lucky number(s)!

Testing and Submission

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

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

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