2023-04-12 11:14:18 -04:00
|
|
|
//
|
2023-03-08 12:50:10 -05:00
|
|
|
// In most of the examples so far, the inputs are known at compile
|
|
|
|
// time, thus the amount of memory used by the program is fixed.
|
|
|
|
// However, if responding to input whose size is not known at compile
|
|
|
|
// time, such as:
|
2023-02-28 20:54:42 -05:00
|
|
|
// - user input via command-line arguments
|
|
|
|
// - inputs from another program
|
|
|
|
//
|
2023-03-08 12:50:10 -05:00
|
|
|
// You'll need to request memory for your program to be allocated by
|
|
|
|
// your operating system at runtime.
|
2023-02-28 20:54:42 -05:00
|
|
|
//
|
2023-03-08 12:50:10 -05:00
|
|
|
// Zig provides several different allocators. In the Zig
|
|
|
|
// documentation, it recommends the Arena allocator for simple
|
|
|
|
// programs which allocate once and then exit:
|
2023-02-28 20:54:42 -05:00
|
|
|
//
|
|
|
|
// const std = @import("std");
|
|
|
|
//
|
2023-03-08 12:50:10 -05:00
|
|
|
// // memory allocation can fail, so the return type is !void
|
2023-02-28 20:54:42 -05:00
|
|
|
// pub fn main() !void {
|
2023-03-08 12:50:10 -05:00
|
|
|
//
|
2023-02-28 20:54:42 -05:00
|
|
|
// var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
|
|
// defer arena.deinit();
|
|
|
|
//
|
|
|
|
// const allocator = arena.allocator();
|
|
|
|
//
|
|
|
|
// const ptr = try allocator.create(i32);
|
|
|
|
// std.debug.print("ptr={*}\n", .{ptr});
|
|
|
|
//
|
2023-03-08 13:09:41 -05:00
|
|
|
// const slice_ptr = try allocator.alloc(f64, 5);
|
2023-05-07 05:08:03 -04:00
|
|
|
// std.debug.print("slice_ptr={*}\n", .{slice_ptr});
|
2023-02-28 20:54:42 -05:00
|
|
|
// }
|
|
|
|
|
2024-05-06 03:13:56 -04:00
|
|
|
// Instead of a simple integer or a slice with a constant size,
|
|
|
|
// this program requires allocating a slice that is the same size
|
|
|
|
// as an input array.
|
2023-02-28 20:54:42 -05:00
|
|
|
|
2023-03-08 12:50:10 -05:00
|
|
|
// Given a series of numbers, take the running average. In other
|
|
|
|
// words, each item N should contain the average of the last N
|
|
|
|
// elements.
|
2023-02-28 20:54:42 -05:00
|
|
|
|
|
|
|
const std = @import("std");
|
|
|
|
|
2023-03-08 12:50:10 -05:00
|
|
|
fn runningAverage(arr: []const f64, avg: []f64) void {
|
2023-02-28 20:54:42 -05:00
|
|
|
var sum: f64 = 0;
|
|
|
|
|
|
|
|
for (0.., arr) |index, val| {
|
|
|
|
sum += val;
|
2023-06-26 17:43:39 -04:00
|
|
|
const f_index: f64 = @floatFromInt(index + 1);
|
|
|
|
avg[index] = sum / f_index;
|
2023-02-28 20:54:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
// pretend this was defined by reading in user input
|
2023-11-21 09:01:22 -05:00
|
|
|
const arr: []const f64 = &[_]f64{ 0.3, 0.2, 0.1, 0.1, 0.4 };
|
2023-02-28 20:54:42 -05:00
|
|
|
|
|
|
|
// initialize the allocator
|
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
|
|
|
|
|
|
// free the memory on exit
|
|
|
|
defer arena.deinit();
|
|
|
|
|
2023-03-08 12:50:10 -05:00
|
|
|
// initialize the allocator
|
2023-02-28 20:54:42 -05:00
|
|
|
const allocator = arena.allocator();
|
|
|
|
|
2023-03-08 19:22:17 -05:00
|
|
|
// allocate memory for this array
|
2024-06-19 12:19:46 -04:00
|
|
|
const avg: []f64 = try allocator.alloc(f64, arr.len);
|
2023-02-28 20:54:42 -05:00
|
|
|
|
|
|
|
runningAverage(arr, avg);
|
|
|
|
std.debug.print("Running Average: ", .{});
|
|
|
|
for (avg) |val| {
|
|
|
|
std.debug.print("{d:.2} ", .{val});
|
|
|
|
}
|
2023-03-09 06:20:45 -05:00
|
|
|
std.debug.print("\n", .{});
|
2023-02-28 20:54:42 -05:00
|
|
|
}
|
|
|
|
|
2023-03-08 12:50:10 -05:00
|
|
|
// For more details on memory allocation and the different types of
|
|
|
|
// memory allocators, see https://www.youtube.com/watch?v=vHWiDx_l4V0
|