Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const std = @import("std");
var items: [5]?[]const u8 = [_]?[]const u8{null} ** 5;
var prices: [5]f64 = [_]f64{0} ** 5;
var count: usize = 0;
var total: f64 = 0;
fn addItem(item: []const u8, price: f64) void {
if (count < items.len) {
items[count] = item;
prices[count] = price;
total += price;
count += 1;
}
}
fn removeItem(index: usize) void {
if (index < count) {
total -= prices[index];
for (index..count-1) |i| {
items[i] = items[i+1];
prices[i] = prices[i+1];
}
count -= 1;
}
}
fn printCart() void {
for (0..count) |i| {
std.debug.print("{s}: ${f}\n", .{items[i], prices[i]});
}
std.debug.print("Total: ${f}\n----------------\n", .{total});
}
pub fn main() void {
addItem("Apple", 2.0);
addItem("Banana", 3.0);
printCart();
removeItem(0);
printCart();
}Coding works best on desktop or with an external keyboard.