Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Summary: Arithmetic, variables and arrays.
- Prompt the user for how many numbers they want to enter.
- Load the numbers into an array or list. (The numbers might be floating point.)
- Print the numbers entered back to the user, followed by the average, median and sum.
- Find the average value, the median value and the sum.
- The average is the sum divided by the count of the numbers, the median is the value that occurs in the middle.
- If we have an even count of numbers, such as 10, then the the median is the average of the two middle values.
- For greater detail about finding the median see MathIsFun (Links to an external site.)Links to an external site..
- Example Execution:
- How many numbers do you want to enter? 10
- Enter number 1: 22.1
- Enter number 2: 4
- Enter number 3: 41
- Enter number 4: 14
- Enter number 5: 24.2
- Enter number 6: 19
- Enter number 7: 25
- Enter number 8: 46
- Enter number 9: 79
- Enter number 10: 9
- You entered 22.1, 4, 41, 14, 24.2, 19, 25, 46, 79, 9.
- The average is 28.33.
- The median is 23.15.
- The sum is 283.3.
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Vars
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("How many numbers do you wish to enter, user?"); //Ask the user for how many numbers they want in their array
- int userInput = Convert.ToInt32(Console.ReadLine()); //Convert the input stream to an int
- Console.WriteLine("You entered\n" + userInput); //Print the number they entered
- double[] userArray = new double[userInput]; //create a new array named userArray of double type and assign it to new double array of length userInput
- for (int i = 0; i < userInput; i ++) //set up a for loop to store each input into the array from user input
- {
- 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
- }
- for (int i = 0; i < userInput; i++) //Print the values of the array
- {
- Console.Write("\n{0} ", userArray[i]);
- }
- double sum = 0;
- foreach (double i in userArray)
- {
- sum += i;
- }
- double average = sum / userArray.Length;
- Console.Write("\nThe average is\n" + average);
- //Find the Median
- double median = 0;
- Array.Sort(userArray);
- Console.WriteLine("\nDEBUG ARRAY PRINT");
- for (int i = 0; i < userInput; i++) //Print the values of the array
- {
- Console.Write("\n{0} ", userArray[i]);
- }
- int mid = userArray.Length / 2;
- if (userArray.Length % 2 == 0)
- {
- median = (userArray[mid] + userArray[mid - 1]) / 2.0;
- }
- else
- {
- median = userArray[mid];
- }
- Console.Write("\nThe Median is\n" + median);
- //Find the sum
- double arraySum = userArray.Sum();
- Console.Write("\nThe sum of the array is\n" + arraySum);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement