A List of Train Cars - Some folks call it a train

Arrays

Now that we know about iteration what can we use it for?  Well we can print hello a bunch of times, but that becomes old fairly quickly.  One thing we might want to do is to step through a list of things.  The picture above shows a list of things.  Let's call this list tc.  For train cars (obviously).  We will create a list of train car types.  It could be a list of contents each car or ...


There is nothing magic about a list or often called an array in computer science. it is just a collection of a bunch of things that are defined and arranged so that we can easily access them with computer code and iteration.

A pseudo code example:

would step through all the train cars and display the type.  (pseudo code describes the desired logic without using a specific programming language)


C

C has the most primitive array structure of the languages (maybe bash)  we will be looking at.  essentially just a collection of items stored linearly in memory.

#include <stdio.h>
int main () {
    // Define two arrays containing population and name.
    int population[] = {5429524, 3519595, 2264823, 1237656, 1062643, 989657, 711925,
                      705103, 693645, 470015, 383437, 335696, 316701, 308875, 287069};
    char cities[15][20] = {"Toronto", "Montreal", "Vancouver", "Calgary", "Edmonton", "Ottawa–Gatineau",
                          "Winnipeg", "Quebec City", "Hamilton", "Kitchener–Waterloo",
                          "London", "Victoria", "Halifax", "Oshawa", "Windsor"};
    // Display a single city
    printf("%s has a population of %d.\n\n", cities[3], population[3]);

    // Display all the cities
    int i = 0;
    while (i < 15) {
        printf("%s has a population of %d.\n", cities[i], population[i]);
        i++;
    }
    // Erase Toronto
    population[0] = 0;
    printf("\n%s has a population of %d.\n\n", cities[0], population[0]);

    // Display a single character from Montreal.
    putchar(cities[1][3]);
}


  • Running the above gives:
Calgary has a population of 1237656.

Toronto has a population of 5429524.
Montreal has a population of 3519595.
Vancouver has a population of 2264823.
Calgary has a population of 1237656.
Edmonton has a population of 1062643.
Ottawa–Gatineau has a population of 989657.
Winnipeg has a population of 711925.
Quebec City has a population of 705103.
Hamilton has a population of 693645.
Kitchener–WaterlooLondon has a population of 470015.
London has a population of 383437.
Victoria has a population of 335696.
Halifax has a population of 316701.
Oshawa has a population of 308875.
Windsor has a population of 287069.

Toronto has a population of 0.

t

PHP 

PHP stores all "arrays" as maps.
#!/usr/bin/env php

<?php
// Define two arrays containing population and name.
$population = array(5429524, 3519595, 2264823, 1237656, 1062643, 989657, 711925,
                      705103, 693645, 470015, 383437, 335696, 316701, 308875, 287069);
$cities = array("Toronto", "Montreal", "Vancouver", "Calgary", "Edmonton", "Ottawa–Gatineau",
                          "Winnipeg", "Quebec City", "Hamilton", "Kitchener–Waterloo",
                          "London", "Victoria", "Halifax", "Oshawa", "Windsor");
// Display a single city
echo $cities[3], " has a population of ", $population[3], "\n\n";

// Display all the cities
$i = 0;
while ($i < 15) {
    echo $cities[$i], " has a population of ", $population[$i],"\n";
    $i++;
}
// Erase Toronto
$population[0] = 0;
echo "\n", $cities[0], " has a population of ", $population[0],"\n";

// Display a single character from Montreal.
   

echo substr($cities[1], 3, 1), "\n\n";

// Make a hash array (Dictionary)

$hash = array("Toronto" => 5429524,
              "Montreal" => 3519595 ,
              "Vancouver" => 2264823,
              "Calgary" => 1237656,
              "Edmonton" => 1062643,
              "Ottawa–Gatineau" => 989657,
              "Winnipeg" => 711925,
              "Quebec City" => 705103,
              "Hamilton" => 693645,
              "Kitchener–Waterloo" => 470015,
              "London" => 383437,
              "Victoria" => 335696,
              "Halifax" => 316701,
              "Oshawa" => 308875,
              "Windsor" => 287069);

// Display the hash of name value pairs

foreach ($hash as $k => $v) {
    echo  $k, " has a population of ", $v, "\n";
}


  • Running the above gives:

Calgary has a population of 1237656

Toronto has a population of 5429524
Montreal has a population of 3519595
Vancouver has a population of 2264823
Calgary has a population of 1237656
Edmonton has a population of 1062643
Ottawa–Gatineau has a population of 989657
Winnipeg has a population of 711925
Quebec City has a population of 705103
Hamilton has a population of 693645
Kitchener–Waterloo has a population of 470015
London has a population of 383437
Victoria has a population of 335696
Halifax has a population of 316701
Oshawa has a population of 308875
Windsor has a population of 287069

Toronto has a population of 0
t

