008_quiz.zig 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // Quiz time! Let's see if you can fix this whole program.
  3. //
  4. // You'll have to think about this one a bit.
  5. //
  6. // Let the compiler tell you what's wrong.
  7. //
  8. // Start at the top.
  9. //
  10. const std = @import("std");
  11. pub fn main() void {
  12. // What is this nonsense? :-)
  13. const letters = "YZhifg";
  14. // Note: usize is an unsigned integer type used for...sizes.
  15. // The exact size of usize depends on the target CPU
  16. // architecture. We could have used a u8 here, but usize is
  17. // the idiomatic type to use for array indexing.
  18. //
  19. // There IS a problem on this line, but 'usize' isn't it.
  20. const x: usize = 1;
  21. // Note: When you want to declare memory (an array in this
  22. // case) without putting anything in it, you can set it to
  23. // 'undefined'. There is no problem on this line.
  24. var lang: [3]u8 = undefined;
  25. // The following lines attempt to put 'Z', 'i', and 'g' into the
  26. // 'lang' array we just created by indexing the array
  27. // 'letters' with the variable 'x'. As you can see above, x=1
  28. // to begin with.
  29. lang[0] = letters[x];
  30. x = 3;
  31. lang[???] = letters[x];
  32. x = ???;
  33. lang[2] = letters[???];
  34. // We want to "Program in Zig!" of course:
  35. std.debug.print("Program in {s}!\n", .{lang});
  36. }