Exercise - Create a method to display the results
In this step, you'll define the method to display the results of the animal groups. You'll also complete the animal group assignments for the other visiting schools. Let's get started!
Develop your method
Enter a new blank code line. Then define the
PrintGroupmethod by entering the following code.void PrintGroup(string[,] group) { }Remember that you decided this method would take a 2D array as input. Now you just need your method to print the contents of the 2D array.
Add the following code to the
PrintGroupmethod:void PrintGroup(string[,] group) { for (int i = 0; i < group.GetLength(0); i++) { for (int j = 0; j < group.GetLength(1); j++) { Console.Write($"{group[i,j]} "); } } }Here, you use
GetLength(0)to get the number of rows in the array, andGetLength(1)for the number of columns. Then you useConsole.Writeto print the element with some extra spaces between the animals. Next, let's add some helper text and a new line to separate the groups.Update your method with the following code:
void PrintGroup(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(); } }Now you have all the code in place to complete the task to prepare for the school visit.
Locate the
PrintGroupmethod call and remove the starting//characters.Next, you'll run your code and observe the results.
Save then run your code by entering dotnet run tt the Terminal command prompt.
To verify that your code is working as expected, check that the output of your application is similar the following output, taking into account the random order of the animals:
School A Group 1: ducks emus rabbits Group 2: lemurs chickens geese Group 3: iguanas macaws alpacas Group 4: goats kangaroos pigs Group 5: capybaras llamas tortoises Group 6: sheep ostriches poniesConsider that you need to perform the same tasks for each school. The differences would be the name of the school and the number of groups. This is a perfect opportunity to use a method with those parameters.
Create a new blank code line above the
RandomizeAnimals()method call. Then create a new method by entering the following code:void PlanSchoolVisit(string schoolName, int groups = 6) { RandomizeAnimals(); string[,] group = AssignGroup(); Console.WriteLine("School A"); PrintGroup(group); }With this code, you're able to perform the same tasks for each school. You also set
groupsto the default value of6. Next, you'll update your method to use the input values.Update your method with the following code:
void PlanSchoolVisit(string schoolName, int groups = 6) { RandomizeAnimals(); string[,] group = AssignGroup(groups); Console.WriteLine(schoolName); PrintGroup(group); }Now you're ready to call this method for each of the schools.
Create a new blank code line above the
PlanSchoolVisitmethod. Then enter the following code:PlanSchoolVisit("School A"); PlanSchoolVisit("School B", 3); PlanSchoolVisit("School C", 2);
Check your work
In this task, you'll run your application from the Integrated Terminal and verify your code is working correctly. Let's get started.
Compare your code to the following to ensure it's correct:
using System; string[] pettingZoo = { "alpacas", "capybaras", "chickens", "ducks", "emus", "geese", "goats", "iguanas", "kangaroos", "lemurs", "llamas", "macaws", "ostriches", "pigs", "ponies", "rabbits", "sheep", "tortoises", }; PlanSchoolVisit("School A"); PlanSchoolVisit("School B", 3); PlanSchoolVisit("School C", 2); void PlanSchoolVisit(string schoolName, int groups = 6) { RandomizeAnimals(); string[,] group = AssignGroup(groups); Console.WriteLine(schoolName); PrintGroup(group); } void RandomizeAnimals() { Random random = new Random(); for (int i = 0; i < pettingZoo.Length; i++) { int r = random.Next(i, pettingZoo.Length); string temp = pettingZoo[r]; pettingZoo[r] = pettingZoo[i]; pettingZoo[i] = temp; } } string[,] AssignGroup(int groups = 6) { string[,] result = new string[groups, pettingZoo.Length/groups]; int start = 0; for (int i = 0; i < groups; i++) { for (int j = 0; j < result.GetLength(1); j++) { result[i,j] = pettingZoo[start++]; } } return result; } void PrintGroup(string[,] groups) { for (int i = 0; i < groups.GetLength(0); i++) { Console.Write($"Group {i + 1}: "); for (int j = 0; j < groups.GetLength(1); j++) { Console.Write($"{groups[i,j]} "); } Console.WriteLine(); } }Save your work using Ctrl + S or using the Visual Studio Code File menu.
If necessary, open Visual Studio Code's Integrated Terminal panel.
In the EXPLORER panel, to open a Terminal at your TestProject folder location, right-click TestProject, and then select Open in Integrated Terminal.
At the Terminal command prompt, enter dotnet run
Check that the output of your application is similar to the following, taking into account the randomized order of animals:
School A Group 1: kangaroos lemurs pigs Group 2: alpacas sheep chickens Group 3: ducks geese capybaras Group 4: ponies iguanas tortoises Group 5: ostriches llamas rabbits Group 6: macaws goats emus School B Group 1: llamas ducks ponies geese chickens goats Group 2: iguanas capybaras macaws kangaroos rabbits sheep Group 3: lemurs tortoises alpacas pigs emus ostriches School C Group 1: sheep ducks pigs macaws kangaroos ostriches rabbits goats lemurs Group 2: iguanas capybaras chickens emus tortoises geese ponies alpacas llamasYou should check that School A has six groups, School B has three groups, and School C has two groups. The order of the animals should be randomized for each school.
If your code produces unexpected errors, you'll need to review your code to find your error and make updates. Run the code again to see if you've fixed the problem. Continue updating and running your code until your code produces the expected results.