1
0
Fork 0
mirror of https://codeberg.org/ziglings/exercises.git synced 2025-06-08 01:57:02 +09:00

Space sentinel

This commit is contained in:
jacwil 2024-01-06 16:38:06 -08:00
parent 0d46acfa02
commit 2bf830a4d5
3 changed files with 27 additions and 16 deletions

View file

@ -913,7 +913,7 @@ const exercises = [_]Exercise{
},
.{
.main_file = "076_sentinels.zig",
.output = "Array:123056. Many-item pointer:123.",
.output = "Array:[ 1 2 3 0 5 6 ]. Many-item pointer:[ 1 2 3 ]. Array:[ 30 31 32 20 34 35 ]. Many-item pointer:[ 30 31 32 ].",
},
.{
.main_file = "077_sentinels2.zig",

View file

@ -48,22 +48,33 @@ pub fn main() void {
// And here's a zero-terminated many-item pointer:
const ptr: [*:0]u32 = &nums;
// For fun, let's replace the value at position 3 with the
// sentinel value 0. This seems kind of naughty.
nums[3] = 0;
// Just to show not everything must be zero-terminated, here
// is a space-terminated array of u8 values 👾:
var space = [_:' ']u8{ '0', '1', '2', '3', '4', '5' };
// So now we have a zero-terminated array and a many-item
// pointer that reference the same data: a sequence of
// numbers that both ends in and CONTAINS the sentinel value.
// And here's a space-terminated many-item pointer:
const spaceptr: [*:' ']u8 = &space;
// For fun, let's replace the value at position 3 with the
// sentinel value. This seems kind of naughty.
nums[3] = 0;
space[3] = ' ';
// Now we have a zero-terminated array, a space-terminated
// array, and a couple of many-item pointers that reference
// the same data: sequences of numbers that both end in and
// CONTAIN the sentinel value.
//
// Attempting to loop through and print both of these should
// demonstrate how they are similar and different.
//
// (It turns out that the array prints completely, including
// the sentinel 0 in the middle. The many-item pointer stops
// the sentinel in the middle. The many-item pointer stops
// at the first sentinel value.)
printSequence(nums);
printSequence(ptr);
printSequence(space);
printSequence(spaceptr);
print("\n", .{});
}
@ -79,27 +90,27 @@ fn printSequence(my_seq: anytype) void {
// depending on which type of my_seq was passed in:
switch (my_typeinfo) {
.Array => {
print("Array:", .{});
print("Array:[", .{});
// Loop through the items in my_seq.
for (???) |s| {
print("{}", .{s});
print(" {x}", .{s});
}
},
.Pointer => {
// Check this out - it's pretty cool:
const my_sentinel = sentinel(@TypeOf(my_seq));
print("Many-item pointer:", .{});
print("Many-item pointer:[", .{});
// Loop through the items in my_seq until we hit the
// sentinel value.
var i: usize = 0;
while (??? != my_sentinel) {
print("{}", .{my_seq[i]});
print(" {x}", .{my_seq[i]});
i += 1;
}
},
else => unreachable,
}
print(". ", .{});
print(" ]. ", .{});
}

View file

@ -1,12 +1,12 @@
--- exercises/076_sentinels.zig 2023-10-03 22:15:22.125574535 +0200
+++ answers/076_sentinels.zig 2023-10-05 20:04:07.186102649 +0200
@@ -82,7 +82,7 @@
print("Array:", .{});
print("Array:[", .{});
// Loop through the items in my_seq.
- for (???) |s| {
+ for (my_seq) |s| {
print("{}", .{s});
print(" {x}", .{s});
}
},
@@ -94,7 +94,7 @@
@ -15,6 +15,6 @@
var i: usize = 0;
- while (??? != my_sentinel) {
+ while (my_seq[i] != my_sentinel) {
print("{}", .{my_seq[i]});
print(" {x}", .{my_seq[i]});
i += 1;
}