The goals for this exercise are to
Prepare a 'utility method' that will be used in this week's lessons' exercises
Review how to pass an array as a parameter to a method
For this exercise, you will do neither searching, nor sorting.
What you need to do to prepare for this exercise:
It's possible to pass an array as an argument to a method (this has been covered in previous lessons). You should practice that by creating a method that will print out all the elements within an array.
Create a class named SearchingAndSorting
, and within it, create a method that does this printing. (Go
back and review the work from prior lessons, if you need help remembering how to do this.)
class SearchingAndSorting { public void printArray(int[] nums) { Console.WriteLine("The array is " + nums.Length + "integers long!"); // Your code goes here, to print out the array! } }
Also remember that in C#, unlike C++ or C, arrays know how long they are. For example, in the above example code,
you can get the length by using the Length
property. If you need to write a loop (or anything else) that needs
to know the length of the array, use this property (DON'T simply write the number into the code, and don't pass
the length as a separate parameter).
Add code to the Main
method of the program, and try printing out a couple of arrays (which you will also need to
created) so that you’ve tested out your method, and you feel confident that it works well.
Write a method to print out the contents of an integer array, as described above.
Note that this should be a very easy method to implement. The purpose of making you do this is to have it ready for the rest of this week’s PCEs.