Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write methods to calculate the minimum, maximum, average, sum and product of a given set of numbers. Overload the methods to work with numbers of type double and decimal. */
- namespace _06.NumberCalculations
- {
- using System;
- using System.Globalization;
- using System.Linq;
- using System.Threading;
- class NumberCalculations
- {
- static void Main(string[] args)
- {
- Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
- int arrayType = GetTypeOfNumbersInArray();
- Console.Write("Enter your sequence of ");
- switch (arrayType)
- {
- case 1:
- Console.WriteLine("Integer numbers on single line, separated by a space: ");
- int[] arrInts = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- Console.WriteLine("The Smallest number in array is: {0}", GetMinNumberOfArray(arrInts));
- Console.WriteLine("The Greater number in array is: {0}", GetMaxNumberOfArray(arrInts));
- Console.WriteLine("The Average of numbers in array is: {0:F3}", GetAverageOfArrayNumbers(arrInts));
- Console.WriteLine("The Sum of numbers in array is: {0}", GetSumOfArrayNumbers(arrInts));
- Console.WriteLine("The Product of numbers in array is: {0}", GetProductOfArrayNumbers(arrInts));
- break;
- case 2:
- Console.WriteLine("Floating-point numbers on single line, separated by a space: ");
- double[] arrDoubles = Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
- Console.WriteLine("The Smallest number in array is: {0}", GetMinNumberOfArray(arrDoubles));
- Console.WriteLine("The Greater number in array is: {0}", GetMaxNumberOfArray(arrDoubles));
- Console.WriteLine("The Average of numbers in array is: {0:F3}", GetAverageOfArrayNumbers(arrDoubles));
- Console.WriteLine("The Sum of numbers in array is: {0}", GetSumOfArrayNumbers(arrDoubles));
- Console.WriteLine("The Product of numbers in array is: {0:F3}", GetProductOfArrayNumbers(arrDoubles));
- break;
- case 3:
- Console.WriteLine("Decimal numbers on single line, separated by a space: ");
- decimal[] arrDecimals = Console.ReadLine().Split(' ').Select(decimal.Parse).ToArray();
- Console.WriteLine("The Smallest number in array is: {0}", GetMinNumberOfArray(arrDecimals));
- Console.WriteLine("The Greater number in array is: {0}", GetMaxNumberOfArray(arrDecimals));
- Console.WriteLine("The Average of numbers in array is: {0:F3}", GetAverageOfArrayNumbers(arrDecimals));
- Console.WriteLine("The Sum of numbers in array is: {0}", GetSumOfArrayNumbers(arrDecimals));
- Console.WriteLine("The Product of numbers in array is: {0:F3}", GetProductOfArrayNumbers(arrDecimals));
- break;
- }
- }
- private static int GetTypeOfNumbersInArray()
- {
- Console.WriteLine(@"Please choose a type of numbers in your array:
- 1 --> int
- 2 --> double
- 3 --> decimal
- ");
- int numChoice;
- do
- {
- Console.Write("Enter the number of your choice: ");
- } while (!int.TryParse(Console.ReadLine(), out numChoice) || numChoice < 1 || numChoice > 3);
- return numChoice;
- }
- private static int GetMinNumberOfArray(int[] ints)
- {
- int minNum = ints[0];
- foreach (int number in ints)
- {
- if (number < minNum)
- {
- minNum = number;
- }
- }
- return minNum;
- }
- private static double GetMinNumberOfArray(double[] doubles)
- {
- double minNum = doubles[0];
- foreach (double number in doubles)
- {
- if (number < minNum)
- {
- minNum = number;
- }
- }
- return minNum;
- }
- private static decimal GetMinNumberOfArray(decimal[] decimals)
- {
- decimal minNum = decimals[0];
- foreach (decimal number in decimals)
- {
- if (number < minNum)
- {
- minNum = number;
- }
- }
- return minNum;
- }
- private static int GetMaxNumberOfArray(int[] ints)
- {
- int maxNum = ints[0];
- foreach (int number in ints)
- {
- if (number > maxNum)
- {
- maxNum = number;
- }
- }
- return maxNum;
- }
- private static double GetMaxNumberOfArray(double[] doubles)
- {
- double maxNum = doubles[0];
- foreach (double number in doubles)
- {
- if (number > maxNum)
- {
- maxNum = number;
- }
- }
- return maxNum;
- }
- private static decimal GetMaxNumberOfArray(decimal[] decimals)
- {
- decimal maxNum = decimals[0];
- foreach (decimal number in decimals)
- {
- if (number > maxNum)
- {
- maxNum = number;
- }
- }
- return maxNum;
- }
- private static long GetSumOfArrayNumbers(int[] ints)
- {
- long sum = 0;
- foreach (int number in ints)
- {
- sum += number;
- }
- return sum;
- }
- private static double GetSumOfArrayNumbers(double[] doubles)
- {
- double sum = 0;
- foreach (double number in doubles)
- {
- sum += number;
- }
- return sum;
- }
- private static decimal GetSumOfArrayNumbers(decimal[] decimals)
- {
- decimal sum = 0;
- foreach (decimal number in decimals)
- {
- sum += number;
- }
- return sum;
- }
- private static double GetAverageOfArrayNumbers(int[] ints)
- {
- return GetSumOfArrayNumbers(ints) / (double)ints.Length;
- }
- private static double GetAverageOfArrayNumbers(double[] doubles)
- {
- return GetSumOfArrayNumbers(doubles) / doubles.Length;
- }
- private static decimal GetAverageOfArrayNumbers(decimal[] decimals)
- {
- return GetSumOfArrayNumbers(decimals) / decimals.Length;
- }
- private static long GetProductOfArrayNumbers(int[] ints)
- {
- long product = 1;
- foreach (int number in ints)
- {
- product *= number;
- }
- return product;
- }
- private static double GetProductOfArrayNumbers(double[] doubles)
- {
- double product = 1;
- foreach (double number in doubles)
- {
- product *= number;
- }
- return product;
- }
- private static decimal GetProductOfArrayNumbers(decimal[] decimals)
- {
- decimal product = 1;
- foreach (decimal number in decimals)
- {
- product *= number;
- }
- return product;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement