dynamic array declaration in Formulas

dynamic array declaration in Formulas

by Oleg Melnikov -
Number of replies: 15

Big Thanks to Dominique Bauer and his team for creating and supporting Formulas quiz type. I'm gradually trying it for more complex questions relating to vectors and matrices. In particular, is there a way to define an array of randomly generated numbers at once, not individually, and then display it as an array?

For example, currently I define random variables:
x0={-100:100:.1};
x1={-100:100:.1};
x2={-100:100:.1};

Then I define a global variable x=[x0, x1, x2]; This allows me to operate on array at once. I also display these values as [{x0}; {x1}; {x2}] in the question text. I tried displaying {x}, but the value "{x}" was printed. I also could not find a way to create x in one line with a single definition. 

FYI: this example and this post were helpful  in making progress.

A somewhat related question. How is localization of number formatting handled in Formulas? As an example, last year I had a Canvas question displaying [x1, x2] with variables x1, x2, but because some localities flip "," instead of ".", the comma between the numbers raised plentiful confusion (and I replaced it with ";"). Is there a way to ensure that all numeric variables use the identical localization in Formulas questions?

Average of ratings: -
In reply to Oleg Melnikov

Re: dynamic array declaration in Formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Oleg,

Thank you for your kind words.

The Formulas question was created in her spare time by Ms. Hon Wai Lau, a young quantum physicist, when she was still a student. Jean-Michel Védrine, then professor of mathematics and statistics, now retired, provided maintenance for ten years during which he made important and difficult upgrades.

I haven't done much, other than promoting the question a bit, lest it be maintained any longer. Last year our team consisted of me and my cat, but he has since passed away, so the team is now reduced to me alone who, by the way, has a full-time job in in a whole different field.

So the team looks more like a few castaways on makeshift rafts, and there is only one badly injured survivor, often sad and angry.

This is the reality. Now the dream.

You can display the vector as you mention, ie [{x0}; {x1}; {x2}]. You can also display it like this [{=x[0]}; {=x[1]}; {=x[2]}]. As far as I know, there is no way to display the vector with just its {x} name. There is also no way to create this vector, with random numbers, in a single line.

There is no localization of number formatting in the Formulas question. There is only one decimal separator, the point (period, full stop). I still wrote a little script to accept student responses written with decimal commas, which you can find at https://moodleformulas.org/course/view.php?id=22&section=25 ↗.

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: dynamic array declaration in Formulas

by Oleg Melnikov -
Hi Dominique Bauer , thanks for a very useful reply and sorry to hear about your cat. Whatever you are doing appears as if there is an army of developers and plugin supporters. Impression is everything smile A great plugin, truly.
In reply to Oleg Melnikov

Re: dynamic array declaration in Formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Oleg,

In my previous post, the = sign is not required when displaying a simple value. Therefore, [{x[0]}; {x[1]}; {x[2]}] is just as good.

I just made three simple examples on how to create arrays. Hope they will be helpful:

Case 1 ↗. Create an array whose elements are equal to some numbers generated randomly. The number of elements in the array is equal to the number of randomly generated numbers. All the elements in the array are different.

Case 2 ↗. Create an array whose elements are taken from a set of randomly generated numbers. The number of elements in the array is given and it is less than the number of randomly generated numbers. All the elements in the array are different.

Case 3 ↗. Create an array whose elements are taken from a set of randomly generated numbers. The number of elements in the array is random. All the elements in the array are different.

ForumQuiz_20210918_2247.png

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: dynamic array declaration in Formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Oleg,

Here is a modified Case 3 where the number of answer boxes corresponds to the random size of the array.



You can try this question here: https://moodleformulas.org/course/view.php?id=115&section=11 ↗

Average of ratings: Useful (2)
In reply to Dominique Bauer

Re: dynamic array declaration in Formulas

by Oleg Melnikov -
Dominique Bauer, fantastic! It works great; and I'll put it to good use! Many thanks!
In reply to Dominique Bauer

Re: dynamic array declaration in Formulas

by Wendy Harper -
Dominique, How does the answer section only show a limited number of boxes. I am tracing through the question, but not understanding that concept. I am trying to make a random list of grades, then determine the mean, median, and mode. I think the random lists you wrote can help, but I am not sure how you are limiting the input boxes. (I will also want them to enter the list of numbers in numerical order)
Thank you
In reply to Wendy Harper

Re: dynamic array declaration in Formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Wendy,

