Unit 5: Day 5
Tasks
56.1 Intro to Common List Algorithms Including Searching (Arrays Example 6 - Weather )
- This is a VERY important example as it not only introduces the concept of SEARCHING, it does so using a function that takes in PARAMETERS and RETURNS a value back.\
- I recommend using real data found on a weather site but providing that data too them so you can focus on the new concepts.
Discuss or provide notes on searching:
- All search algorithms do the following:
- Loop through a list looking for a search term match.
- They then save the index value of the found term so it can be used to look up a value in a parallel list
- It is common practice to return -1 (or any negative number) when the search term is NOT in the list.
- There are 2 search algorithms AP CSP students must be familiar with and should be able to trace on the AP Exam:
- Linear Search (Used in this example) aka Sequential Search - Can be run on any list but is not efficient as EVERY item in the list is checked.
- Binary Search (Used and explained in a later example)- Can only be run on SORTED lists.
Discuss or provide notes on advanced functions:
- Most of our functions so far (keypressed, draw, scene functions) work independently.
- However, functions can add two more capabilities:
- Take in extra data necessary to do their job. This extra data is known as a Parameter or an Argument.
- Return a result back to where the function was called. This data send back is known as a return value.
- Today’s SEARCH function does both:
- It takes in the name of the list and the term to search for as parameters
- It returns the index value of the found item or returns -1 if not found.
Through the Weather Example, demonstrate some new algorithms:
- Filtered Print by season
- Sum If - Total the precipitation for one season
- Count IF - Count the number of months with more than than 10mm
- Find the lowest precipitation amount (save highest for the assignment)
- Introduce searching method
56.2 Functions Review Using AP Classroom
- If you have access to AP Classroom, take a few moments to go over the Topic Questions for 3.12 Calling Procedures and 3.1 Developing Procedures. There were 30+ sample multiple choice questions available at the time this document was produced.
Weather Example
let allmonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
let allprecip = [4.8, 17.9, 12.9, 2.8, 92.9, 105.5, 121.2, 83, 5, 11.2, 22.5, 2.5];
let allseasons = ["Winter", "Winter", "Spring", "Spring", "Spring", "Summer", "Summer", "Summer", "Fall", "Fall", "Fall", "Winter"];
function setup() {
let sketch = createCanvas(800, 200);
sketch.parent("myP5canvas");
//Filtered print by season
for (let i = 0; i < allmonths.length; i++) {
if (allseasons[i] === "Spring") {
print(allmonths[i] + " " + allprecip[i] + "mm " + allseasons[i]);
}
}
//SumIf by season
let total = 0;
for (let i = 0; i < allmonths.length; i++) {
if (allseasons[i] === "Summer") {
total += allprecip[i];
}
}
print("Total precipipitation in the summer: " + total + "mm.");
//CountIF - Count the months with more than 10mm
let counter = 0;
for (let i = 0; i < allmonths.length; i++) {
if (allprecip[i] > 10) {
counter++;
}
}
print("The number of high precipipitation months is:" + counter);
//Lowest - Find the month with the lowest precipipitation
let lowestindex = 0;
let lowest = allprecip[0];
for (let i = 0; i < allmonths.length; i++) {
if (allprecip[i] < lowest) {
lowestindex = i;
lowest = allprecip[i];
}
}
print("The lowest month is " + allmonths[lowestindex] + " with " + lowest + "mm.")
}//end setup
function keyPressed() {
if (key === 's') {
let toFind = window.prompt("What month do you want?");
let foundindex = search(allmonths, toFind);
if (foundindex === -1) {
print("Not found");
} else {
print(allprecip[foundindex] + "mm in " + toFind)
}
}
}
function search(array, searchterm) {
for (let i = 0; i < array.length; i++) {
if (array[i] === searchterm) {
return i;
}
}
return -1;
} //end search
function draw() {
background(200, 50, 50);
text("Press the letter s to search by month.", 50, 100);
}//end draw
Continue to Unit 5: Day 6 »