# Arrays

Arrays are "lists" of related values. Every value in the array is usually of the exact same type and only differentiated by the position in the array. For example, all the quiz scores for a test could be stored in an array with the single variable name: quiz_scores. The first students quiz score would be found in the first "bucket" of the array, the second students score in the "second" bucket, etc.

# Arrays

For general information on data types and arrays see the topic on Data Types.

Arrays are used to store lists of related information. Your shopping list (Type = string); for the names of the students in a class (type = string) for the grades for the first exam (type = numbers).

Note: Array variables should usually be named with "plural" words. For example: grades, students, words.

Arrays have a couple of interesting properties/terminologies that are used with them:

1. ### Index of an array

Arrays have a single name, but many values. How do we (the programmer) define which particular value we are interested in? The answer is, we provide a numerical "index".

The index into an array is a number representing which bucket is currently of interest. Consider an array with the following 5 grades : [ 100, 95, 99, 87, 90 ];

The 3rd value in the array is 99.

The idea of a related list of information is so prevalent, that all languages support arrays in one form or another. Thus, the "semantics" of arrays is always the same. The "syntax" and "usage" of arrays can vary. Below we discus the differences between C, Matlab, and Actionscript

## Matlab

1. Arrays start at index: ONE
2. Arrays can be initialized using [] brackets: grades = [99, 100, 50];
3. Arrays are indexed using the syntax of: parentheses, grades(1) = 99;
```                ```
%
%  Create an array, print the 3rd value in the array, update that value, and print the new value.
%
grades = [ 100, 90, 50 ];

fprintf('the 3rd grade in the array is %f\n', grades(3) );

fprintf('the updated 3rd grade in the array is now %f\n', grades(3) );
```
```

## The C Language

1. Arrays start at index: ZERO
2. The size of the array must be declared when the array is: int grades[3]
3. Arrays can be initialized (at creation) using curly brackets {}: int grades[3] = {99, 100, 50};
4. Arrays are indexed using the syntax of: square brackets, grades[0] = 99;
```                ```
//
//  Create an array, print the 3rd value in the array, update that value, and print the new value.
//
int grades[3] = { 100, 90, 50 };

printf("the 3rd grade in the array is %f\n", grades[2] );

grades[2] = 70; // NOTICE 2 means the third value in the array!!!! ZERO BASED!!!

printf("the updated 3rd grade in the array is now %f\n", grades[2] );
```
```

## Actionscript

1. Arrays start at index: ZERO
2. The size of the array can be declared when the array is, but can be added to later: var grades : Array = new Array(3)
3. Arrays can be initialized (at creation) using square brackets []: var grades = new array([99, 100, 50]);
4. Arrays are indexed using the syntax of: square brackets, grades[0] = 99;
```                ```
/*
*  Create an array, print the 3rd value in the array, update that value, and print the new value.
*/
var grades = new Array( [ 100, 90, 50 ] );

trace("the 3rd grade in the array is " + grades[2] );

grades[2] = 70; // NOTICE 2 means the third value in the array!!!! ZERO BASED!!!

trace("the updated 3rd grade in the array is now " + grades[2] );
```
```

Remember: in C, Java and Actionscript, arrays are indexed from 0, whereas Matlab is indexed from 1.

Key Idea: the value of bucket in an array and the index of the bucket are two separate things. The value is stored at the location of the index.

2. ### Arrays and Loops

Because arrays are "indexed" by numbers, we can "iterate" over an entire array using code such are:

```            ```
grades = ...;  % create an array of grades

for index assigned from 1 to the length of the grades array
print the grade at "index"  is grades(index)
end
```
```

We use the symbolic variable "index" to reference the current value in the array that we are concerned with.

3. ### Size or Length

The size of an array (also known as the length) is the number of buckets in the array that have data in them. Most of the time we will use the symbolic "length" of the array when coding. We seldom will "hard code" an actual number, such as 7, for the length of an array..

Example:

## Matlab

```                ```
%
% Sum up the values in a variabled named array_of_grades
%
total = 0;

for i = 1 : length(array_of_grades)  % the length function tells us the number of values in the array!!!
total = total + array_of_grades(i);
end
```
```

## C Code

```                ```
//
// Sum up the values in a variabled named array_of_grades
//
float total = 0;
int   length_of_array = 100;  // There is NO length function, so we have to "hard code" length

for (int i=0; i <length_of_array; i++)
{
total = total + array_of_grades[i];
}
```
```

## ActionScript

```                ```
//
// Sum up the values in a variabled named array_of_grades
//
var  total  : float  = 0;
var  grades : Array  = new Array(100); // 100 possible values in array

for (var i:int=0; i <grades.length; i++)
{
total = total + grades[i];
}
```
```

Again, to find out the "Size" of an array, you can use the following syntax:

## Matlab

```                ```
length(name);       % provides the number of characters in the string
length(grade_list); % provides the number of numbers in the array
```
```

## C Code

```                ```
// YOU CANNOT. You must create a variable and  must keep track of the
// size yourself
```
```

## Java

```                ```
grade_list.length();  // get length of list of grades
```
```

## ActionScript

```                ```