037-046 completed

This commit is contained in:
Andrew Scott 2024-01-13 01:19:56 -05:00
parent c2b1610399
commit 6c5fdef6b1
Signed by: a
GPG key ID: 7CD5A5977E4931C1
10 changed files with 19 additions and 8 deletions

View file

@ -36,6 +36,7 @@ const Character = struct {
role: Role,
gold: u32,
experience: u32,
health: u8,
};
pub fn main() void {
@ -44,6 +45,7 @@ pub fn main() void {
.role = Role.wizard,
.gold = 20,
.experience = 10,
.health = 100,
};
// Glorp gains some gold.

View file

@ -43,6 +43,13 @@ pub fn main() void {
// Feel free to run this program without adding Zump. What does
// it do and why?
chars[1] = Character{
.role = Role.bard,
.gold = 10,
.health = 100,
.experience = 20,
};
// Printing all RPG characters in a loop:
for (chars, 0..) |c, num| {
std.debug.print("Character {} - G:{} H:{} XP:{}\n", .{

View file

@ -30,7 +30,7 @@ pub fn main() void {
// Please make num2 equal 5 using num1_pointer!
// (See the "cheatsheet" above for ideas.)
num2 = ???;
num2 = num1_pointer.*;
std.debug.print("num1: {}, num2: {}\n", .{ num1, num2 });
}

View file

@ -23,7 +23,7 @@ const std = @import("std");
pub fn main() void {
const a: u8 = 12;
const b: *u8 = &a; // fix this!
const b: *const u8 = &a; // fix this!
std.debug.print("a: {}, b: {}\n", .{ a, b.* });
}

View file

@ -31,7 +31,7 @@ pub fn main() void {
// Please define pointer "p" so that it can point to EITHER foo or
// bar AND change the value it points to!
??? p: ??? = undefined;
var p: *u8 = undefined;
p = &foo;
p.* += 1;

View file

@ -37,5 +37,5 @@ pub fn main() void {
// This function should take a reference to a u8 value and set it
// to 5.
fn makeFive(x: *u8) void {
??? = 5; // fix me!
x.* = 5; // fix me!
}

View file

@ -68,7 +68,7 @@ pub fn main() void {
// FIX ME!
// Please pass Glorp to printCharacter():
printCharacter(???);
printCharacter(&glorp);
}
// Note how this function's "c" parameter is a pointer to a Character struct.

View file

@ -19,12 +19,14 @@ const Elephant = struct {
pub fn main() void {
var elephantA = Elephant{ .letter = 'A' };
// (Please add Elephant B here!)
var elephantB = Elephant{ .letter = 'B' };
var elephantC = Elephant{ .letter = 'C' };
// Link the elephants so that each tail "points" to the next elephant.
// They make a circle: A->B->C->A...
elephantA.tail = &elephantB;
// (Please link Elephant B's tail to Elephant C here!)
elephantB.tail = &elephantC;
elephantC.tail = &elephantA;
visitElephants(&elephantA);

View file

@ -29,7 +29,7 @@ pub fn main() void {
// Please threaten the result so that answer is either the
// integer value from deepThought() OR the number 42:
const answer: u8 = result;
const answer: u8 = result orelse 42;
std.debug.print("The Ultimate Answer: {}.\n", .{answer});
}

View file

@ -21,7 +21,7 @@ const std = @import("std");
const Elephant = struct {
letter: u8,
tail: *Elephant = null, // Hmm... tail needs something...
tail: ?*Elephant = null, // Hmm... tail needs something...
visited: bool = false,
};
@ -51,7 +51,7 @@ fn visitElephants(first_elephant: *Elephant) void {
// We should stop once we encounter a tail that
// does NOT point to another element. What can
// we put here to make that happen?
if (e.tail == null) ???;
if (e.tail == null) break;
e = e.tail.?;
}