005_arrays2.zig 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // Zig has some fun array operators.
  3. //
  4. // You can use '++' to concatenate two arrays:
  5. //
  6. // const a = [_]u8{ 1,2 };
  7. // const b = [_]u8{ 3,4 };
  8. // const c = a ++ b ++ [_]u8{ 5 }; // equals 1 2 3 4 5
  9. //
  10. // You can use '**' to repeat an array:
  11. //
  12. // const d = [_]u8{ 1,2,3 } ** 2; // equals 1 2 3 1 2 3
  13. //
  14. const std = @import("std");
  15. pub fn main() void {
  16. const le = [_]u8{ 1, 3 };
  17. const et = [_]u8{ 3, 7 };
  18. // (Problem 1)
  19. // Please set this array concatenating the two arrays above.
  20. // It should result in: 1 3 3 7
  21. const leet = ???;
  22. // (Problem 2)
  23. // Please set this array using repetition.
  24. // It should result in: 1 0 0 1 1 0 0 1 1 0 0 1
  25. const bit_pattern = [_]u8{ ??? } ** 3;
  26. // Okay, that's all of the problems. Let's see the results.
  27. //
  28. // We could print these arrays with leet[0], leet[1],...but let's
  29. // have a little preview of Zig "for" loops instead:
  30. std.debug.print("LEET: ", .{});
  31. for (leet) |*n| {
  32. std.debug.print("{}", .{n.*});
  33. }
  34. std.debug.print(", Bits: ", .{});
  35. for (bit_pattern) |*n| {
  36. std.debug.print("{}", .{n.*});
  37. }
  38. std.debug.print("\n", .{});
  39. }