099-105 completed

This commit is contained in:
Andrew Scott 2024-06-21 08:49:10 -04:00
parent e06cf45604
commit 8a6106f82b
Signed by: a
GPG key ID: 7CD5A5977E4931C1
7 changed files with 13 additions and 13 deletions

View file

@ -131,7 +131,7 @@ pub fn main() !void {
for (0..size) |b| { for (0..size) |b| {
// What formatting is needed here to make our columns // What formatting is needed here to make our columns
// nice and straight? // nice and straight?
print("{???} ", .{(a + 1) * (b + 1)}); print("{d:>3} ", .{(a + 1) * (b + 1)});
} }
// After each row we use double line feed: // After each row we use double line feed:

View file

@ -39,7 +39,7 @@ pub fn main() void {
const hex_nums = [_]u8{ 0xb, 0x2a, 0x77 }; const hex_nums = [_]u8{ 0xb, 0x2a, 0x77 };
const dec_nums = [_]u8{ 11, 42, 119 }; const dec_nums = [_]u8{ 11, 42, 119 };
for (hex_nums, ???) |hn, ???| { for (hex_nums, dec_nums) |hn, dn| {
if (hn != dn) { if (hn != dn) {
std.debug.print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn }); std.debug.print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn });
return; return;

View file

@ -51,7 +51,7 @@ pub fn main() void {
// We would like to number our list starting with 1, not 0. // We would like to number our list starting with 1, not 0.
// How do we do that? // How do we do that?
for (roles, gold, experience, ???) |c, g, e, i| { for (roles, gold, experience, 1..) |c, g, e, i| {
const role_name = switch (c) { const role_name = switch (c) {
.wizard => "Wizard", .wizard => "Wizard",
.thief => "Thief", .thief => "Thief",

View file

@ -83,7 +83,7 @@ fn sub(a: f16, b: f16) f16 {
// an error that you need // an error that you need
// to correct. // to correct.
test "sub" { test "sub" {
try testing.expect(sub(10, 5) == 6); try testing.expect(sub(10, 5) == 5);
try testing.expect(sub(3, 1.5) == 1.5); try testing.expect(sub(3, 1.5) == 1.5);
} }
@ -108,5 +108,5 @@ test "divide" {
// Now we test if the function returns an error // Now we test if the function returns an error
// if we pass a zero as denominator. // if we pass a zero as denominator.
// But which error needs to be tested? // But which error needs to be tested?
try testing.expectError(error.???, divide(15, 0)); try testing.expectError(error.DivisionByZero, divide(15, 0));
} }

View file

@ -119,9 +119,9 @@
// after all we need some practice. Suppose we want to count the words // after all we need some practice. Suppose we want to count the words
// of this little poem: // of this little poem:
// //
// My name is Ozymandias, King of Kings; // My name is Ozymandias, King of Kings;
// Look on my Works, ye Mighty, and despair! // Look on my Works, ye Mighty, and despair!
// by Percy Bysshe Shelley // by Percy Bysshe Shelley
// //
// //
const std = @import("std"); const std = @import("std");
@ -136,7 +136,7 @@ pub fn main() !void {
; ;
// now the tokenizer, but what do we need here? // now the tokenizer, but what do we need here?
var it = std.mem.tokenizeAny(u8, poem, ???); var it = std.mem.tokenizeAny(u8, poem, " ,;!\n");
// print all words and count them // print all words and count them
var cnt: usize = 0; var cnt: usize = 0;

View file

@ -97,12 +97,12 @@ pub fn main() !void {
defer handle.join(); defer handle.join();
// Second thread // Second thread
const handle2 = try std.Thread.spawn(.{}, thread_function, .{-4}); // that can't be right? const handle2 = try std.Thread.spawn(.{}, thread_function, .{2}); // that can't be right?
defer handle2.join(); defer handle2.join();
// Third thread // Third thread
const handle3 = try std.Thread.spawn(.{}, thread_function, .{3}); const handle3 = try std.Thread.spawn(.{}, thread_function, .{3});
defer ??? // <-- something is missing defer handle3.join(); // <-- something is missing
// After the threads have been started, // After the threads have been started,
// they run in parallel and we can still do some work in between. // they run in parallel and we can still do some work in between.

View file

@ -81,8 +81,8 @@ pub fn main() !void {
defer handle1.join(); defer handle1.join();
// Second thread to calculate the minus numbers. // Second thread to calculate the minus numbers.
??? const handle2 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_minus, 3, count });
defer handle2.join();
} }
// Here we add up the results. // Here we add up the results.
std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus}); std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus});