Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Pre-requisite Review

all three files inside the upload dropbox

Problem 1

(20 marks)

The factorial of a non-negative integer n , denoted by n!n!, is the product of all positive integers less than or equal to n . The factorial of n also equals the product of n with the next smaller factorial:

n!=n×(n1)×(n2)×(n3)××3×2×1=n×(n1)!n ! = n × ( n − 1 ) × ( n − 2 ) × ( n − 3 ) × ⋯ × 3 × 2 × 1 = n × ( n − 1 ) !

For example, 5!=5×4!=5×4×3×2×1=120.5 ! = 5 × 4 ! = 5 × 4 × 3 × 2 × 1 = 120. The value of 0! is 1, according to the convention for an empty product.

Write a recursive C function to compute factorial of a natural number n. It should pass the test_factorial function 10 times. test_factorial function is given.

C programmers: download and edit the file test_factorial_template.c

Rename the file to test_factorial.c. Complete the function and submit the file as a separate file to Gradescope autograder.

Problem 2

(20 marks)

A natural number (1, 2, 3, 4, 5, 6, etc.) is called a prime number (or a prime) if it is greater than 1 and cannot be written as the product of two smaller natural numbers.

Write a C function to calculate if a number is prime. Return 1 if it is prime and 0 if it is not a prime. If the number is not a prime number, then a factor exists. Return the factor as through a pointer.

C programmers: Download and edit the file test_prime_template.c. Complete the function and submit the file as a separate file to gradescope.

Rename the file to test_prime.c. Complete the function and submit the file as a separate file to Gradescope autograder.

Problem 3

(20 marks)

2.a. Write a C data structure named struct date that capture year, month and days of a date.

2.b. Also write a function greg_is_leap_year that determines whether an year is a leap year.

2.c. Also write a function date_greg_days that count the total number of days since 0001-01-01 AD.

C programmers: Download and edit the file test_date_template.c. Complete the function and submit the file as a separate file to gradescope.

Rename the file to test_date.c. Complete the function and submit the file as a separate file to Gradescope autograder.

Problem 4

(50 marks)

Please review Linear Algebra concepts and notations from here and answer the following questions (all answers are in the pdf).

  1. (2 marks) In matrix notation defined in the linked document, when I say a matrix is n×mn \times m (alternatively the matrix is in the set Rn×m\mathbb{R}^{n \times m}, does the n refer to the number of rows or the number of columns .

  2. (5 marks) Let the matrix AR4×2A \in \mathbb{R}^{4 \times 2} and BR2×3B \in \mathbb{R}^{2 \times 3} be defined as

    A:=[12357111317],B:=[192329313741]A := \begin{bmatrix} 1 & 2 \\ 3 & 5 \\ 7 & 11 \\ 13 & 17 \\ \end{bmatrix}, \\ B := \begin{bmatrix} 19 & 23 & 29 \\ 31 & 37 & 41 \\ \end{bmatrix} \\

    Is the matrix multiplication ABAB valid? Make up an example of matrix BB when matrix multiplication ABAB would not have been valid. Find out the matrix multiplication C=ABC = AB by hand. Write down all the steps to show which numbers get multiplied by which numbers.

  3. Given two matrices AA of size m×nm \times n and BB of size p×qp \times q, when is the matrix multiplication ABAB valid? When is the matrix multiplication BABA valid? When is the addition A+BA + B valid? (5 marks)

  4. What is the transpose of a matrix? If a matrix AA has the shape n×mn \times m, what is the shape of matrix AA^\top (3 marks).

  5. Define dot product for two vectors? How to test when two given vectors are perpendicular? Assume you have two n-dimensional vectors a=[a1,a2,,an]\vec{a} = [a_1, a_2, \dots, a_n] and b=[b1,b2,,bn]\vec{b} = [b_1, b_2, \dots, b_n]. Denote dot product as, ab\vec{a} \cdot \vec{b}? (5 marks)

  6. Denote the above vectors as column matrices. Define the following n×1n \times 1 matrices with the vector components.

    a=[a1a2an],b=[b1b2bn]\mathbf{a} = \begin{bmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{bmatrix},\\ \mathbf{b} = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_n \end{bmatrix}

    Use matrix operation like matrix transpose and matrix multiplication to write vector dot product (5 marks).

  7. Define cross product for two vectors? How to test when two vectors are parallel to other? (5 marks)

  8. How to can you find the magnitude of a vector? What is a unit vector? (5 marks)

  9. What is a square matrix (0 marks)?

  10. What are the column vectors and row vectors of a matrix? How can you write matrix multiplication in terms of row vectors and column vectors of a matrix (5 marks)?

  11. Suppose you are given a n×nn \times n square matrix called UU. All it’s nn columns vectors are unit vectors and every pair of the unit vectors are perpendicular to each other. Find out the product UUU^\top U . What is the name of given to such matrices whose column vectors are unit vectors and are perpendicular to each other? (10 marks).

    Hint: Assume the column vectors of the matrix UU be u1,,un\mathbf{u}_1, \dots, \mathbf{u}_n. Then the UU matrix can be written as,

    U=[u1u2un]U = \begin{bmatrix} | & | & & | \\ \mathbf{u}_1 & \mathbf{u}_2 & \dots & \mathbf{u}_n \\ | & | & & | \\ \end{bmatrix}

    The transpose of matrix UU is

    U=[u1u2un]U^\top = \begin{bmatrix} \rule{2em}{0.4pt} \mathbf{u}_1^\top \rule{2em}{0.4pt} \\ \rule{2em}{0.4pt} \mathbf{u}_2^\top \rule{2em}{0.4pt} \\ \vdots \\ \rule{2em}{0.4pt} \mathbf{u}_n^\top \rule{2em}{0.4pt} \\ \end{bmatrix}

    Multiply them together to find the answer.