Arrays are data structures that can hold many items of the same type. A space in memory is allocated to hold the values, and the items are positioned sequentially. Static arrays have fixed sizes and do not change at run time, so their sizes must be specified at definition.
Array items are distinguished from each other by a specific number given to each item and called "index". The first item in the array always has an index equal to 0.
When defining an array, the appropriate memory is allocated in memory to store the array items, and no default value is given to these items, so if you print them after the definition directly, random values that are already in the memory before executing the program will be printed.
Arrays in Alusus are defined like this:
def array_name : array[DataType, size];Where:
To access an item in the array we put the name of the array followed by the index of the item enclosed in round brackets, and we should remember that indices of arrays start from zero.
myArray(element_index)
Assigning a value for an item:
myArray(index) = value
To print the value of an item in the array we follow the same printing ways that we talked about earlier, for example to print the value of the item with index 0, and if the items of this array has `Int` type:
Srl.Console.print("The value of the first element is: %d",myArray(0));
Whereas if they have `char` type:
Srl.Console.print("The value of the first element is: %c",myArray(0));
And so on..
Defining an array with 3 items with type `Int` and print it.
import "Srl/Console.alusus"; use Srl; // define an array with 3 items of type `Int`. def myArray: array[Int,3]; myArray(0) = 14; // Assigning value 14 to the first item. myArray(1) = 57; myArray(2) = 90; // a loop through the array items to print them. def index: Int; for index=0, index<3, index++ { Console.print("The value of element %d is: %d\n", index, myArray(index)); } /* The value of element 0 is: 14 The value of element 1 is: 57 The value of element 2 is: 90 */
Calculating the average and the sum of 4 entered integers.
import "Srl/Console.alusus"; use Srl.Console; def myArray: array[Int, 4]; // define an array with 4 items of type `Int` def avg: float[64]; // a float variable to store the average def sum: int[64] = 0; // an int variable to store the sum def i: int[64]; // loop control element // a loop to enter the values for i = 0, i < 4, i++{ print("Enter number %d \n", (i+1)); myArray(i) = getInt(); // enter the value } // calculate the sum for i = 0, i < 4, i++{ sum = sum + myArray(i) } avg = sum / 4.0; // calculate the average print("Sum of entered number is: %d \n", sum); // print the sum print("Average of entered number is: %0.01f \n", avg); // print the average /* Enter number 1 3 Enter number 2 2 Enter number 3 3 Enter number 4 2 Sum of entered number is: 10 Average of entered number is: 2.50 */
Dynamic arrays are similar to static arrays, except they are dynamic, easier to use, and memory friendly. These arrays can grow dynamically as you add items to them. They manage the memory automatically, and they share memory buffers between different instances of an array when possible, eliminating unnecessary memory allocation and copying. Documentation of this class can be found here.
Defining the array is done using the `Array` class, which we first need to include, as follows:
import "Srl/Array.alusus"; // including `Array` class def array_name: Srl.Array[Data_type]; // defining a dynamic array
To initialize the array items with initial values we use the following form:
def array_name : Array[Data_type]({ v1,v2,..,vn });
Note: We can write the previous definition as follows:
def array_name: Array[Data_type](num_of_elements, v1, v2, ..., vn);
The first form is just a syntactic sugar of the second. The compiler automatically converts the arguments between curly brackets into count followed by the arguments themselves.
At first the size of dynamic array is zero, and we can check that (check the number of items in the array) at any time using `getLength` function.
To add an item to the dynamic array we use `add` function.
array_name.add(value); // add an item array_name.add({ value1, value2,.., valueN }); // add many items at once
When declaring a dynamic array, an initial buffer is allocated for it, and when this buffer is full it gets automatically extended. To query the size of the total allocated buffer we use `getBufSize` function.
Example:
In the next example, we will define a dynamic array and we will use the previous functions with it.
import "Srl/Console.alusus"; import "Srl/Array.alusus"; use Srl; use Srl.Console; def a1: Array[Int]; // define a dynamic array print("Number of elements in the array is: %d \n", a1.getLength()); // Number of elements in the array is: 0 print("Initial buffer size is: %d \n", a1.getBufSize()); // Initial buffer size is: 0 def index: int[32]; // add 5 items to the array for index = 0, index < 5, index++ { a1.add(index * 2); // buffer size after each item is added print("Buffer size after adding %d element is: %d \n",index+1, a1.getBufSize()); } /* Buffer size after adding 1 element is: 2 Buffer size after adding 2 element is: 2 Buffer size after adding 3 element is: 3 Buffer size after adding 4 element is: 4 Buffer size after adding 5 element is: 6 */ print("Array size is: %d \n", a1.getLength()); // Array size is: 5 // print the items of the array for index = 0, index < 5, index++ { print("The value of element %d is: %d\n", index, a1(index)); } /* The value of element 0 is: 0 The value of element 1 is: 2 The value of element 2 is: 4 The value of element 3 is: 6 The value of element 4 is: 8 */
Generally we rarely need to use `getBufSize` function. It is used in rare cases, for example, when we need to improve performance or to deal in low-level with the memory usage.
Usually the results of `getBufSize` and `getLength` are equal, but it is not always necessary, the reason is that the dynamic array is trying to decrease the number of allocating and copying operations from the memory because these are slow operations, so mostly it reserve a memory larger than the number of items in case new items are added.
To add a new item at a specific location in the array we use `insert` function.
array_name.insert(index,value);
Where `index` represents the order of the new item in the array after inserting it.
To remove a specific item from the dynamic array we use `remove` function.
array_name.remove(index);
To remove all items from the array at once we use `clear` function.
array_name.clear();
Example:
In the next example we will alter the previous code to add the number 88 at index 2 from the array a1 then we will remove it, and finally we will clear the array.
import "Srl/Console.alusus"; import "Srl/Array.alusus"; use Srl; def a1: Array[Int]; Console.print("The size of the array is: %d \n", a1.getLength ()); Console.print("Initial size of the Buffer is: %d \n", a1.getBufSize ()); def index: int[32]; for index = 0, index < 5, index++ { a1.add(index * 2) } // add number 88 at index 2 (which means it will be the third item) a1.insert(2, 88); // print the array items for index = 0, index < 5, index++ { Console.print("The value of the element with index %d is: %d\n",index,a1(index)); } /* The value of the element with index 0 is: 0 The value of the element with index 1 is: 2 The value of the element with index 2 is: 88 The value of the element with index 3 is: 4 The value of the element with index 4 is: 6 */ // remove the item we added earlier a1.remove(2); // print the array items for index = 0,index < 5, index++ { Console.print("The value of the element with index %d is: %d\n",index,a1(index)); } /* The value of the element with index 0 is: 0 The value of the element with index 1 is: 2 The value of the element with index 2 is: 4 The value of the element with index 3 is: 6 The value of the element with index 4 is: 8 */ // clear all items from the array a1.clear(); Console.print("The size of the array is: %d \n", a1.getLength()); // The size of the array is: 0 Console.print("Buffer size after cleaning: %d \n", a1.getBufSize()); // Buffer size after cleaning: 0