ms_learn_csharp/030_project_petting_zoo/petting_zoo/Program.cs

49 lines
1.3 KiB
C#
Raw Normal View History

2024-08-11 21:59:22 -04:00
string[] animals = {
"alpacas", "capybaras", "chickens", "ducks", "emus", "geese",
"goats", "iguanas", "kangaroos", "lemurs", "llamas", "macaws",
"ostriches", "pigs", "ponies", "rabbits", "sheep", "tortoises",
};
plan_school_visit("School A");
plan_school_visit("School B", 3);
plan_school_visit("School C", 2);
void plan_school_visit(string school, int groups = 6) {
randomize_animals();
string[,] group = assign_group(groups);
Console.WriteLine(school);
print_group(group);
Console.WriteLine();
}
void randomize_animals() {
Random random = new Random();
for (int i = 0; i < animals.Length; i++) {
int r = random.Next(i, animals.Length);
string temp = animals[r];
animals[r] = animals[i];
animals[i] = temp;
}
}
string[,] assign_group(int group = 6) {
string[,] result = new string[group, animals.Length / group];
int start = 0;
for (int i = 0; i < group; i++) {
for (int j = 0; j < result.GetLength(1); j++) {
result[i, j] = animals[start++];
}
}
return result;
}
void print_group(string[,] group) {
for (int i = 0; i < group.GetLength(0); i++) {
Console.Write($"Group {i + 1}: ");
for (int j = 0; j < group.GetLength(1); j++) {
Console.Write($"{group[i,j]} ");
}
Console.WriteLine();
}
}