# Extra exercises

February 2020

Back to index.html.

# Extra exercises

### Some numerics

1. Write a program to find triplets of integer numbers ($a$, $b$, and $c$) to satisfy $a^2+b^2=c^2$ where $0<a,b,c<500$.
2. [Optional] Write a program to find prime numbers from 1 to 100.

### Systematic covariance structure

1. Read one integer number ($n>0$) and one real number ($0<r<1$) from keyboard, then allocate a square matrix $\mathbf{M}$.
2. Make a tridiagonal matrix with 1 on diagonal and 0 or $r$ on off-diagonal with an arbitrary $n$. See the following example for $n=5$. $\mathbf{M}=\left[ \begin{array}{ccccccccc} 1&r&0&0&0\\ r&1&r&0&0\\ 0&r&1&r&0\\ 0&0&r&1&r\\ 0&0&0&r&1\\ \end{array} \right]$
3. Make an “autoregressive” covariance-structure matrix with an arbitrary $n$. See the following example for $n=5$. $\mathbf{M}=\left[ \begin{array}{ccccccccc} 1&r&r^2&r^3&r^4\\ r&1&r&r^2&r^3\\ r^2&r&1&r&r^2\\ r^3&r^2&r&1&r\\ r^4&r^3&r^2&r&1\\ \end{array} \right]$

### Symmetric matrix

1. Read one integer number ($n$) from the keyboard, and allocate a square matrix ($\mathbf{M}$). Make a symmetric matrix with random numbers with random_number.
2. Make a function to convert a square matrix to a symmetric matrix using its lower (or upper) triangular elements.

### Half-stored symmetric matrix

1. Consider a symmetric matrix ($\mathbf{M}$). How many elements at most do you need to store the unique elements? It means how many elements do you need to store the upper triangular and diagonal elements?
2. Make a symmetric matrix with random numbers. Create a subroutine to extract the lower (or upper) elements and put the elements into a vector ($\mathbf{m}$: packed “matrix”). Mathematically, it is called vec function.
3. Create a subroutine to expand a packed “matrix” ($\mathbf{m}$) to the full symmetric matrix $\mathbf{M}$. It is vech function.
4. [Optional] Create a function to compute the multiplication of a packed “matrix” ($\mathbf{m}$) by an arbitrary vector $\mathbf{x}$. The result should be the same as $\mathbf{Mx}$.

### Monte Carlo simulation

The random number ($r$) in this exercise is in $0\leq r < 1$, generated with random_number.

1. Generate two random numbers $r_1$ and $r_2$ at a time, and if $r_1^2+r_2^2\leq 1$, increment $c$. Repeat this process $N$ times. In the end, what number do you get as $4c/N$?
2. Generate 12 random numbers at a time, and compute $x$ as the sum of the random numbers. If $x-6$ is less than 1.96, increment $c$. Repeat this process $N$ times. What number do you get as $c/N$.
3. [Optional] Simulate the Monty Hall Problem.

### Fixed file format

1. Assume a fixed file format, but each field is separated by at least 1 space. Write a function to read the first row of a file and to determine the column of the last field. For example, when the row has A001 1 John, the function returns 10.
2. With the above exercise, create a subroutine to return the width of the last field in the first row.
3. Using the above procedures, create a subroutine to read the last field in a fixed-format file and print it to the screen.

Back to index.html.