# Automatic tables

## 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, Wiris Quizzes 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 very simple 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 behavior for a list inside a table cell is to fill the table towards the right. However, in many settings we would like to have a list of numbers expanding downwards. 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 *Define random variables and functions* 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:

LIVE DEMO

### 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 size 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 Variables tab:

LIVE DEMO

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:

LIVE DEMO

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, all we have to do is 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:

LIVE DEMO

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

Wiris Quizzes 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 Wiris CAS by storing them all in a matrix.

### Basics

The most basic instance of this feature is actually a very useful one. Placing an *n*x*m* 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 *N* 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 Wiris CAS.

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, looks like:

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 exactly 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.

To declare a random matrix, we should be familiar with the "list of lists" syntax for matrices. It's very simple though; each bracketed list is just a matrix row:

For the purposes of making 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:

LIVE DEMO

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 simpler way of making a random matrix may suffice in some cases, for example:

LIVE DEMO

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.