...
/Solution: Halve the Elements of the Array
Solution: Halve the Elements of the Array
Here is a solution to the coding challenge given in the previous lesson.
We'll cover the following...
Solution #
Here is the code that halves all the elements of the array which are greater than 10.
Press + to interact
import std.stdio;void Halve() {double[] array = [ 1, 20, 2, 30, 7, 11 ];double[] slice = array; // Start with a slice that// provides access to all of// the elements of the arraywhile (slice.length) { // As long as there is at least// one element in that sliceif (slice[0] > 10) { // Always use the first elementslice[0] /= 2; // in the expressions}slice = slice[1 .. $]; // Shorten the slice from the// beginning}int i = 0;while(i<array.length) {write(array[i]," ");++i;}}