2021-01-04 02:34:26 +01:00
|
|
|
//
|
2021-01-06 23:41:53 +01:00
|
|
|
// Let's learn some array basics. Arrays are declared with:
|
2021-01-04 02:34:26 +01:00
|
|
|
//
|
2021-02-07 17:06:51 +01:00
|
|
|
// var foo [3]u32 = [3]u32{ 42, 108, 5423 };
|
2021-01-04 02:34:26 +01:00
|
|
|
//
|
|
|
|
// When Zig can infer the size of the array, you can use '_' for the
|
2021-01-06 23:41:53 +01:00
|
|
|
// size. You can also let Zig infer the type of the value so the
|
|
|
|
// declaration is much less verbose.
|
2021-01-04 02:34:26 +01:00
|
|
|
//
|
2021-02-07 17:06:51 +01:00
|
|
|
// var foo = [_]u32{ 42, 108, 5423 };
|
|
|
|
//
|
|
|
|
// Get values of an array using array[index] notation:
|
|
|
|
//
|
2021-02-13 05:55:26 +01:00
|
|
|
// const bar = foo[2]; // 5423
|
2021-02-07 17:06:51 +01:00
|
|
|
//
|
|
|
|
// Set values of an array using array[index] notation:
|
|
|
|
//
|
2021-02-13 05:55:26 +01:00
|
|
|
// foo[2] = 16;
|
2021-02-07 17:06:51 +01:00
|
|
|
//
|
|
|
|
// Get the length of an array using the len property:
|
|
|
|
//
|
|
|
|
// const length = foo.len;
|
2021-01-04 02:34:26 +01:00
|
|
|
//
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
pub fn main() void {
|
2021-02-07 17:06:51 +01:00
|
|
|
// (Problem 1)
|
|
|
|
// This "const" is going to cause a problem later - can you see what it is?
|
|
|
|
// How do we fix it?
|
2021-01-06 23:41:53 +01:00
|
|
|
const some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
|
|
|
|
|
2021-02-07 17:06:51 +01:00
|
|
|
// Individual values can be set with '[]' notation.
|
|
|
|
// Example: This line changes the first prime to 2 (which is correct):
|
2021-01-06 23:41:53 +01:00
|
|
|
some_primes[0] = 2;
|
|
|
|
|
|
|
|
// Individual values can also be accessed with '[]' notation.
|
2021-02-07 17:06:51 +01:00
|
|
|
// Example: This line stores the first prime in "first":
|
2021-01-04 02:34:26 +01:00
|
|
|
const first = some_primes[0];
|
|
|
|
|
2021-02-07 17:06:51 +01:00
|
|
|
// (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[???];
|
2021-01-04 02:34:26 +01:00
|
|
|
|
2021-02-07 17:06:51 +01:00
|
|
|
// (Problem 3)
|
|
|
|
// Use the len property to get the length of the array:
|
2021-01-04 02:34:26 +01:00
|
|
|
const length = some_primes.???;
|
|
|
|
|
|
|
|
std.debug.print("First: {}, Fourth: {}, Length: {}\n",
|
|
|
|
.{first, fourth, length});
|
|
|
|
}
|