From adf5ddb27df7f5a22b0b7d3321dfc8bca1e7937a Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Sun, 7 Feb 2021 11:06:51 -0500 Subject: Consistent instructions and examples I started off with "hints" that required the poor student to piece together the information from incomplete bits. A complete example is like a picture that is worth 1000 words and far clearer. --- 04_arrays.zig | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to '04_arrays.zig') diff --git a/04_arrays.zig b/04_arrays.zig index a509800..0f4ffe1 100644 --- a/04_arrays.zig +++ b/04_arrays.zig @@ -1,31 +1,49 @@ // // Let's learn some array basics. Arrays are declared with: // -// const foo [size] = [size]{ values }; +// var foo [3]u32 = [3]u32{ 42, 108, 5423 }; // // When Zig can infer the size of the array, you can use '_' for the // size. You can also let Zig infer the type of the value so the // declaration is much less verbose. // -// const foo = [_]{ values }; +// var foo = [_]u32{ 42, 108, 5423 }; +// +// Get values of an array using array[index] notation: +// +// const bar = foo[3]; // 5423 +// +// Set values of an array using array[index] notation: +// +// foo[3] = 16; +// +// Get the length of an array using the len property: +// +// const length = foo.len; // const std = @import("std"); pub fn main() void { - + // (Problem 1) + // This "const" is going to cause a problem later - can you see what it is? + // How do we fix it? const some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 }; - // Individual values can be set with '[]' notation. Let's fix - // the first prime (it should be 2!): + // Individual values can be set with '[]' notation. + // Example: This line changes the first prime to 2 (which is correct): some_primes[0] = 2; // Individual values can also be accessed with '[]' notation. + // Example: This line stores the first prime in "first": const first = some_primes[0]; - // Looks like we need to complete this expression (like 'first'): - const fourth = ???; + // (Problem 2) + // Looks like we need to complete this expression. Use the example + // above to set "fourth" to the fourth element of the some_primes array: + const fourth = some_primes[???]; - // Use '.len' to get the length of the array: + // (Problem 3) + // Use the len property to get the length of the array: const length = some_primes.???; std.debug.print("First: {}, Fourth: {}, Length: {}\n", -- cgit v1.2.3-ZIG