Try   HackMD

Working with physical arrays

Objectives

  • Work on computational thinking
  • Describe the concepts of the array
  • Experiment for yourselves how a computer thinks

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Setup

Each student (or pair) needs to have one of the Strings selectors, one counter, the 6 numbers from 0 to 5.
We’re going to be the computer that is going to compute with this information and we’re going to read, execute and write instructions.

Activity 1

Find the latin alphabet and the extended alphabet and create one word of 6 letters. Can be your name or any idea related to you.

When you’re done we’re going to write the word that we have written. Since we have a String, we know that String (the whole word) is equal to that.
Write down

String = {the word that you have chosen}

If we write Output (String) we ‘re going to write in a special part what we can see on the string.

Arrays are linear structures that store data. In this case we have an array of letters that are in a specific order to create a word. This is called “String” (Cadena de caracteres in Spanish, but also known as string)

Looking it with care you will tell that the numbers bellow the letters start with 0 instead of 1 and that they are inside brackets. That’s because in computing ordinal numbers (first, second and so one) start with 0 instead of 1. They are called zero-based arrays because of that. You can see that also in physics or in math when you have an initial time t sub 0.
To access this data we use the name of the array (String in this case) followed by a couple of brackets.

Activity 2

Write down the following:

String [0] = {the character in the first place on top of the [0] }
String[1] = {the character in the second place on top of the [1] }
(arrive until String[5])

Activity 3

Now we’re going to mess it up a bit. We’re going to change one of the letters
If we write
String [1] = ‘A’ we’re going to change the 2 letter (on top of the [1]) into an A

Now try to follow, execute, these instructions (in order!) using whatever state you had in your String

String[0] = M
String[2] = G
String[1] = A
String[3] = U
String[5] = L
String[1] = I
String[4] = E
Output String

Write down the instructions to create another word of 6 letters of your choice

Activity 4

The next step is to use another variable to change the number. That will be the Counter dialer. We can give it values from 0 to 5 (it’s a bit limited). For example if we write
Counter= 1 we are putting the 1 in the counter.
If then we write
String[Counter] = T we are going to put a T in the second place because Counter equals to 1 right now

If then we write
String [Counter -1] = A
then we’re going to substitute the letter that it’s in [0] because that’s the value of Counter – 1
If we change then counter writing
Counter = 4
and then we repeat
String [Counter -1] = A
we’re going to substitute the 4th letter because Counter -1 is equal to 3 and that's the 4th position

Now try to follow, execute, these instructions (in order!) using whatever state you had in your String

Counter = 0
String[Counter] = S
String[Counter+1] = T
Counter = 4
String[Counter-2] = E
String[Counter] = E
Counter = 3
String[Counter] = V
String[Counter+2] =N
Output(String)
Output(Counter)

Activity 5

Flow control. Here we're going to use an "if" condition. If somehting is true, then we're going to do something. If not, we're not.

Let's supose that we have the word 'FATIMA' inside the String and we want to check if it has an M as the first letter. To do that we have to write.

if (String[0] = M) then
    output "The string starts with an M"
end if 

Note that comparing inside an if doesn't modify the value of the String itself. FATIMA hasn't change and stil doesn't start with an M

if we use programming languages we have to write in different ways this kind of if.

For example in Java (For DP Option D) this would be like this:

   if (String[0] == 'M'){
    System.out.println("The string starts with an M");
   }

in Arduino it would be something similar to this:

   if (String[0] == 'M'){
    Serial.println("The string starts with an M");
   }

If we want to do something else we use else. Using the example of seeing if FATIMA starts with an M:

if (String[0] = M) then
    output "The string starts with an M"
else then 
    output "The sting doesn't start with an M"
end if 

We can concatenate also several if clauses using else if for example

if (String[0] = M) then
    output "The string starts with an M"
else if (String[0] = N) then
    output "The string starts with an N"
else then 
    output "The sting doesn't start with an M nor an N"
end if 

Set the String to be the word that you used in Activity 1. Now execute this algorithm:

if(String[1] = E) then 
    output "Potato"
else if (String[1]= A) then
    output "Coliflower"
else then
    output "blackberry"
end if

Now change the algorithm so you output the three posibilities (you can change the index, the number or the letter)

Activity 6

Now we're going to check if a specific letter is in our String. To do that first we have t repeat a lot of code (you don't have to write it)

Let's check the word 'AMELIE' and check out if it has any L characters

If we write it all it would be something like this. Execute this code with your String.

if (String [0] = L) then
    output "The string has an L"
end if
if (String [1] = L) then
    output "The string has an L"
end if
if (String [2] = L) then
    output "The string has an L"
end if
if (String [3] = L) then
    output "The string has an L"
end if
if (String [4] = L) then
    output "The string has an L"
end if
if (String [5] = L) then
    output "The string has an L"
end if

It's a nightmare to write this. Imagine if we want to find the letter Q in the Quixote, we would have to write more lines than the Quixote itself. Not a good plan.

Also if we have a word with several L (like LLUVIA) we would have several messages telling us that we have an L in the String. Also we don't know when did that happened.

Activity 7

To face that problem we are going to use loops. But, now we're going to go back with the counter for a bit.

In this case we're going to use Counter = Counter + 1 to add 1 to the value of Counter

Execute this algorithm:

Counter = 0 
String[Counter] = H
Counter = Counter +1
String[Counter] = E
Counter = Counter +1
String[Counter] = L
Counter = Counter +1
String[Counter] = E
Counter = Counter +1
String[Counter] = N
Counter = Counter +1
String[Counter] = A
Output(String)
Output(Counter)

(this one is very obvious what it's the name)

Activity 8

Now let's write another version of the "Let's see if there is and L over here". Execute this code

Counter = 0 
if (String [Counter] = L) then
    output "The string has an L"
end if
Counter = Counter +1
if (String [Counter] = L) then
    output "The string has an L"
end if
Counter = Counter +1
if (String [Counter] = L) then
    output "The string has an L"
end if
Counter = Counter +1
if (String [Counter] = L) then
    output "The string has an L"
end if
Counter = Counter +1
if (String [Counter] = L) then
    output "The string has an L"
end if
Counter = Counter +1
if (String [Counter] = L) then
    output "The string has an L"
end if
Output counter

This code is still repetitive but as you can see when we're repeating we are repeating the exact same lines. In the previous version we had somthing that changed from line to line. We can condense this into a loop that is a bit of code that it's going to repeat while a condition occurs.

For example for this the while loop would be something like this

counter = 0
loop while Counter < 6
    if (String [Counter] = L) then
        output "The string has an L"
    end if
    Counter = Counter +1
end loop
Output counter

Write an algorithm to find the letter A in the first 3 letters

Solution for Activity 8


loop Counter from 0 to 2
    if (String [Counter] = A) then
        output "The string has an L"
    end if
end loop


loop Counter from 0 to 2
    if (String [Counter] = A) then
        output "The string has an L"
        break
    end if
end loop

counter = 0
Found = false
loop while Counter < 6 AND NOT Found
    if (String [Counter] = L) then
        output "The string has an L"
        Found = true
    end if
    Counter = Counter +1
end loop
Output counter
  for (counter = 0; counter < 6; counter ++) {
      if (String[counter] == 'A') {
          System.out.println("Found the A");
          break;
      }
  }