Advertisement
balrougelive

Untitled

Oct 14th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. /*
  2. Summary: Arithmetic, variables and arrays.
  3.  
  4. Prompt the user for how many numbers they want to enter.
  5. Load the numbers into an array or list. (The numbers might be floating point.)
  6. Print the numbers entered back to the user, followed by the average, median and sum.
  7. Find the average value, the median value and the sum.
  8. The average is the sum divided by the count of the numbers, the median is the value that occurs in the middle.
  9. If we have an even count of numbers, such as 10, then the the median is the average of the two middle values.
  10. For greater detail about finding the median see MathIsFun (Links to an external site.)Links to an external site..
  11. Example Execution:
  12. How many numbers do you want to enter? 10
  13. Enter number 1: 22.1
  14. Enter number 2: 4
  15. Enter number 3: 41
  16. Enter number 4: 14
  17. Enter number 5: 24.2
  18. Enter number 6: 19
  19. Enter number 7: 25
  20. Enter number 8: 46
  21. Enter number 9: 79
  22. Enter number 10: 9
  23. You entered 22.1, 4, 41, 14, 24.2, 19, 25, 46, 79, 9.
  24. The average is 28.33.
  25. The median is 23.15.
  26. The sum is 283.3.
  27.  
  28. */
  29.  
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Text;
  34. using System.Threading.Tasks;
  35.  
  36. namespace Vars
  37. {
  38. class Program
  39. {
  40. static void Main(string[] args)
  41. {
  42. Console.WriteLine("How many numbers do you wish to enter, user?"); //Ask the user for how many numbers they want in their array
  43.  
  44. int userInput = Convert.ToInt32(Console.ReadLine()); //Convert the input stream to an int
  45.  
  46. Console.WriteLine("You entered\n" + userInput); //Print the number they entered
  47.  
  48. double[] userArray = new double[userInput]; //create a new array named userArray of double type and assign it to new double array of length userInput
  49.  
  50. for (int i = 0; i < userInput; i ++) //set up a for loop to store each input into the array from user input
  51. {
  52. userArray[i] = Convert.ToDouble(Console.ReadLine()); //iterate through the length of the array, as defined by user, and convert to doubles to be stored into the array
  53. }
  54.  
  55. for (int i = 0; i < userInput; i++) //Print the values of the array
  56. {
  57. Console.Write("\n{0} ", userArray[i]);
  58. }
  59.  
  60. double sum = 0;
  61. foreach (double i in userArray)
  62. {
  63. sum += i;
  64. }
  65. double average = sum / userArray.Length;
  66.  
  67. Console.Write("\nThe average is\n" + average);
  68.  
  69. //Find the Median
  70. double median = 0;
  71. Array.Sort(userArray);
  72.  
  73. Console.WriteLine("\nDEBUG ARRAY PRINT");
  74.  
  75. for (int i = 0; i < userInput; i++) //Print the values of the array
  76. {
  77. Console.Write("\n{0} ", userArray[i]);
  78. }
  79.  
  80. int mid = userArray.Length / 2;
  81.  
  82. if (userArray.Length % 2 == 0)
  83. {
  84. median = (userArray[mid] + userArray[mid - 1]) / 2.0;
  85. }
  86. else
  87. {
  88. median = userArray[mid];
  89. }
  90. Console.Write("\nThe Median is\n" + median);
  91.  
  92. //Find the sum
  93. double arraySum = userArray.Sum();
  94. Console.Write("\nThe sum of the array is\n" + arraySum);
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement