Added random order function

Added a function to generate an array containing an random order of all groups
main
Victor Hans-Georg Waitz 2024-10-25 04:53:24 +02:00
parent 0f046689c0
commit f5b0496975
1 changed files with 56 additions and 12 deletions

View File

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const writer = std.io.getStdOut().writer();
const Timestamp = struct { const Timestamp = struct {
hours: u64, hours: u64,
@ -6,32 +7,75 @@ const Timestamp = struct {
}; };
const testees:[]const []const u8 = &[_][] const u8 { const testees:[]const []const u8 = &[_][] const u8 {
"Nastja", "1",
"Vikkes", "2",
"Group 3", "3",
"4",
"5",
"6",
"7",
"8"
}; };
const number_of_testees: u64 = testees.len; // len von testees
const total_minutes: u64 = 60; // Gesamte Dauer in min const number_of_testees: u64 = testees.len; // len von testees
const pause_minutes: u64 = 2; // Länge der Pause zwischen 2 Testaten const total_minutes: u64 = 60; // Gesamte Dauer in min
const pause_minutes: u64 = 2; // Länge der Pause zwischen 2 Testaten
pub fn main () !void { pub fn main () !void {
const writer = std.io.getStdOut().writer();
var testStamp = Timestamp { var testStamp = Timestamp {
.hours = 13, .hours = 13,
.minutes = 5, .minutes = 5,
}; };
try writer.print("Amount tests: {d} \n", .{number_of_testees}); try writer.print("Amount tests: {d} \n\n", .{number_of_testees});
for (testees) |row| {
try writer.print("Name: {s} \n", .{row});
}
try printTimestamp(&testStamp); try printTimestamp(&testStamp);
try writer.print("\n", .{});
try printPlan();
} }
// Printet timestamp im Format HH:MM // Printet timestamp im Format HH:MM
pub fn printTimestamp(ts: *Timestamp) !void { pub fn printTimestamp(ts: *Timestamp) !void {
const writer = std.io.getStdOut().writer();
try writer.print("{d}:{d}", ts.*); try writer.print("{d}:{d}", ts.*);
} }
pub fn printPlan() !void {
const rand = std.crypto.random;
const noneNumber:u64 = number_of_testees+1;
var testees_order: [number_of_testees]u8 = undefined;
// Fill order array with noneNumber
for (testees_order, 0..) |_, index| {
testees_order[index] = noneNumber;
}
// Fill order array with a random order
for (testees, 0..) |_, blockIndex| {
while(true) {
const randIndex:u8 = rand.intRangeLessThan(u8, 0, number_of_testees);
if (arrayContains(&testees_order, randIndex)) {
continue;
}
testees_order[blockIndex] = randIndex;
break;
}
}
for (testees_order, 0..) |elem, index| {
try writer.print("Nr.{d}: {s} \n", .{index, testees[elem]});
}
}
pub fn arrayContains(arr: []u8, q: i64) bool {
for (arr) |element| {
if (element == q) {
return true;
}
}
return false;
}