Due Date: ~~Friday, June 5, 2020 at 9:00 pm~~ Sunday, June 7, 2020 at 9:00 pm.

- Before every assignment, make sure you read the
**updated**preamble. - Monitor the OFFICIAL A2 Piazza Post for any corrections, updates, or frequently asked questions.
- The assignment files are available here (they are also provided in Seashell).
- The questions are listed in (approximate) course chronological order, not necessarily in order of their difficulty or the amount of effort required.
- This assignment covers material in the notes up to Section 04 – slide 52.

In this assignment, you must only use the C features covered in class up to Section 04 – slide 52.

In this assignment, you must use **iteration** (loops), i.e., you may **not** use recursion. The only exception is q2a-recursion.

No `int` in any of the questions or your tests can exceed `+/- 2,147,483,647`. None of our private tests will intentionally cause an integer overflow.

This question involves printing out patterns using iteration.

Review the provided sample.expect files to see how each shape is drawn. Remember, only print newlines after each line of your output: you must not print any "extra" (double) newlines.

- [q1a-xbox]:
`xbox(width, height)`draws a box with a given width and height and draws an`'X'`in its centre.

- [q1b-saturn]:
`saturn(diameter)`draws a solid, filled circle with a ring in its centre. The circle is filled with`'*'`; the outside of the circle is empty (`' '`). The ring is three characters tall; the outer characters of the ring are`'='`, the inner characters`'#'`. There are several algorithms to determine if a point lies on the inside or the outside of a circle; we recommend using the Midpoint circle algorithm.

**Hints:**

- Remember that you don't necessarily have to initialize loop variables with 0 or 1!

- [q1c-cube]:
`cube(dimension)`draws a perspective cube with`dimension`as its width and height, and`dimension / 2`(rounded up) as depth.

`my_pow(b, e)` returns `b` to the power of `e`.

`count_digits(n)` returns the number of digits in `n`, for example, 1234 => 4.

`sum_digits(n)` returns the sum of all digits in `n`, for example, 1234 => 1 + 2 + 3 + 4 = 10.

`is_prime(n)` returns `true` if `n` is prime, and `false` otherwise. The Sieve of Eratosthenes is a simple and easy to implement primality test.

- [q2a-recursion]: Implement all four functions (
`my_pow`,`count_digits`,`sum_digits`, and`is_prime`). You must use recursive algorithms; you may not use any iterations (loops)! - [q2b-iteration]: Implement all four functions (
`my_pow`,`count_digits`,`sum_digits`, and`is_prime`). In addition, also implement`fibonacci`, a function that calculates the Fibonacci Number (see Assignment 1). You must use iterative algorithms (loops); you may not use any recursion!

Benford's Law states that in many naturally occurring collections of numbers, smaller digits (e.g., 1) appear more often as leading significant digits compared to larger digits (e.g., 9). We think this law is cool because it models many naturally occurring phenomena, especially those that follow an exponential growth pattern (populations, for example). We will get back to Benford's Law later.

Histrograms are a particular type of visualization for distributions, frequently used in descriptive statistics. They mostly presented as a bar-chart: on the x-axis, you have an arbitrary number of categories (oftentimes called "bins" or "buckets"); on the y-axis, you have the count (or tally) of values in each category.

- [q3a-histogram]: Implement the function
`draw_hist(..)`. This function draws a histogram for exactly ten categories. The bar for each category is one character wide (`'#'`), and there is a one-character gap between each bar as well as between the y-axis and the first bar (`' '`). The histogram also has numbers along the y-axis every 5 units; use the following statement to print the number and the y-axis:`printf("%2d |", ...)`. (The printf-command "%2d" instructs`printf`to print the digit with at least two characters, which makes formatting easier. You can assume that no count in any category will exceed`99`.) The x-axis is made of dashes (`-`); the labels on the x-axis, which represent the 10 categories, are the integers`0 ... 9`. Make sure to analyze`single.expect`to learn more about the output format.

Now, let's get back to Benford's Law:

- [q3a-benford]: Write a program that reads an arbitrary number of integers from input, counts the occurrences of the integers
`1 ... 9`as leading significant digits, and prints out these counts in the form of a histogram.

If you think about Benford's Law, it make sense why it holds true for the *leading significant digits* of a series of numbers. But does it also hold true if you look at *all digits* in a series of numbers? For example, in the two numbers 38014184 and 14711827 (population of Canada and Ontario) the digits 0, 2, and 3 appears once, the digit 7 twice, the digits 4 and 8 thrice, and the digit 1 five times.

- [q3c-benford-reanalyzed]: Write a program that reads an arbitrary number of integers from input, counts the occurrences of the integers
`0 ... 9`within these numbers, and prints out these counts in the form of a histogram.

**Hints:**

- We recommend using our provided
`draw_hist`-function; this assures that your output format matches exactly the one expected by Marmoset.~~You may use our provided~~`draw_hist`-function. Alternatively, you may copy and paste your draw_hist(..) function from part (a) into parts (b) and (c). In this case, comment out the line`#include "hist.h"`. - Make sure to use a divide-and-conquer approach: break down the problem into multiple parts and solve them separately.
- Make sure to avoid code redundancies, i.e., having the same code multiple times. Instead use helper functions!
- In part (b), you only have 9 categories to print (
`1 ... 9`), although`>draw_hist`requires 10 parameters (`c_0 ... c_9`). Simply set the parameter`c_0`to`0`! - Remember that you may not use global mutable variables. This means that you have to store your tallies for
`0 ... 9`in one of the functions, probably in`main`. (There is a much better solution using arrays, which we will learn about later.) This also means that you maybe have to be a little bit "clever" when analyzing an integer. Remember that it is okay (for now) to write inefficient code.

For this question, the Marmoset basic tests ("public" tests) will be the same as the I/O test provided in `simple.in`.

The look-and-say sequence is a fun sequence of numbers where every number is generated by counting the number of unique digits of each kind in the previous number. We adopt one of a few variations of this sequence. Please pay attention to how we define our sequence below.

In the example below, our look-and-say sequence beginning with the seed 1:

1, 11, 21, 1112, 3112, 211213, 312213, 212223, 114213, 31121314, 41122314, ...

To generate a member of the sequence from the previous member, read off each unique digit in the previous member from 0 to 9, counting the total number of times the digit appears in the previous member.

For example, suppose that a member in the sequence is 1112. We will generate the next member in the sequence as follows.

In "1112", we check for any occurence of "0". Since there is no "0", we move on to "1". There are three "1" in "1112", and we can read this off as "three 1". Next, there is one "2", which we can read off as "one 2". Last, there are no digits between "3" and "9" in "1112", so we ignore these digits. This leaves us with "three 1" and "one 2", which becomes "3112".

Going through the sequence above results in:

- Starting with 1, the next member is "one 1", or 11.
- After 11, the next member is "two 1", or 21.
- After 21, the next member is "one 1, one 2", or 1112.
- After 1122, the next member is "three 1, one 2" or 3112.
- After 3112, the next member is "two 1, one 2, one 3" or 211213.
- After 211213, the next member is "three 1, two 2, one 3" or 312213.
- And so on...

For this assignment question write a program that reads exactly two integers, `seed` and `n`, from input and prints out the first `n` members of the look-and-say sequence starting with the number `seed`. Use `printf("%d\n", ...);` to print each member. If any of the errors below occurs, print out the error message `"Invalid inputs.\n"` and exit.

`READ_INT_FAIL`occurs when reading either`seed`or`n`.`seed`is not a positive integer.`n`is not a positive integer.

**Hints:**

- Some of the required functionality in this question might be similar to the ones in Q3b and Q3c. Feel free to use some of code you wrote there in Q4 as well; don't reinvent the wheel!

For this question, the Marmoset basic tests ("public" tests) will be the same as the I/O test provided in `simple.in`.

You don't need to do anything for this question. By having this question, we want to let you know that one or more of the questions on this assignment will be hand-marked for coding style and the quality of your tests. We are not stating which question(s) because you should use proper style and testing on all questions.