Arrays: Difference between revisions

From Computer Science Wiki
No edit summary
No edit summary
Line 4: Line 4:


In an array we are usually concerned about 2 things, the '''position''' (or the index) of an element and the element.
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 treat the memory allocation of arrays differently.  
You should also be aware that different programming languages define arrays differently.


* In some programming languages, arrays have a pre-defined, fixed, memory size.  
* 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:
** For example, in the C programming language, arrays are defined by the number of elements in the array
** 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
* 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) are do not need to be allocated
** 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:  
An example of a simple array in PHP can be found below:  

Revision as of 17:08, 27 July 2017

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:

<?php
# this file  is used to teach the VERY BASICS of arrays in PHP

$pizza_toppings = [
"extra cheese",
"onions",
"pepperoni",
"ham",
"pineapple",
"chicken",
"arugula",
"pesto"
];

# This creates a very simple array with 8 items. 

?>

A video[edit]

This video references the C programming language and scratch, but the ideas about conditionals are excellent. In the case of conditionals, PHP and C share similar syntax (but not exact).

Standards[edit]

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


References[edit]