aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-01-06 17:41:53 -0500
committerDave Gauer <dave@ratfactor.com>2021-01-06 17:41:53 -0500
commitb9b89737fca7cc80b7d1b1527445e0ec71b25dda (patch)
treeb922cc620d25392892b91a0b634925e828edfcd7
parent30ef32e238465e67321d15c562d6313043dd12d7 (diff)
downloadziglings-b9b89737fca7cc80b7d1b1527445e0ec71b25dda.tar.gz
ziglings-b9b89737fca7cc80b7d1b1527445e0ec71b25dda.tar.bz2
ziglings-b9b89737fca7cc80b7d1b1527445e0ec71b25dda.tar.xz
ziglings-b9b89737fca7cc80b7d1b1527445e0ec71b25dda.zip
Added first quiz
-rw-r--r--03_assignment.zig4
-rw-r--r--04_arrays.zig24
-rw-r--r--08_quiz.zig34
-rwxr-xr-xziglings1
4 files changed, 50 insertions, 13 deletions
diff --git a/03_assignment.zig b/03_assignment.zig
index 6280833..2c4c15a 100644
--- a/03_assignment.zig
+++ b/03_assignment.zig
@@ -24,7 +24,7 @@ pub fn main() void {
// Perhaps you noticed before that the print function takes two
// parameters. Now it will make more sense: the first parameter
// is a string. The string may contain placeholders '{}', and the
- // second parameter is an anonymous struct (data structure)
- // with values to be printed in place of the placeholders.
+ // second parameter is an "anonymous list literal" (don't worry
+ // about this for now!) with the values to be printed.
std.debug.print("{} {} {}\n", .{n, pi, negative_eleven});
}
diff --git a/04_arrays.zig b/04_arrays.zig
index 2e3c208..a509800 100644
--- a/04_arrays.zig
+++ b/04_arrays.zig
@@ -1,26 +1,28 @@
//
-// Let's learn some array basics. Arrays literals are declared with:
+// Let's learn some array basics. Arrays are declared with:
//
-// [size]<type>{ values };
+// const foo [size]<type> = [size]<type>{ values };
//
// When Zig can infer the size of the array, you can use '_' for the
-// size like so:
+// size. You can also let Zig infer the type of the value so the
+// declaration is much less verbose.
//
-// [_]<type>{ values };
+// const foo = [_]<type>{ values };
//
const std = @import("std");
pub fn main() void {
- const some_primes = [_]u8{ 2, 3, 5, 7, 11, 13, 17, 19 };
- // Array values are accessed using square bracket '[]' notation.
- //
- // (Note that when Zig can infer the type (u8 in this case) of a
- // value, we don't have to manually specify 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!):
+ some_primes[0] = 2;
+
+ // Individual values can also be accessed with '[]' notation.
const first = some_primes[0];
- // Looks like we need to complete this expression:
+ // Looks like we need to complete this expression (like 'first'):
const fourth = ???;
// Use '.len' to get the length of the array:
diff --git a/08_quiz.zig b/08_quiz.zig
new file mode 100644
index 0000000..e23f856
--- /dev/null
+++ b/08_quiz.zig
@@ -0,0 +1,34 @@
+//
+// Quiz time! Let's see if you can fix this whole program.
+//
+// This is meant to be challenging.
+//
+// Let the compiler tell you what's wrong.
+//
+// Start at the top.
+//
+const std = @import("std");
+
+pub fn main() void {
+ // What is this nonsense? :-)
+ const letters = "YZhifg";
+
+ const x: u8 = 1;
+
+ // This is something you haven't seen before: declaring an array
+ // without putting anything in it. There is no error here:
+ var lang: [3]u8 = undefined;
+
+ // The following lines attempt to put 'Z', 'i', and 'g' into the
+ // 'lang' array we just created.
+ lang[0] = letters[x];
+
+ x = 3;
+ lang[???] = letters[x];
+
+ x = ???;
+ lang[2] = letters[???];
+
+ // We want to "Program in Zig!" of course:
+ std.debug.print("Program in {}!\n", .{lang});
+}
diff --git a/ziglings b/ziglings
index 98f8a50..7825cf6 100755
--- a/ziglings
+++ b/ziglings
@@ -72,6 +72,7 @@ check_it 04_arrays.zig "Fourth: 7, Length: 8" "There are two things to complete
check_it 05_arrays2.zig "LEET: 1337, Bits: 100110011001" "Fill in the two arrays."
check_it 06_strings.zig "d=d ha ha ha Major Tom" "Each '???' needs something filled in."
check_it 07_strings2.zig "Ziggy" "Please fix the lyrics!"
+check_it 08_quiz.zig "Program in Zig" "See if you can fix the program!"
echo
echo " __ __ _ "