Toronto has a population of 5429524
Montreal has a population of 3519595
Vancouver has a population of 2264823
Calgary has a population of 1237656
Edmonton has a population of 1062643
Ottawa–Gatineau has a population of 989657
Winnipeg has a population of 711925
Quebec City has a population of 705103
Hamilton has a population of 693645
Kitchener–Waterloo has a population of 470015
London has a population of 383437
Victoria has a population of 335696
Halifax has a population of 316701
Oshawa has a population of 308875
Windsor has a population of 287069


Python

Python stores "arrays" as lists or dictionaries

#!/usr/bin/env python3

population = [5429524, 3519595, 2264823, 1237656, 1062643, 989657, 711925,
              705103, 693645, 470015, 383437, 335696, 316701, 308875, 287069]

cities = ["Toronto", "Montreal", "Vancouver", "Calgary", "Edmonton", "Ottawa-Gatineau",\
          "Winnipeg", "Quebec City", "Hamilton", "Kitchener-Waterloo", "London", "Victoria",\
          "Halifax", "Oshawa", "Windsor"]

print(cities[3], "has a population of ", population[3])
print("-------------- Python will print the whole list --------------")
print(cities)
print(population)

print("---------- As two arrays (actually lists) ---------------")
for i in range(0, 14):
    print(cities[i], "has a population of ", population[i])

# Erase Toronto
print("--------Erase Toronto---------")
population[0] = 0
print("\n", cities[0], " has a population of population of ", population[0], "\n")

# But we really should use a dictionary.
hash = {"Toronto": 5429524,
        "Montreal": 3519595 ,
        "Vancouver": 2264823,
        "Calgary": 1237656,
        "Edmonton": 1062643,
        "Ottawa–Gatineau": 989657,
        "Winnipeg": 711925,
        "Quebec City": 705103,
        "Hamilton": 693645,
        "Kitchener–Waterloo": 470015,
        "London": 383437,
        "Victoria": 335696,
        "Halifax": 316701,
        "Oshawa": 308875,
        "Windsor": 287069}

print("------- As a Dictionary ----------------------------------")
for i in hash:
    print(i, "has a population of ", hash[i])



  • Running the above gives:

Calgary has a population of  1237656
-------------- Python will print the whole list --------------
['Toronto', 'Montreal', 'Vancouver', 'Calgary', 'Edmonton', 'Ottawa-Gatineau', 'Winnipeg', 'Quebec City', 'Hamilton', 'Kitchener-Waterloo', 'London', 'Victoria', 'Halifax', 'Oshawa', 'Windsor']
[5429524, 3519595, 2264823, 1237656, 1062643, 989657, 711925, 705103, 693645, 470015, 383437, 335696, 316701, 308875, 287069]
---------- As two arrays (actually lists) ---------------
Toronto has a population of  5429524
Montreal has a population of  3519595
Vancouver has a population of  2264823
Calgary has a population of  1237656
Edmonton has a population of  1062643
Ottawa-Gatineau has a population of  989657
Winnipeg has a population of  711925
Quebec City has a population of  705103
Hamilton has a population of  693645
Kitchener-Waterloo has a population of  470015
London has a population of  383437
Victoria has a population of  335696
Halifax has a population of  316701
Oshawa has a population of  308875
--------Erase Toronto---------

 Toronto  has a population of population of  0

------- As a Dictionary ----------------------------------
Toronto has a population of  5429524
Montreal has a population of  3519595
Vancouver has a population of  2264823
Calgary has a population of  1237656
Edmonton has a population of  1062643
Ottawa–Gatineau has a population of  989657
Winnipeg has a population of  711925
Quebec City has a population of  705103
Hamilton has a population of  693645
Kitchener–Waterloo has a population of  470015
London has a population of  383437
Victoria has a population of  335696
Halifax has a population of  316701
Oshawa has a population of  308875
Windsor has a population of  287069


Bash


population=(5429524 3519595 2264823 1237656 1062643 989657 711925
            705103 693645 470015 383437 335696 316701 308875 287069)


cities=("Toronto" "Montreal" "Vancouver" "Calgary" "Edmonton" "Ottawa-Gatineau"\
        "Winnipeg" "Quebec City" "Hamilton" "Kitchener-Waterloo" "London"\
        "Victoria" "Halifax" "Oshawa" "Windsor")

echo
echo ${cities[3]} has a population of  ${population[3]}
echo

for i in ${!population[@]}; do
    echo ${cities[$i]} has a population of ${population[$i]}
done

echo
population[0]=0
echo ${cities[0]} has a population of  ${population[0]}


  • Running the above gives:
Calgary has a population of 1237656

Toronto has a population of 5429524
Montreal has a population of 3519595
Vancouver has a population of 2264823
Calgary has a population of 1237656
Edmonton has a population of 1062643
Ottawa-Gatineau has a population of 989657
Winnipeg has a population of 711925
Quebec City has a population of 705103
Hamilton has a population of 693645
Kitchener-Waterloo has a population of 470015
London has a population of 383437
Victoria has a population of 335696
Halifax has a population of 316701
Oshawa has a population of 308875
Windsor has a population of 287069

Toronto has a population of 0



Homework

Choose your favorite program and use a random number generator to create or fill an array of 10 entries (0-9) with random numbers and then display the random numbers

How to get a random number: