Lesson 7.2

Arrays and Loops

Watch: Arrays and Loops - p5.js Tutorial

Video for 7.2:

In this lesson we will learn how to process arrays - to write functions that consume lists and to write others that produce them. In all cases we wil iterate through the items in a list using for loops.


Functions that consume lists.

Exercise 7.2a: Write a function addNumbers() which returns the sum of the numbers in a list. Include a test function.

How do we write the function?

We will start with a template for most functions that will process lists of values.

The dots, …… , are where we will write the block of code, that is, what we wish to do with each value at that index, one at a time. In this case we wish to add the value to the sum of the previous values. We can write this as follows:

When the list contains nothing, the initial value of sum should be 0. So we can define the variable above the for loop as follows.

To get at the value, we will have the function return it.

Here are tests you can run in the console. You can add some more on your own.

It look like it works, so we can include a test function and we are done.

We can now run the test function.

Exercise 7.2b: Write a function average() which returns the average of the numbers in a list. Include a test function.

To find the average is now a simple matter of dividing the sum by the number of items in the list. I have included the test function as well.

We can now run this test.

Exercise 7.2c: Write a function howMany() which takes an array and a value as input and returns how many times that value occurs in the array.For example, howMany() (alist,45) should return 1 for the array [1,2,5,1,5,6,7,45]. Include a test function.

Here is the function.

Note how similar it is to addNumbers(), The only difference is that we do not add the value of the list to sum but the number 1 for every time that we find the value in the list with a conditional. Here it is with the test function.

And the test…

Exercise 7.2d: Write a function containsValue() which takes a list and a value, returns true if the value occurs on the list and false if it does not.
For example, containsValue([1,2,3,5],4) should return false, and containsValue([1,2,3,5],3) would return true. Include a test function.

Here is the function with a test function.

Note that this has two return statements. The first instance that we find returns true, unless none are found by the time we reach the end of the list, and it returns false.

Exercise 7.2e: Write a function itemNum() which takes a list and value as input and returns the first location where the value is found, or "not found" if it does not occur on the list.
For example, itemNum([1,2,3,4,5], 3) should return 2, and itemNum([1,2,3,4,5], 3) should return not found. Include a test function.


Functions that produce lists.

Exercise 7.2f: Write a function squareList () that takes a single number as input and produces a list of prefect squares up to that number.
For example, squareList(10) should return [0, 1, 4, 9, 16, 25, 36, 49, 64, 81].
squareList(50) should return [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]. Include a test function.

Exercise 7.2g: Write a function multiples() that takes two numbers as input. The first specifies how many multiples are to occur in a list, the second the number we want multiples of.
For example, multiples(50,13) should return [0, 13, 26, 39, 52, 65, 78, 91, 104, 117, 130, 143, 156, 169, 182, 195, 208, 221, 234, 247, 260, 273, 286, 299, 312, 325, 338, 351, 364, 377, 390, 403, 416, 429, 442, 455, 468, 481, 494, 507, 520, 533, 546, 559, 572, 585, 598, 611, 624, 637]. Include a test function.

Exercise 7.2h: Write a function squaresFromList() that takes a list and finds the square of the numbers in the list.
For example, if var alist = [1,3,5,12]; squaresFromList(alist) should return [1, 9, 25, 144]. Include a test function.

Exercise 7.2i: Write a function CtoFList() that takes a list of numbers as input , temperatures in Celsius, and returns a list of numbers – those values converted to Fahrenheit. Write a helper function.
For example, if var list1 = [-40,0,100]; CtoFList(list1) should return [-40, 32, 212]. Include a test function.

Exercise 7.2j: Write a function replaceWith50() which takes a list of grades and replaces all of the grades below 50 with 50.
For example, if var list1 = [80,32,65,15,24,67,95,52]; replaceWith50(list1) should return [80, 50, 65, 50, 50, 67, 95, 52]. Include a test function.

Exercise 7.2k: Write a function reverseIt() that takes a list of numbers as input and reverses their order in the list.
For example, if var list1 = [80,32,65,15,24,67,95,52]; reverseIt(list1) should return [52, 95, 67, 24, 15, 65, 32, 80]. Include a test function.