Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.58 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 ICA15
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. do
  14. {
  15. int size = 0; //user input for size of array
  16. double[] marks; //mark array
  17. string[] strings; //string array
  18.  
  19. //reset console
  20. Console.Clear();
  21.  
  22. //title
  23. Console.WriteLine("\t\t\tDale Chen-Song Assignment 15\n");
  24.  
  25. //get value for size of array
  26. GetValue("Enter the number of students in the class: ", 4, 10, out size);
  27.  
  28. //get array of marks
  29. marks = MakeRecords(size);
  30.  
  31. //get array of strings
  32. strings = MakeRecords(size, 5, 12);
  33.  
  34. //display the contents of the arrays
  35. Show(marks, strings);
  36.  
  37. //display average of the marks
  38. Average(marks, strings);
  39.  
  40. //display those who have failed
  41. Fails(marks, strings);
  42.  
  43. //display message asking user if they want to do it again
  44. Console.Write("\nRun again? y/n: ");
  45. //if user inputs yes, loop
  46. } while (Console.ReadLine().ToLower() == "y");
  47. }
  48. //***********************************************************************************
  49. //Method: static private void GetValue(string prompt, int min, int max, out int input)
  50. //Purpose: get integer value from user with error and range checking
  51. //Parameters: string prompt- prompt to display to the user
  52. // int min- the minimum value of range for value
  53. // int max- the maximum value of range for value
  54. // out int input- input out the valid integer
  55. //***********************************************************************************
  56. static private void GetValue(string prompt, int min, int max, out int input)
  57. {
  58. bool success = false; //success flag, true if success
  59.  
  60. //repeat until input is right
  61. do
  62. {
  63. //user input value for integer
  64. Console.Write(prompt);
  65. success = int.TryParse(Console.ReadLine(), out input);
  66.  
  67. //check if the value is valid
  68. if (!success)
  69. {
  70. //display error message if invalid number, prompt user to try again
  71. Console.WriteLine("An invalid number was entered, please try again.");
  72. }
  73. //check if the value is within the range
  74. else if (input < min || input > max)
  75. {
  76. //display error message if out of range, prompt user to try again
  77. Console.WriteLine("An out of range number was entered, please try again.");
  78. }
  79. }//continue looping until the input is correct
  80. while (!success || input < min || input > max);
  81. }
  82. static private double[] MakeRecords(int input)
  83. {
  84. Random generator = new Random(); //random number generator
  85.  
  86. //create the array of doubles to the specified size from user
  87. double[] array = new double[input];
  88.  
  89. //randomly generate doubles numbers into the doubles array
  90. for (int i = 0; i < array.Length; ++i)
  91. {
  92. array[i] = (generator.NextDouble() * 100);
  93. }
  94. //return the double array to the main program
  95. return array;
  96. }
  97. static private string[] MakeRecords(int input, int min, int max)
  98. {
  99. Random generator = new Random(); //random number generator
  100. string name =""; //random string holder
  101.  
  102. //create the array of strings to the specified size from user
  103. string[] array = new string[input];
  104.  
  105. //randomly generate strings into the string array
  106. for (int i = 0; i < array.Length; ++i)
  107. {
  108. //reset string for name
  109. name = "";
  110.  
  111. //
  112. for (int j = 0; j < generator.Next(5, 12); j++)
  113. {
  114. //if it is the first letter, make it a random uppercase
  115. if (j == 0)
  116. {
  117. //generate a random uppercase letter
  118. char upperCase = (char)(generator.Next(65, 90));
  119.  
  120. //add that character to the string
  121. name += upperCase.ToString();
  122. }
  123. //otherwise make it a random lowercase letter
  124. else
  125. {
  126. //generate a random lowercase letter
  127. char letter = (char)(generator.Next(97, 122));
  128.  
  129. //add that character to the string
  130. name += letter.ToString();
  131. }
  132. }
  133. //have the generated random string in the array
  134. array[i] = name;
  135. }
  136. //return the string array to the main program
  137. return array;
  138. }
  139. //***********************************************************************************
  140. //Method: static private void Show(double[] arrayMarks, string[] arrayStrings)
  141. //Purpose: display the contents of both the marks array and strings array
  142. //Parameters: double[] arrayMarks- the randomly generated double array to display
  143. // string[] arrayStrings- randomly generated string array to display
  144. //***********************************************************************************
  145. static private void Show(double[] arrayMarks, string[] arrayStrings)
  146. {
  147. //displays the contents of both arrays
  148. Console.WriteLine($"\n{"Name",15} {"Mark",-15}");
  149. Console.WriteLine($"{"----",15} {"---",-15}");
  150. for (int i = 0; i < arrayMarks.Length; ++i)
  151. {
  152. Console.WriteLine($"{arrayStrings[i], 15:F1} : {arrayMarks[i], -20:F1}");
  153. }
  154. }
  155. //***********************************************************************************
  156. //Method: static private double Average(int[] array)
  157. //Purpose: caculate the average of the integers in the array
  158. //Parameters: int[] array- randomnly generated integer array
  159. //Returns: double average- the calculated average of the array
  160. //***********************************************************************************
  161. static private void Average(double[] arrayMarks, string[] arrayStrings)
  162. {
  163. double average = 0; //average of the values in the array
  164. int total = 0; //sum of the values in the array
  165.  
  166. //sum up the total value within the array
  167. foreach (int value in arrayMarks)
  168. {
  169. total += value;
  170. }
  171.  
  172. //calculating the average of the array
  173. average = (double)total / arrayMarks.Length;
  174.  
  175. //display average of the marks
  176. Console.WriteLine($"\nThe average of the marks is {average:F1} percent.");
  177.  
  178.  
  179. }
  180. //***********************************************************************************
  181. //Method: static private double Average(int[] array)
  182. //Purpose: caculate the average of the integers in the array
  183. //Parameters: int[] array- randomnly generated integer array
  184. //Returns: double average- the calculated average of the array
  185. //***********************************************************************************
  186. static private void Fails(double[] arrayMarks, string[] arrayStrings)
  187. {
  188.  
  189. //display the list of failing students
  190. Console.WriteLine("\nHere is a list of failing students...\n");
  191. Console.WriteLine($"{"Name",15} {"Mark",-15}");
  192. Console.WriteLine($"{"----",15} {"---",-15}");
  193. for (int i = 0; i < arrayMarks.Length; ++i)
  194. {
  195. if (arrayMarks[i] <50.0)
  196. {
  197. Console.WriteLine($"{arrayStrings[i],15:F1} : {arrayMarks[i],-20:F1}");
  198. }
  199. }
  200.  
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement