# Automatic tables

It is possible to generate tables from variables created in our algorithm. An essential feature is to create tables from lists. It is also possible to generate a table from a matrix. In the following sections, we explain both options.

## Generating tables from lists

Often, the data we wish to supply for a problem statement is best displayed as the contents of a table. However, editing each cell individually can be tedious, especially if we want to use variables to create a random table. Luckily, WirisQuizzes can easily convert list variables into table cells, which cuts quite a bit of the workload. But more interestingly, this feature allows us to create tables of variable size (see the last section for details).

### Basics

It's straightforward to use this functionality. In the simplest case, we need only place a list variable inside a single table cell (you can make a table either with the table wizard or by making a table yourself in HTML mode). Let's see how it turns out.

#### Writing the statement

Create a short answer question, and type a variable inside a 1x1 table: #### Create a list

Define `a` as a list in the Variables tab: #### View the results

Now when we go to the question, we see that the numbers in the list have automatically expanded into individual cells in a row: As we've seen in this example, the default behaviour for a list inside a table cell is to fill the table towards the right. However, we would like to have a list of numbers expanding downwards in many settings. This is achievable simply by placing a cell above the variable cell, as in the following example: The above input, in the question statement, becomes: #### Example

Let's put what we've seen to use in a real question. A typical exercise in entry-level probability is to answer questions on a discrete probability distribution, using a table to describe it.

By using variables to fill out the table we can exploit the random capabilities at our disposal to create a question that is different each time it is opened. Let's input the following in the editor: In the Random variables section we have to declare the lists and calculate the answers: Our question is actually a list of three questions, so firstly we'll have to write them in the Correct answer field. Then, we'll have to select Grade answer boxes separately in the Input options section, and the grade weight for each subquestion. Finally, when we view the question we see the table expanded as so: ### Randomly sized tables

It's possible to make the size of a table depend on a variable if we so wish. The actual work involved has to do more with knowing how to make a list of variable sizes since we've already seen how lists can automatically expand into tables. There are many options for defining such lists, but we'll go through the basic ones.

#### Example 1

Say we wanted the probability distribution in the previous example to have a random number of elements. We would then use the following command in the Define random variables and functions section: As we see, N above could have been the result of any other previous computation (provided it produces a number). This lets us be quite creative in making random lists.

#### Example 2

Another common type of list is one with its elements evenly spaced between the endpoints, typically representing points in an interval of real numbers. Let's take a look at the following command: This means, in plain English, "a list of numbers from 0 to 3, spaced by increments of 0.5".

To make lists of this style random, we have to replace the numbers in the definition with variables. In this example, we keep the start point and the increments the same, but we randomize the endpoint: There are many other possibilities for defining random lists. Any parameter that one would use to create a list can be replaced with a variable, so our creativity can take us as far as we want.

## Generating tables from matrices

WirisQuizzes supports the option of rendering matrix variables as tables. In other words, entries in a matrix can optionally be visualized as individual cells in a table. Or from another viewpoint, table cells can be more easily manipulated in CalcMe by storing them all in a matrix.

### Basics

The most basic instance of this feature is actually a beneficial one. Placing an nxm matrix variable inside a 1x1 table will create a table with n rows and m columns, each cell containing the corresponding matrix entry. Let's take a look. The 1x1 table in the editor where M is defined, for example, as produces the following table when we view the question: As mentioned previously, this means we can essentially store and edit arbitrary tables as matrices in CalcMe. As with lists, this feature also works when the matrix variable has other cells around it. A short example would be: and in the question statement, it looks like this: In general, if other cells are present (as opposed to placing a matrix in a one-cell table like above), the space for the matrix should already be there, in at least one dimension (because the table expansion will avoid creating empty cells). In the previous example, the table already had the same number of columns as the matrix, and we could have added as many rows as desired.

Lastly, note that automatic table expansion will avoid overwriting existing cells. Use the following example as a guideline: with M as before, this does not overwrite the occupied cell and simply creates a table with all of M inside one cell: ### Random tables

Analogous to the list functionality, matrices can also be used to generate random tables. The idea is precisely the same as with lists, we only need to know how to declare a random matrix variable, and the table that results will automatically adapt. We should be familiar with the "list of lists" syntax for matrices to declare a random matrix. It's straightforward, though; each bracketed list is just a matrix row: To make a table, we could also use curly brackets `{}` instead of square brackets. However, we should be aware that the resulting object in CalcMe, in this case, will be an actual list of lists and not a matrix. Anyhow, let's try this in the question editor, and add some randomness with the help of list comprehension: If we place N as in the first example on this page, we'll have achieved a table with random integer entries, with m rows and n columns, where n and m are also randomized. A more straightforward way of making a random matrix may suffice in some cases, for example: It gives us a matrix of fixed size but with one variable entry. Again, the possibilities for declaring random matrices are as many as our creativity allows.