An Array Example

Here is a simple program that shows how arrays can simplify things in a code.

Example code #

Let’s revisit the challenge where we printed the twice of the value, but this time we will print twice of five different values using an array:

Press + to interact
import std.stdio;
void main() {
// This variable is used as a loop counter
int counter;
// The definition of a fixed-length array of five
// elements of type double
double[5] values;
// setting the values in a loop
writeln("values");
while (counter < values.length) {
values[counter]=counter*2;
writeln(values[counter]);
++counter;
}
writeln("Twice the values:");
counter = 0;
while (counter < values.length) {
writeln(values[counter] * 2);
++counter;
}
// The loop that calculates the fifths of the values would
// be written similarly
}

Observations

...