Write a sketch that takes an array full of random numbers and prints out the numbers that are less than 10.
The starting array needs to contain 30 random numbers between 1 and 20. Write a function to generate and return that array. Display that array to the screen with the title “Original Array”. Then take the original array and pass it to a different function that loops through the array, looking for any numbers less than 10 and storing those numbers in a new array that will be returned by the function. Display the filtered array to the screen with the title “Filtered Array”. You have functions from previous activities for displaying arrays on screen with titles.
You can use the
int(...) function around random so you get integers rather than numbers with long fractional parts. For example:
var x = int(random(1,20)); (Try it with and without
int to see the difference.
Use descriptive names for the functions you write.
The function to generate the initial array needs to take a parameter where you specify how big an array to create. You will pass in 20 but if we later want to create a bigger or smaller array, that should be simply passing a different number into the function. Like:
Likewise the function to filter the array needs to take a parameter to specify what numbers it is looking for. Pass in a value that will be the limit below which the function will return values. For this example, you would pass in 10. Like:
There should be no need for global variables.
And it goes without saying that you should follow the coding style guidelines.
Call your sketch ArrayFilter.
Go back to Arrays 2