Array

From FizzFuzz

Jump to: navigation, search

An array is a linear data structure that stores a finite amount of elements via indexing. FizzFuzz arrays are dynamic-length arrays, and are defined in a C/C++-style manner. Note that FizzFuzz arrays are not associative arrays. For this functionality, please see the map data structure, included in the structures library of the Standard Suite.

Contents

Defining An Array

As with C and C++, arrays are defined as a certain data type, though, unlike C and C++, this doesn't have to be a primitive data type. Note that FizzFuzz arrays begin indexing at 1, not 0 like C and C++.

integer int_array[5]
char    char_array[5]

As FizzFuzz arrays are dynamic-size arrays, the size is not nescesary, and doesn't need to be included. Simply placing the brackets after the definition will indicate it's an array.

integer int_array[]
char    char_array[]

Initializing An Array

The syntax for initializing the value(s) of an array are also inherited from C++. The individual values are placed in braces, and delimited by a comma.

integer int_array[5]  = {1, 2, 3, 4, 5}
char    char_array[5] = {'A', 'B', 'C', 'D', 'E'}

Note that if an array is initialized, the size of the array need not be included.

//Both are automatically assigned a size of 5.
integer int_array  = {1, 2, 3, 4, 5}
char    char_array = {'A', 'B', 'C', 'D', 'E'}

Reassigning An Array

The syntax for reassigning the values of an array is syntactically the same as initializing an array, but this can be done outside of initialization. In addition to reassigning the values of the array, any change in size will be taken into account, and set equal to the total number of elements in the assignment.

#include <std.dflt.*>
using variable environment
 
integer int_array = {1, 2, 3, 4, 5}
 
void main()
    int_array = {6, 7, 8, 9, 10, 11}
    outputln(int_array[]) //This outputs "6". See below for notes on this
                          //syntax.

Accessing An Index

To access an index in an array, follow the name of the array with brackets, with a number in those brackets. Using operators with this syntax will cause the indicated operator to be performed with that particular index.

#include <std.dflt.*>
using variable environment
 
integer int_array[5] = {1, 2, 3, 4, 5}
 
void main()
    outputln(int_array[3]) //Outputs the third element, which is 3.
 
    //This accesses the 4th element, then subtracts seven from it,
    //meaning it now stores a value of -3.
    int_array[4] -= 7

Altering Array Sizes

In FizzFuzz, placing brackets, sans anything inside them (whitespace exlcluded) indicates an attempt to alter the size of the array. Doing this without any modification will simply output the array size. The <-, ->, and = operators can be used to alter the size of the array. Two other operators, >> and <<, also exist to perform operations on the elements in the array, when brackets are present.

#define <std.dflt.*>
using variable environment
 
integer int_array[5]  = {1, 2, 3, 4, 5}
 
void main()
    outputln(int_array[]) //As the size of the array is five, this will output "5".
 
    //This expands the size of the array, making it 10 elements, now.
    //It also fills those with null values.
    int_array[] -> 5
 
    for(integer a = 1; a <= int_array[]; a ++)
        /*
        This loop will output the following:
        Element 1: 1
        Element 2: 2
        Element 3: 3
        Element 4: 4
        Element 5: 5
        Element 6:
        Element 7:
        Element 8:
        Element 9:
        Element 10:
        */
 
        outputln("Element [a]: [int_array[a]]")
 
    //This contracts the size of the array, leaving it now with 3
    //elements. When an array is contracted, certain elements will
    //be deleted, starting with the last element first.
    int_array[] <- 7
 
    for(integer a = 1; a <= int_array[]; a ++)
        /*
        This loop will output the following:
        Element 1: 1
        Element 2: 2
        Element 3: 3
        */
 
        outputln("Element [a]: [int_array[a]]")
 
    /*
    This sets the size of the array to 1. Depending on the current
    size of the array, directly assigning the size will either add
    elements (if the array is smaller than the size being assigned),
    remove elements (if the array is larger than the size being
    assigned), or do nothing at all (if the array is equal to the
    size being assigned).
    */
    int_array[] = 1
 
    for(integer a = 1; a <= int_array[]; a ++)
        /*
        This loop will output the following:
        Element 1: 1
        */
 
        outputln("Element [a]: [int_array[a]]")

