# Fundamentals of Programming: One-Dimensional Arrays

 ← Fundamentals of Structured Programming One-Dimensional Arrays Two-Dimensional Arrays →

A diagram showing how a 2d array works, the equivalent of the following:
dim animals(3) as string
animals(0) = "Dog"
animals(2) = "Cat"
```Dim friends(0 To 2) As String

friends(0) = "Barry"
friends(1) = "Aubrey"
friends(2) = "Gertrude"
```

You can also declare arrays by placing the values directly into them, this code does exactly the same as the above:

```Dim friends() As String = {"Barry", "Aubrey", "Gertrude"}
```

You can pick out individual items by using their index

```Console.WriteLine(friends(2))
Console.WriteLine(friends(0))
```

Would output:

Code Output

Gertrude
Barry

You can treat indexed array items as variables and change their values:

```friends(0) = console.readline()
```
 Exercise: One-Dimensional Arrays Declare an array listing 5 animals in a zoo (aardvark, bear, cuckoo, deer, elephant) in alphabetical order: Answer : ```dim zooanimals() as string = {"aardvark","bear","cow","deer","elephant"} ``` Write code to output the first and last animal Answer : ```console.writeline(zooanimals(0)) console.writeline(zooanimals(4)) ``` Someone has accidentally eaten the cuckoo, let the user add a new third animal and print them all out:    Code Output Insert new third animal: Crocodile 1: Aardvark 2: Bear 3: Crocodile 4: Deer 5: Elephant Answer : ```console.write("Insert new third animal:") zooanimals(2) = console.readline() console.writeline("1: " & zooanimals(0)) console.writeline("2: " & zooanimals(1)) console.writeline("3: " & zooanimals(2)) console.writeline("4: " & zooanimals(3)) console.writeline("5: " & zooanimals(4)) ''Alternatively an A-grade student might write: for x = 0 to 4 console.writeline(x + 1 & ": " & zooanimals(x)) next ```

To print out the entire array it is best to use some form of iteration:

```For x As Integer = 0 To 2
Console.WriteLine(friends(x))
Next
```

Would print out:

Code Output

Barry
Aubrey
Gertrude

To overwrite something, you treat it like a variable:

```friends(1)="Peter"
For x As Integer = 0 To 2
Console.WriteLine(friends(x))
Next
```

Would output:

Code Output

Barry
Peter
Gertrude

 Exercise: One-Dimensional Arrays What is the output of the following code: ```dim primes() as integer = {2,3,5,7,11,13,17,19,23} dim count = 8 While count >= 0 console.write(primes(count) & ", ") count = count - 1 end while ``` Answer :    Code Output 23,19,17,13,11,7,5,3,2 Declare an array that will hold the names of your 5 best friends, call is `befr` Answer : ```dim befr(5) as string 'befr(4) would also be accepted ``` Write a loop so that you can input each of your five best friends and it will output them in the order you input them. For example:    Code Output Insert best friends: 1: Nell 2: Al 3: Sean 4: Paley 5: Jon You listed: Nell,Al,Sean,Paley,Jon Answer : ```dim befr(5) as string console.writeline("Insert best friends:") for x = 1 to 5 console.write(x & ": ") befr(x) = Console.ReadLine() next console.writeline("You listed:") for x = 1 to 5 console.write(befr(x) & ", ") next ``` Adjust the code above so that it outputs the list in reverse order: Answer : ```dim befr(5) as string console.writeline("Insert best friends:") for x = 1 to 5 console.write(x & ": ") befr(x) = Console.ReadLine() next console.writeline("You listed:") for x = 5 to 1 step -1 console.write(befr(x)) next ```
 Extension: For each Sometimes you might not know the length of an array that you area dealing with yet you will still want to cycle through all the elements. If you don't know what numbers to put into the `for x = 0 to ??` code then how will you cycle through everything? Visual Basic and most languages offer a `for each` routine that allows you to look at each element until you find the last one. This makes for far more robust code where you don't have to keep changing the variables of loops each time you change the size of arrays: ```Dim someNumbers() as integer = {1,2,3,4,5,6,7,23,77} For Each n In someNumbers Console.Write(n & ", ") Next ``` The above code would output:    Code Output 1, 2, 3, 4, 5, 6, 7, 23, 77,

### Uses

Arrays are very useful for solving all manner of problems, ranging from sorting lists to storing the results to calculations.

Take the Fibonacci sequence of numbers where: the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.

${\displaystyle F_{n}=F_{n-1}+F_{n-2},\!\,}$
A tiling with squares whose sides are successive Fibonacci numbers in length

For example:

${\displaystyle 0,1,1,2,3,5,8,13,21,34,55,89,144,...}$

This could take some time to calculate by hand but and we can use an array to calculate and store this sequence:

```dim fib(30) as integer
'initiate the first two values
fib(0) = 0
fib(1) = 1
for x = 0 to 28
fib(x+2) = fib(x) + fib(x+1)
next
console.writeline("The first 31 fibonacci numbers are:")
for y = 0 to 30
console.write(fib(y) & ",")
next
```
 Exercise: Calculating with arrays Update the above code to allow the user to input a number, then the program to store and display that many fibonacci numbers. Test it for 10 and 1000 Answer : ```dim size as integer size = console.readline() 'integer is too small to hold this value so we change to single dim fib(size) as single 'initiate the first two values fib(0) = 0 fib(1) = 1 for x = 0 to size - 2 fib(x+2) = fib(x) + fib(x+1) next console.writeline("The first " & size & " fibonacci numbers are:") for y = 0 to size console.write(fib(y) & ",") next ```

Arrays are also very important when we are searching and sorting data. You will learn a lot more about this in A2, but for the moment take a look at this linear search routine:

```dim attendance() as string = {"Callum", "John", "Olamide", "Mathew", "Gabriel", "Dong"}
dim search as string
console.writeline("Who are you searching for:")

for x = 0 to attendance.length - 1 'why do we need -1 here?
if attendance(x) = search then
console.writeline(search & " found at position : " & x)
end if
next
```

If we were to try and find Olamide we should see the following:

Code Output

Who are you searching for:
Olamide
Olamide found at position : 2

 Exercise: Searching arrays Why do we have attendance.length - 1 in the above code? Answer : As the array starts at location 0, the length of the array will be 1 more than the largest index number. Adjust the code above to tell you when it hasn't found a person: Answer : ```'there are multiple ways of doing this: dim attendance() as string = {"Callum", "John", "Olamide", "Mathew", "Gabriel", "Dong"} dim search as string dim found as boolean = false console.writeline("Who are you searching for:") search = console.readline() for x = 0 to attendance.length - 1 'why do we need -1 here? if attendance(x) = search then console.writeline(search & " found at position : " & x) found = true end if next if found = false then console.writeline(search & " NOT found in the array") end if ```

 ← Fundamentals of Structured Programming One-Dimensional Arrays Two-Dimensional Arrays →