|
@@ -7,11 +7,11 @@
|
|
//
|
|
//
|
|
const std = @import("std");
|
|
const std = @import("std");
|
|
|
|
|
|
-pub fn main() void {
|
|
|
|
- const my_numbers = [4]u16{ 5, 6, 7, 8 };
|
|
|
|
-
|
|
|
|
- printPowersOfTwo(my_numbers);
|
|
|
|
- std.debug.print("\n", .{});
|
|
|
|
|
|
+pub fn main() void{
|
|
|
|
+ const my_numbers = [4]u16{ 5, 6, 7, 8 };
|
|
|
|
+
|
|
|
|
+ _ = printPowersOfTwo(my_numbers);
|
|
|
|
+ std.debug.print("\n", .{});
|
|
}
|
|
}
|
|
|
|
|
|
// You won't see this every day: a function that takes an array with
|
|
// You won't see this every day: a function that takes an array with
|
|
@@ -21,23 +21,24 @@ pub fn main() void {
|
|
//
|
|
//
|
|
// This function prints, but does not return anything.
|
|
// This function prints, but does not return anything.
|
|
//
|
|
//
|
|
-fn printPowersOfTwo(numbers: [4]u16) ??? {
|
|
|
|
- loop (numbers) |n| {
|
|
|
|
- std.debug.print("{} ", .{twoToThe(n)});
|
|
|
|
- }
|
|
|
|
|
|
+fn printPowersOfTwo(numbers: [4]u16) void{
|
|
|
|
+ for(numbers) |n|{
|
|
|
|
+ std.debug.print("{} ", .{ twoToThe(n) });
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// This function bears a striking resemblance to twoToThe() in the last
|
|
// This function bears a striking resemblance to twoToThe() in the last
|
|
// exercise. But don't be fooled! This one does the math without the aid
|
|
// exercise. But don't be fooled! This one does the math without the aid
|
|
// of the standard library!
|
|
// of the standard library!
|
|
//
|
|
//
|
|
-fn twoToThe(number: u16) ??? {
|
|
|
|
- var n: u16 = 0;
|
|
|
|
- var total: u16 = 1;
|
|
|
|
-
|
|
|
|
- loop (n < number) : (n += 1) {
|
|
|
|
- total *= 2;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return ???;
|
|
|
|
|
|
+fn twoToThe(number: u16) u16{
|
|
|
|
+ var n: u16 = 0;
|
|
|
|
+ var total: u16 = 1;
|
|
|
|
+
|
|
|
|
+ while(n < number) : (n += 1){
|
|
|
|
+ total *= 2;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return total;
|
|
}
|
|
}
|
|
|
|
+
|