Advertisement
Guest User

asdasddsfsasdfdswd

a guest
May 29th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ArrayMethodPartB
  8. {
  9. class Program
  10. {
  11. const int STUDENTBATCHSIZE = 15;
  12.  
  13. static void Main(string[] args)
  14. {
  15. /*
  16. * This question asks us to modify the exisiting program for capturing and printing student names in alphabetical order
  17. * 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
  18. * ask the user to tell the system how many students we wish to enter-the system should be capable of handling any number
  19. * of student names without having to first tell the program the number.
  20. *
  21. * However, we are faced with a dilemma as arrays do not, by default, dynamically grow or shrink once they are created.
  22. * All solutions to this dilemma are unfortunately heavyweight in terms of CPU processing--ours will not be any different.
  23. * What we do is incrementally grow the array we create by making calls to Array.Resize() when we reach the end of the given
  24. * array. This is a fudge as Resize() doesn't actually grow our array--it simply creates a new array of the desired size
  25. * and copies over the elements from our exisitng array; the new array is then returned to us (and our original array is
  26. * marked for garabage collection/destruction)--this is clearly an expensive operation and becomes more expensive the larger
  27. * the original array is. The best solution to this dilemma requires us to use a different data structure than the array, i.e.,
  28. * a list of some sort
  29. */
  30.  
  31. string[] StudentNames = CaptureStudentNames();
  32.  
  33. // Remember that all arrays that we create actually inherit many features from System.Array (e.g., the Length property),
  34. // or can be sent as parameters to static functions provided by the System.Array class. Here we call the Sort() static
  35. // function provided by System.Array
  36. Array.Sort(StudentNames);
  37.  
  38. Console.WriteLine(Environment.NewLine + "Students in alphabetical order:");
  39. PrintStudentNames(StudentNames);
  40.  
  41. Console.ReadKey();
  42. }
  43.  
  44. static string[] CaptureStudentNames()
  45. {
  46. Console.WriteLine("Enter student names. Pressing enter on its own will terminate the student name entry process");
  47. string[] StudentNames = new string[STUDENTBATCHSIZE];
  48.  
  49. int index = 0;
  50. int NumOfStudentBatches = 1; // Keeps tabs on how many batches of student's we've processed
  51. bool TerminateLoop = false;
  52. do
  53. {
  54. // First we need to check if we've filled up the current array. Note the && index > 0 to cater for the edge case
  55. // where the first time ever through this loop the expression "index % STUDENTBATCHSIZE" will return 0-we don't want
  56. // to resize in this case
  57. if ((index % STUDENTBATCHSIZE == 0) && index > 0)
  58. {
  59. NumOfStudentBatches++;
  60. // Note the ref keyword--here we are passing the array StudentNames by reference as Resize() will change what StudentNames
  61. // refers to (or points to) in heap memory. This is because Resize() creates a new array in the heap and copies over
  62. // all the elements in the original StudentNames array. Finally, Resize() overwrites what StudentNames refers to so that
  63. // it now refers to our newly created array.
  64. Array.Resize(ref StudentNames, NumOfStudentBatches * STUDENTBATCHSIZE);
  65. }
  66.  
  67. Console.Write("Enter student name ({0}): ", index);
  68. string StudentName = Console.ReadLine();
  69. if (string.IsNullOrWhiteSpace(StudentName))
  70. {
  71. TerminateLoop = true;
  72. /* 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
  73. * due to entering only a handful of students in a given batch (say only 3, but we allocate STUDENTBATCHSIZE number of elements
  74. * in the array). If performance issues are a concern, then it might be we can leave the un-used slots and code accordingly.
  75. * Personally I don't like this approach as it means you and anyone else maintaing/managing your code will have to remember
  76. * this special situation too. (The reason we trim is that the unused slots will get "printed" out when we iterate over the array
  77. * in the PrintStudentNames() function, and they will appear as empty lines)
  78. */
  79. Array.Resize(ref StudentNames, index);
  80. }
  81. else
  82. {
  83. StudentNames[index++] = StudentName;
  84. }
  85. } while (TerminateLoop == false);
  86.  
  87. return StudentNames;
  88. }
  89.  
  90. static void PrintStudentNames(string[] studentNames)
  91. {
  92. for (int i = 0; i < studentNames.Length; i++)
  93. {
  94. Console.WriteLine(studentNames[i]);
  95. }
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement