[ Prev | Index | Next ]
Japanese English

Arrays

In this section, we'll explain arrays, which are variables capable of storing multiple values.

About Arrays

When you need to represent multiple values with a single variable name, you use an array.

For example, imagine you need to animate 100 points in a program. If you were to declare each point's x and y positions as separate variables, you'd need a total of 200 variables. Moreover, you'd have to write code to calculate and store values for each variable pair, which is not only laborious but also becomes challenging when you need to adjust the number of points.

In such cases, declaring the x and y positions of each point as arrays allows you to conveniently store all the x and y positions together. Additionally, as we'll see in subsequent sections, using control structures like loops makes it easy to perform operations on each point collectively.

Generally, effectively utilizing arrays (or lists, depending on the language) is crucial when dealing with large amounts of data in programming.

Array Declarations

When you need to represent multiple values with a single variable name, you use arrays.

When you need to represent multiple values with a single variable name, you use arrays.

To declare an array, you write it like this:

data_type   array_name[ array_size ] ;

Here, array_size specifies the number of values the array can hold. In VCSSL, it's also possible to write the size just after the data type like this:

data_type[ array_size ]   array_name ;

Both declarations have the same meaning in VCSSL.

As an example, let's prepare an array to store integer values:

or

Now, we have an array a capable of holding 5 integer values.

It's important to note that writing the size before the data type, as in the second example, would result in an error in C language. While VCSSL is similar to C, there are differences in array syntax, as we'll touch on later.

To store a value to or get a stored value from an array, you use the array name followed by an index in square brackets:

- Execution Result -

1

Executing this code will display 1. Here, we assigned a value to the third element of array "a".

Note that, indices of array elements start from 0 and end with (array_size - 1). It's crucial to remember that there's no element corresponding to the array_size-th index. For instance, in the example above, elements 0, 1, 2, 3, and 4 are available, and there's no element at index 5. Hence, an error occurs if you access a[5].

Arrays are often used in combination with loops. This allows for efficient manipulation and processing of large datasets. As an example, let's calculate the squares of integers from 0 to 10 and store them in an array:

- Execution Result -

64

Executing this code will display 64. This demonstrates how arrays can store computed values for later retrieval, showcasing their utility in data processing tasks.

Multidimensional Arrays

Arrays can have multiple indices, which are referred to as multidimensional arrays.

The declaration and usage of multidimensional arrays are essentially the same as those for one-dimensional arrays, except for using multiple sets of brackets [ ]. For example:

- Execution Result -

16

When executed, it displays 16.

In the above example, array "a" is a two-dimensional array. You can use any number of brackets [ ] to create arrays of higher dimensions.

Getting the Length of an Array

It is often necessary to get the number of elements in an array, because it may vary during program execution. In such cases, you can use the "length" function.

The length function retrieves the number of elements for the specified dimension of an array and returns the result as an integer value. You provide the array variable as the first argument and specify the dimension as subsequent arguments. Dimension index is counted from the left of the array as 0th dimension, 1st dimension, 2nd dimension, and so on.

- Execution Result -

3

When executed, it displays 3. This represents the number of elements in the 2nd dimension (counting from the left, as 0, 1, 2), which is the rightmost dimension of array a. Similarly, length(a, 0) returns 1, and length(a, 1) returns 2.

Omitting the Second Argument is Deprecated

Note that omitting the second argument in the length function is deprecated and should be avoided.

In the above example, if you use length(a), it returns an integer array (the size is 3) with the lengths of each dimension. However, this behavior is maintained for backward compatibility and might cause confusion because it counts dimensions from the right as 0, 1, 2.

Therefore, omitting the second argument in the length function is now considered deprecated.

Changing Array Size

To dynamically change the size of an array at runtime, the "alloc" function is used.

alloc( array, size_of_0th_dimension, size_of_1st_dimension, ... ) ;

This function dynamically adjusts the size of the specified array, which is provided as the first argument. Again, dimensions are indexed from 0, starting from the left.

Let's see how to use it. Write the following and execute it:

- Execution Result -

20

When executed, it displays 20. This means the size of array variable "a" was changed from 10 to 20 using the alloc function.

Before and after the size change using the alloc function, the values of each element in the array are maintained. For example, if a[1] initially has the value 255, it will retain this value even after resizing. This holds true for multidimensional arrays as well; corresponding elements at the same indices retain their values.

Assignment and Operations on All Elements of an Array

The content from this section onwards is not supported in many C-like programming languages and will result in errors. Therefore, please consider them as array operations unique to VCSSL.

In programming languages and libraries that focus on scientific computing, similar array operations are often supported, allowing for more intuitive coding of calculations involving matrices, tensors, and other complex structures. VCSSL adopts this approach with the same intention.

Firstly, if you want to assign the same value to all elements of an array, you can omit to specify indices as:

array = value ;

For example:

This assigns 1 to all elements of "a".

- Execution Result -

1

Furthermore, if you want to perform the same operation (e.g., addition) with the same value on all elements of an array without looping, you can simply write:

- Execution Result -

3

In the operation "a = a + 2" above, 2 is added to all elements of array a, resulting in 3. You can also write "a += 2" for the same effect.

Assigning an Array with One Element to a Non-Array Variable

On the other hand, In general, assigning an array to a non-array variable is not possible.

However, there is an exception: when the array has only one element. Arrays with a single element are essentially the same as regular variables in terms of data, so they can be assigned to non-array variables:

- Execution Result -

1

When executed, it displays 1.

Assignment Operation Between Arrays

When you assign another array to an array without specifying any indices, the values of each element become the same as the assigned array. Here is an example:

- Execution Result -

2

Executing this program will display 2.

When assigning arrays with different sizes, some caution is needed. In such cases, the size of the destination array (a) will be changed to match the size of the source array (b).

- Execution Result -

2

Executing this program will display 2, and the size of array a will become 20.

Note on Differences from C-like Languages

In VCSSL, while the syntax may resemble that of C-like languages, the behavior of array assignments is fundamentally different.

In contrast to C and its derivatives, where array assignment copies the reference (address) of the array, VCSSL array assignment copies the values of each element. This distinction is crucial for understanding how data is managed and manipulated within VCSSL programs.

For example, in many C-like languages, after an assignment operation is performed between arrays, the destination array (a) and the source array (b) of behave as if they are the same array (they reference the same data area). In this case, changes made to the destination array (a) after assignment will be reflected in the source array (b) as well. In the contrast, in VCSSL, changes made to the destination array (a) after assignment will not be reflected in the source array (b).

Acknowledgement: We greatly appreciate the cooperation of two ChatGPT AIs in translating this page.
» How we translated this page

Japanese English
Index
News From RINEARN
* VCSSL is developed by RINEARN.

Released the Latest Versions of RINEARN Graph and VCSSL - Now Supporting Customizable Tick Positions and Labels!
2024-11-24 - Starting with this update, a new "MANUAL" tick mode is now supported, allowing users to freely specify the positions and labels of ticks on the graph. We'll explain the details and how to use it.

Released Exevalator 2.2: Now Compatible with TypeScript and Usable in Web Browsers
2024-10-22 - The open-source expression evaluation library, Exevalator, has been updated to version 2.2. It now supports TypeScript and can be used for evaluating expressions directly in web browsers. Explains the details.

Behind the Scenes of Creating an Assistant AI (Part 1: Fundamental Knowledge)
2024-10-07 - The first part of a series on how to create an Assistant AI. In this article, we introduce the essential knowledge you need to grasp before building an Assistant AI. What exactly is an LLM-based AI? What is RAG? And more.

Launching an Assistant AI to Support Software Usage!
2024-09-20 - We've launched an Assistant AI that answers questions about how to use RINEARN software and helps with certain tasks. Anyone with a ChatGPT account can use it for free. We'll explain how to use it.

Software Updates: Command Expansion in RINEARN Graph, and English Support in VCSSL
2024-02-05 - We updated our apps. This updates include "Enhancing the Command-Line Features of RINEARN Graph" and "Adding English Support to VCSSL." Dives into each of them!

Inside the Repetitive Execution Speedup Impremented in Vnano Ver.1.1
2024-01-17 - Delves into the update in Vnano 1.1 from a developer's viewpoint, providing detailed insights into the specific improvements made to the internal structure of the script engine.

Scripting Engine Vnano Ver.1.1 Released: Dramatic Speed Improvement for Repetitive Executions of the Same Content
2023-12-22 - Released the Vnano script engine Ver.1.1. In this version, we've made significant enhancements in processing speed by reducing the overhead of handling requests. Explains the details.

RINEARN Graph 3D Updated and Next Version (Ver.6) Development Has Begun!
2023-09-04 - We have released RINEARN Graph 3D Ver.5.6.34. In addition, we have initiated the development of the next major version, Ver.6! Details inside.

Updated Contents
Circular Wave Animation

Draws the circular wave as 3D animation, under the specified wave parameters.
2022-12-14
Sine Wave Animation

Draws the sine wave as animation, under the specified wave parameters.
2022-11-26
Tool For Converting Units of Angles: Degrees and Radians

A GUI tool for converting the angle in degrees into radians, or radians into degrees.
2022-11-22
Connector Fatal Exception - Specification
The unchecked exception thrown when errors have occurred, caused by incorrect implementations (might be bugs).
2022-09-26
Connector Exception - Specification
The checked exception thrown when errors have occurred, cause by expected normal problems.
2022-09-26