First, the maximum number of answer boxes are included in the Part's text, ie 100 in example 3. Then, in the text of the question, the answer boxes in excess of the size of the array are hidden using the following line in the script:

  $(".formulas_number:gt("+n+")").hide();

If I understood you correctly, you want to give the students a list of grades, the number and values of which are random, presented in ascending order, and ask the students to calculate the mean, median and mode. Is not it ?

Note that the mean, median and mode functions are not yet implemented in the Formulas question. Until they are, I could write them so that they can be included in the Global variables.

In reply to Dominique Bauer

Re: dynamic array declaration in Formulas

by Wendy Harper -

I attached the formulas question.

I do not know how to write javascript (but, the site needs each new script I use to be whitelisted- so I try to stay away from using script)

I have mean, median and range.   The challenge is mode since sort deletes all duplicates.

I had to write seperate questions to test mode.

Wendy

In reply to Dominique Bauer

Re: dynamic array declaration in Formulas

by Wendy Harper -
If you will share an example with me, I would appreciate it.
I wanted to give the students the grades out of order. (Perhaps, I am making this too difficult for myself.)
I considered limiting the grades to multiple of fives, that way I can then count how many times they occur.
I have also considered making an array that is shuffled that determines the frequency of each grade
numgrade=shuffle([0,1,2,0,1,2,0,1,2,0,1,2,0,1,3]); Prior to being shuffled this would generate 16 grades. 0 of the first grade in the grade array, 1 of the second grade, two of the third grade, none of the fourth grade.....

grade=[30,35,40,45, 50,55,60,65,70,75,80,85,90,95,100];
my issue is that I can not display this out of order.
Thank you!
In reply to Wendy Harper

Re: dynamic array declaration in Formulas

by Wendy Harper -
I see the script now. I am not sure how I missed it.
I was able to get the mean, median, and range programmed.
Since the sort feature combines all like terms, I am struggling with having duplicate terms. If you have any suggestions, I would appreciate it.
In reply to Wendy Harper

Re: dynamic array declaration in Formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

As mentioned in the documentation, the sort() function ↗ may not behave as it should, at least it does not behave like comparable functions in PHP and JavaScript. It may need to be modified.

In the meantime, it may be possible to write the function in the Global variables field. I will look into this tomorrow.

In reply to Dominique Bauer

Re: dynamic array declaration in Formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
In your example, you have 15 grades. The problem with a varying number of grades is more difficult, but could be addressed  later. First, I would try with a fixed number of grades, say 15 like in your example.
In reply to Dominique Bauer

Re: dynamic array declaration in Formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Wendy,

Here is an example. It does not include any script. The data set includes 15 elements. This number is fixed. There is only one mode with random recurrences of 2 or 3. The number of decimals is set to 2 for numbers less than 10 and to 1 for numbers greater than 10, in other words 3 significant digits are requested which corresponds to an accuracy of at least ±1%. If you set the question behaviour to Adaptive, students will be able to answer parts one at a time and check their answers as they progress through the question.

You can try the example here ↗.

screenshot-moodleformulas.org-2021.10.03-04_05_02.png

In reply to Dominique Bauer

Re: dynamic array declaration in Formulas

by Wendy Harper -

Clever!

So instead of adding elements to the end of the list, you are overwriting the one or two elements after the number that is the mode.   This way you also know what the mode is.  This eliminates the need to test all the data.

I appreciate your time to make the example.   It teaches me a lot of the built in functions.   

I have struggled with the limitations of not having while loops, or nested if statements.    With this example it shows how to think through the algorithm in a different way.

I will  work on adding a second number that repeats, so the students don't just pick the one number that repeats.

Thank you!

Wendy 

In reply to Wendy Harper

Re: dynamic array declaration in Formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Wendy,

I modified the example as follows:

  • There is a part for each grade in the ordered list. This way, students can check their answers as they enter them.
  • The data set has two modes, one with three recurrences, the other with two. These numbers of two modes, and three and two recurrences are fixed. They can be varied with a JavaScript script or, perhaps more simply, by using variations of the question chosen randomly ↗ which is easy to do.
  • I wrote and incorporated in the question a proper sort() function, because the question becomes trivial to elaborate with such a function. Writing the sort() function in the Global Variables was fun, but above all it demonstrated the possibilities offered by the existing functions of the Formulas question even if they are few in number. The next step is of course to improve the sort() function in the PHP code of the Formulas question so that it is easy to use.

You can try the question here ↗.

screenshot-moodleformulas.org-2021.10.03-23_56_30.png