A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/One-Dimensional Arrays







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

You can pick out individual items by using their index Would output:

Gertrude Barry

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

Declare an array listing 5 animals in a zoo (aardvark, bear, cuckoo, deer, elephant) in alphabetical order:

Write code to output the first and last animal

Someone has accidentally eaten the cuckoo, let the user add a new third animal and print them all out:

Insert new third animal: Crocodile 1: Aardvark 2: Bear 3: Crocodile 4: Deer 5: Elephant

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

Barry Aubrey Gertrude

To overwrite something, you treat it like a variable: Would output:

Barry Peter Gertrude

What is the output of the following code:

23,19,17,13,11,7,5,3,2

Declare an array that will hold the names of your 5 best friends, call is

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:

Insert best friends:

1: Nell

2: Al

3: Sean

4: Paley

5: Jon

You listed: Nell,Al,Sean,Paley,Jon

Adjust the code above so that it outputs the list in reverse order:

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  code then how will you cycle through everything? Visual Basic and most languages offer a  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: The above code would 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.
 * $$F_n = F_{n-1} + F_{n-2},\!\,$$



For example:


 * $$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:

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

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:

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

Who are you searching for:

Olamide

Olamide found at position : 2

Why do we have attendance.length - 1 in the above code?

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:



