Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ICA13
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. do
  15. {
  16. int size = 0; //input
  17. int[] array;
  18.  
  19. //reset
  20. Console.Clear();
  21.  
  22. //title
  23. Console.WriteLine("\t\t\tDale Chen-Song Assignment 13");
  24.  
  25. //get value for size of array
  26. GetValue("Enter the size of the array: ", 4, 10, out size);
  27.  
  28. //make arrays from user input
  29. array = MakeArray(size);
  30.  
  31. //display the contents of the integer array
  32. Show(array);
  33.  
  34. //display the contents of the integer array in reverse
  35. ShowReverse(array);
  36.  
  37. //display the average of the integer array
  38. Console.WriteLine($"\nThe average is {Average(array)}");
  39.  
  40. //display the largest value of the integer array
  41.  
  42. //display message asking user if they want to do it again
  43. Console.Write("Run again? y/n: ");
  44. //if user inputs yes, loop
  45. } while (Console.ReadLine().ToLower() == "y");
  46. }
  47. //***********************************************************************************
  48. //Method: static private int GetValue(string prompt, int min, int max)
  49. //Purpose: get integer value from user with error and range checking
  50. //Parameters: string prompt- prompt to display to the user
  51. // int min- the minimum value of range for value
  52. // int max- the maximum value of range for value
  53. // out int input- input out the valid integer
  54. //***********************************************************************************
  55. static private void GetValue(string prompt, int min, int max, out int input)
  56. {
  57. bool success = false; //success flag, true if success
  58.  
  59. //repeat until input is right
  60. do
  61. {
  62. //user input value for integer
  63. Console.Write(prompt);
  64. success = int.TryParse(Console.ReadLine(), out input);
  65.  
  66. //check if the value is valid
  67. if (!success)
  68. {
  69. //display error message if invalid number, prompt user to try again
  70. Console.WriteLine("An invalid number was entered, please try again.");
  71. }
  72. //check if the value is within the range
  73. else if (input < min || input > max)
  74. {
  75. //display error message if out of range, prompt user to try again
  76. Console.WriteLine("An out of range number was entered, please try again.");
  77. }
  78. }//continue looping until the input is correct
  79. while (!success || input < min || input > max);
  80. }
  81.  
  82. static int[] MakeArray(int input)
  83. {
  84. Random generator = new Random(); //random number generator
  85.  
  86. //create the array of integers to the specified size from user
  87. int[] integers = new int[input];
  88.  
  89. //randomly generate numbers into the integer array
  90. for (int i = 0; i < integers.Length; ++i)
  91. {
  92. integers[i] = generator.Next(0, 100);
  93. }
  94.  
  95. //return the integers to the main program
  96. return integers;
  97. }
  98. static private void Show(int[] array)
  99. {
  100. Console.WriteLine("\nThe array contents... ");
  101. //displays the contents of the integer array
  102. for (int i = 0; i < array.Length; ++i)
  103. {
  104. Console.WriteLine($"array[{i}] = {array[i]}");
  105. }
  106.  
  107. }
  108. static private void ShowReverse(int[] array)
  109. {
  110. Console.WriteLine("\nThe array in reverse...");
  111. // displays the contents of the integer array
  112. for (int i = array.Length-1; i > 0; --i)
  113. {
  114. Console.WriteLine($"array[{i}] = {array[i]}");
  115. }
  116. }
  117. static private double Average(int[] array)
  118. {
  119. double average = 0; //average of the values in the array
  120. int total = 0; //sum of the values in the array
  121.  
  122. //sum up the total value within the array
  123. foreach (int value in array)
  124. {
  125. total += value;
  126. }
  127.  
  128. //calculating the average of the array
  129. average = (double)total / array.Length;
  130.  
  131. //returns the average to the main console
  132. return average;
  133. }
  134. static private void Largest()
  135. {
  136.  
  137. }
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement