Unit 5: Day 2

START DATE:DUE DATE:STATUS:Open

Tasks

53.1 Arrays Example 1 - Music  Playlist

  • This is purely a review of yesterday’s example.
  • Discuss the use of an Algorithm. Our first algorithm is just the Print algorithm
  • Demonstrate the CHEAT PRINT.  Cheat Print is what I call printing without the use of a FOR loop.  It can be used to quickly print the contents of an array into the console.

53.2 Arrays Example 2 - Parallel Arrays & Splice Function - Competition Judges

  • Discuss the 2 ways of setting up an Array (full or empty).  You MUST use the square brackets for both.
  • Discuss that we will be adding one new piece of code SPLICE and one new algorithm (total & average)
    • W3schools Array Functions page
    • Splice is used to insert and to delete
    • Note that there is actually a DELETE function but it leaves an empty array slot which is problematic

53.3 Important Notes about Javascript Arrays vs the AP CSP Exam Lists

  • Javascript requires the use of the SPLIT function to insert and remove items from the middle of the list.  However, many other languages have easier to use INSERT and/or REMOVE functions to take care of this.  
  • Javascript uses the PUSH function to add to the end of the list.  Others use APPEND or similar functions.
  • See AP CSP Topic 3.10 for a list of the functions that will be used on the exam.

53.4 Theory Video on Algorithms - Part 2

  • Continue the documentary video started last class
  • At 7:17 and 11:00 on the algorithm video, there are two visual expressions of algorithms (Euclid GCD and Pagerank). Have students devise representations of algorithms visually either through flowcharts or using diagrams.

53.5 Messy Data and How to Clean It!

  • Students can work in groups through the worksheet on Messy Data (Year 1 Theory), recalling the videos from Activity 52.4. This can be done in class or given as a homework task.

Arrays Ex1 - Playlist

let allScores = [];

let allBands =[];

function setup() {

let sketch = createCanvas(800, 200);

sketch.parent("myP5canvas");

    print("Judges Scores");

    allBands[0] = "Pink Floyd";

    allBands[1] = "Led Zeppelin";

    allBands[2] = "The Ramones";

    allBands[3] = "Loverboy";

    allBands[4] = "The Band";

    print("the first band: " + allBands[0]);

    print("the last band: " + allBands[5]);

    print("the last band: " + allBands[allBands.length-1]);      

    //special function: PUSH

    allBands.push("Black Sabbath");

    for(let i=0; i<allBands.length; i++){

        print(allBands[i]);

    }

    print("");

    //But this is your stupid older brother's playlist.  Now you get to play a trick on him.  Let's change the list to music he hates

    allBands[0] = "Taylor Swift";

    allBands[1] = "?";    

    for(let i=0; i<allBands.length; i++){

        print(allBands[i]);

    }

}//end setup

function draw() {

    background(200, 50, 50);

}//end draw

Arrays Ex2 - Judges

let allJudges = ["Bob", "Diane", "Jasneet", "Simone"];

let allScores = [];

function setup() {

let sketch = createCanvas(800, 200);

    sketch.parent("myP5canvas");    

    print("Music Competition");

    allScores[0] = 15;

    allScores[1] = 16;

    allScores[2] = 14;

    allScores[3] = 8;

    allScores[4] = 15;

    //Oops we must have forgotten a judge's name

    //We could just add that person to the end, but notice that they are sorted

    //How do you insert "Maleek"?

    //We use a strange function called splice

    allJudges.splice(3,0,"Maleek");

    //splice(where to insert, how many to remove, what to insert)  

    print(allJudges);  //I call this a 'cheat print'    

    for(let i=0; i<allJudges.length; i++){

        print(allJudges[i] +"  - " + allScores[i]);

    }

    print("");

    //Wait!  what is going on?

    allScores[4] = 9;

    //I think some judges are corrupt!

print("Filter for Corrupt Judges");

    for(let i=0; i<allJudges.length; i++){

        if(allScores[i] < 10){

            print("Judge number " + i + " gave a score of " + allScores[i] );

        }

    }

    print("");

    print("Removing 2 judges starting at location 3")

    allJudges.splice(3,2);    

    print("");

    print("Final Scores");

    for(let i=0; i<allJudges.length; i++){

        print(allJudges[i] +"  - " + allScores[i]);

    }

    print("");

    //Total the Scores

    let total = 0;

    for(let i=0; i<allJudges.length; i++){

        total+=allScores[i];

    }

    print("The total score is: " + total);

    print("The average score is: " + (total/allJudges.length));    

}//end setup

function draw() {

    background(200, 50, 50);    

}//end draw


Continue to Unit 5: Day 3 »