Operations On Arrays

In FizzFuzz, operations can be performed on the elements of an entire array. This is done by using the array as would be done using any other variable in an expression. Note that the >> and << operators have a special action when including brackets as one normally would for referencing the size of an array.

The Rotation Operators

The >> and << operators are used to rotate the elements of an array, either from left-to-right (>>) or right-to-left (<<).

integer int_array[5] = {1, 2, 3, 4, 5}
 
void main()
    //Shifts the elements of the array to the left,
    //three times.
    int_array[] << 3
 
    for(integer a = 1; a <= int_array[]; a ++)
        /*
        This loop will output the following:
        Element 1: 3
        Element 2: 4
        Element 3: 5
        Element 4: 1
        Element 5: 2
        */
 
        outputln("Element [a]: [int_array[a]]")
 
    //Shifts the elements of the array to the left,
    //three times.
    int_array[] << 4
 
    for(integer a = 1; a <= int_array[]; a ++)
        /*
        This loop will output the following:
        Element 1: 2
        Element 2: 3
        Element 3: 4
        Element 4: 5
        Element 5: 1
        */
 
        outputln("Element [a]: [int_array[a]]")

Comparison Operators

Operators like == and != will compare the contents of the array.

integer
    a[5] = {1, 2, 3, 4, 5}
    b[5] = {1, 2, 3, 4, 5}
    c[5] = {6, 7, 8, 9, 10}
 
environment.outputln(a == b) //Outputs "true".
environment.outputln(a != b) //Outputs "false".
environment.outputln(a == c) //Outputs "false".
environment.outputln(a != c) //Outputs "true".

Other Operations

To perform operations on all the elements in an array, treat the array as a normal variable and perform operations normally. Note that using the assignment operator (=) will change the value of the array, not each element in the array.

integer int_array[5] = {1, 2, 3, 4, 5}
 
void main()
    //This would add three to each element in the array, making the values
    //4, 5, 6, 7, and 8, respectively.
    int_array += 3
 
    //This would left bitshift each element in the array by one, making the
    //values 8, 10, 12, 14, and 16.
    int_array <<= 1

The .. And , Operators

The .. and , operators can be used to select multiple values to be altered, without selecting the whole array. The .. operator can be used to inclusively select a range of values, while the , operator is used to select mutliple values. These can be used in conjunction, to allow the selection of multiple ranges of values.

#include <std.dflt.*>
using variable environment
 
integer int_array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
 
void main()
     //This selects the elements 1, 2, 3, and 4, and then
     //multiples them by two.
     int_array[1..4] *= 2
 
     //This selects elements 1, 3, 5, 7, and 9, and then
     //multiples them by three.
     int_array[1, 3, 5, 7, 9] *= 3
 
     //This selectsion 1, 2, 3, 8, 9, and 10, and adds five
     //to their value.
     int_array[1..3, 8..10] += 5
 
     for(integer a = 1; a <= int_array[]; a ++)
         /*
         This outputs the following:
         Element 1: 11
         Element 2: 9
         Element 3: 23
         Element 4: 8
         Element 5: 15
         Element 6: 6
         Element 7: 21
         Element 8: 13
         Element 9: 32
         Element 10: 15
         */
 
         outputln("Element [a]: [int_array[a]]")

Multidimensional Arrays

Multidimensional arrays in FizzFuzz are declared by including extra sets of brackets after the first. Each set of brackets indicates the total dimensions (for example, two sets of brackets indicates a two-dimensional array), and the value in the brackets indicates the depth of each particular dimension. Use nested brackets to assign values in these arrays.

//This creates a two-dimensional arrays with 5 arrays of 2 elements each.
integer int_array[5][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}
Personal tools
Navigation
functions
language