Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ArrayMethodPartB
- {
- class Program
- {
- const int STUDENTBATCHSIZE = 15;
- static void Main(string[] args)
- {
- /*
- * This question asks us to modify the exisiting program for capturing and printing student names in alphabetical order
- * so that the we no longer ask the user to tell us how many student names they wish to enter. It does seem unnatural to
- * ask the user to tell the system how many students we wish to enter-the system should be capable of handling any number
- * of student names without having to first tell the program the number.
- *
- * However, we are faced with a dilemma as arrays do not, by default, dynamically grow or shrink once they are created.
- * All solutions to this dilemma are unfortunately heavyweight in terms of CPU processing--ours will not be any different.
- * What we do is incrementally grow the array we create by making calls to Array.Resize() when we reach the end of the given
- * array. This is a fudge as Resize() doesn't actually grow our array--it simply creates a new array of the desired size
- * and copies over the elements from our exisitng array; the new array is then returned to us (and our original array is
- * marked for garabage collection/destruction)--this is clearly an expensive operation and becomes more expensive the larger
- * the original array is. The best solution to this dilemma requires us to use a different data structure than the array, i.e.,
- * a list of some sort
- */
- string[] StudentNames = CaptureStudentNames();
- // Remember that all arrays that we create actually inherit many features from System.Array (e.g., the Length property),
- // or can be sent as parameters to static functions provided by the System.Array class. Here we call the Sort() static
- // function provided by System.Array
- Array.Sort(StudentNames);
- Console.WriteLine(Environment.NewLine + "Students in alphabetical order:");
- PrintStudentNames(StudentNames);
- Console.ReadKey();
- }
- static string[] CaptureStudentNames()
- {
- Console.WriteLine("Enter student names. Pressing enter on its own will terminate the student name entry process");
- string[] StudentNames = new string[STUDENTBATCHSIZE];
- int index = 0;
- int NumOfStudentBatches = 1; // Keeps tabs on how many batches of student's we've processed
- bool TerminateLoop = false;
- do
- {
- // First we need to check if we've filled up the current array. Note the && index > 0 to cater for the edge case
- // where the first time ever through this loop the expression "index % STUDENTBATCHSIZE" will return 0-we don't want
- // to resize in this case
- if ((index % STUDENTBATCHSIZE == 0) && index > 0)
- {
- NumOfStudentBatches++;
- // Note the ref keyword--here we are passing the array StudentNames by reference as Resize() will change what StudentNames
- // refers to (or points to) in heap memory. This is because Resize() creates a new array in the heap and copies over
- // all the elements in the original StudentNames array. Finally, Resize() overwrites what StudentNames refers to so that
- // it now refers to our newly created array.
- Array.Resize(ref StudentNames, NumOfStudentBatches * STUDENTBATCHSIZE);
- }
- Console.Write("Enter student name ({0}): ", index);
- string StudentName = Console.ReadLine();
- if (string.IsNullOrWhiteSpace(StudentName))
- {
- TerminateLoop = true;
- /* We have to decide at this point whether we trim the array we have as it is possible we have un-used slots in our array
- * due to entering only a handful of students in a given batch (say only 3, but we allocate STUDENTBATCHSIZE number of elements
- * in the array). If performance issues are a concern, then it might be we can leave the un-used slots and code accordingly.
- * Personally I don't like this approach as it means you and anyone else maintaing/managing your code will have to remember
- * this special situation too. (The reason we trim is that the unused slots will get "printed" out when we iterate over the array
- * in the PrintStudentNames() function, and they will appear as empty lines)
- */
- Array.Resize(ref StudentNames, index);
- }
- else
- {
- StudentNames[index++] = StudentName;
- }
- } while (TerminateLoop == false);
- return StudentNames;
- }
- static void PrintStudentNames(string[] studentNames)
- {
- for (int i = 0; i < studentNames.Length; i++)
- {
- Console.WriteLine(studentNames[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement