Lab 19: Factorial Function

Getting Started

If you have a solution open in Visual Studio, close it (File->Close Solution).

Factorials

In this lab you will write a function to compute the factorial of a positive integer, and write tests to ensure that the function returns the correct answer for the values 1 through 7.

Recall that the factorial of a positive integer n is defined as the product

1 x 2 x 3 x ... x n

The following table shows the factorials of 1..7:

Input value Expected result value
1 1
2 2
3 6
4 24
5 120
6 720
7 5040

1. Implement the factorial function so that it returns the factorial of the int value given as its parameter.
2. Add calls to the ASSERT_EQUALS function to the main function to test whether or not the factorial function computes the correct answer for the values 1 through 7.

To compute the factorial, use the same approach as in Lab 7: traverse the sequence 1 through n using a loop, and use an accumulator variable to incrementally build up the overall factorial.

When calling the ASSERT_EQUALS function, the first argument value is the expected value of a particular factorial (e.g., 720), and the second argument value is computed by a call to the factorial function (e.g., factorial(6)). The ASSERT_EQUALS function will compare the expected value to the computed value to make sure they match. If they do not match, it will cause the program to exit. Otherwise, it prints a message stating that the test was successful, and the program continues.

You are done when the program produces output similar to the following:

```Checking that 1 == factorial(1) at line 17: YES
Checking that 2 == factorial(2) at line 19: YES
Checking that 6 == factorial(3) at line 20: YES
Checking that 24 == factorial(4) at line 21: YES
Checking that 120 == factorial(5) at line 22: YES
Checking that 720 == factorial(6) at line 23: YES
Checking that 5040 == factorial(7) at line 24: YES
All tests passed!
```

Submitting

When you are done, choose Tools->Submit to submit your completed lab to the server. You will need to enter your Marmoset username and password.

If the Tools->Submit option does not work, create a zip file containing the files in your project, and upload it to the submission server as lab19. The server URL is

https://camel.ycp.edu:8443/