This is an old revision of the document!
For these exercises you can use the D Online Editor. If you wish to install the D compiler and Standard Library, go to the download page.
Compute the median element of an unsorted integer array. For this, you will have to:
D has a standard library called Phobos. Implement exercise 1 using functions from the std.algorithm package. Use Universal Function Call Syntax (UFCS) for an increase in expressiveness.
Using associative arrays:
struct ColleagueInfo { string telefon; string mail; }
Now go ahead and create your <name, ColleagueInfo> dictionary
ColleagueInfo[string] d;
Inspect the source file. Compile and run the code.
size_t numbersSum(T)(T[] numbers) { size_t sum = 0; foreach(num; numbers) { sum += num; } return sum; } void main() { int[] arr = [1, 2, 3, 4, 5]; writeln(numbersSum!int(arr)); // 15 float[] arr2 = [1.0, 2.0, 3.0, 4.0, 5.0]; writeln(numbersSum!float(arr2)); // 15 }
size_t numbersSum(T)(T[] numbers) if (is(T == int) || is(T == float)) { size_t sum = 0; foreach(num; numbers) { sum += num; } return sum; }
import std.traits : isNumeric; size_t numbersSum(T)(T[] numbers) if (isNumeric!T) { size_t sum = 0; foreach(num; numbers) { sum += num; } return sum; }