From Computer Science Wiki
Revision as of 19:34, 12 October 2017 by Mr. MacKenty (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Programming basics[1]

Arrays are a fundamental data structure, and they are extremely useful. We use arrays to hold values of the same type at contiguous memory locations. In particular, the use of arrays allows us to create "groups" or "clusters" of variables without needing to give a unique variable name to each, but still allowing us to individually index into the elements of the array. If you haven't started counting from zero yet, now is the time, because in C, arrays are zero-indexed which means the first element of a k-element array is located at array index 0 and the last element is located at array index k-1. In case you're wondering, that's the primary reason we count from 0 in CS50 (and in computer science more broadly!)[2]

In an array we are usually concerned about 2 things, the position (or the index) of an element and the element. You should also be aware that different programming languages define arrays differently.

  • In some programming languages, arrays have a pre-defined, fixed, memory size. You can't easily change the size of an array once you create it in the languages below:
    • In the C programming language, an array's size is fixed when it is created (or instantiated).
    • In the java programming language, an array size is fixed when it is created (or instantiated).
  • In other languages, arrays have a dynamic memory allocation, meaning they can grow after they are created
    • In Python, lists (which are analogous to arrays) do not need to have their size defined
    • in PHP an array is dynamic. You do not need to define the size (or the type) in the array

An example of a simple array in PHP can be found below:


/* This file helps 9th and 10th grade (15 and 16 years old) students understand pragmatic and practical aspects of arrays as used in the PHP programming language.  

// this is where we define an array:

$classes=["fighter","cleric","magic user","barbarian","monk"];
echo "<hr>";

/* this is how we "dump" an array to see it (blech, ugly). When we dump an array we see the index, the type and the element. Take a look at the sample of part of var_dump below: 

... [0]=> string(7) "fighter" [1]=> string(6) "cleric"...

We can see at index zero we have a string which is 7 characters, "fighter".  At index one we have a string which is 6 characters, "cleric"


echo "<hr>";

// this is how we access one element in an array. In this case we are selecting the element at index 2

echo $classes[2];
echo "<hr>";

// this is how we count elements in an array. 

echo count($classes);

echo "<hr>";

// this is how we ADD something to an array

$classes[] = "Archer";
echo "<hr>";

// this is how we delete an item from an array. 

echo "<hr>";

// We often loop through an array. This is also called iterating through an array.

foreach ($classes as $i) {

    echo "$i is an item in our array<br>";
    # Looping through an array is a very powerful technique, especially when we combine PHP and HTML.
    # echo "Please type a name for a $i : <input type=\"text\">";
echo "<hr>";

// let's see if there is a specific element in our array:

$searchTerm = "monk";

    echo "there is a monk in the array";

} else {

    echo "there is not a monk in the array";
echo "<hr>";

// let's find the index of something we want to search for:

$indexOfSearchTerm =  array_search($searchTerm,$classes);
echo "You are searching for $searchTerm. It looks like this element has been found at index $indexOfSearchTerm.";
echo "<hr>";

// let's randomly select only one INDEX from our array

$randomlySelectedClass = array_rand($classes,1);
echo "We have randomly selected an index from our array. This will most likely change every time you refresh the page. It is possible the same index will be
chosen again. The random index chosen is: $randomlySelectedClass";
echo "<hr>";

// let's echo the class we just chose from our array of classes:

echo "If we access the element at index $randomlySelectedClass, we find the element $classes[$randomlySelectedClass].
 This will most likely change every time you refresh the page. It is possible the same index will be chosen again.";

A video[edit]

This video references the C programming language and scratch, but the ideas about arrays are excellent.


  • Construct algorithms using pre- defined sub-programmes, one- dimensional arrays and/or collections.