Advertisement
stak441

Methods - 15 - OperationsOnVariableTypes

Jan 21st, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _15.OperationsOnVariableTypes
  7. {
  8.     class Program
  9.     {
  10.         static T GetMinimum<T>(params T[] array)
  11.         {
  12.             dynamic min = array[0];
  13.  
  14.             for (int i = 0; i < array.Length; i++)
  15.             {
  16.                 if (array[i] < min)
  17.                 {
  18.                     min = array[i];
  19.                 }
  20.             }
  21.  
  22.             return min;
  23.         }
  24.  
  25.         static T GetMaximum<T>(params T[] array)
  26.         {
  27.             dynamic max = array[0];
  28.  
  29.             for (int i = 0; i < array.Length; i++)
  30.             {
  31.                 if (array[i] > max)
  32.                 {
  33.                     max = array[i];
  34.                 }
  35.             }
  36.  
  37.             return max;
  38.         }
  39.  
  40.         static T GetAverage<T>(params T[] array)
  41.         {
  42.             dynamic sum = 0;
  43.             for (int i = 0; i < array.Length; i++)
  44.             {
  45.                 sum += array[i];
  46.             }
  47.  
  48.             return sum / array.Length;
  49.         }
  50.  
  51.         static T GetSum<T>(params T[] array)
  52.         {
  53.             dynamic sum = 0;
  54.             for (int i = 0; i < array.Length; i++)
  55.             {
  56.                 sum += array[i];
  57.             }
  58.  
  59.             return sum;
  60.         }
  61.  
  62.         static T GetProduct<T>(params T[] array)
  63.         {
  64.             dynamic prod = 1;
  65.             for (int i = 0; i < array.Length; i++)
  66.             {
  67.                 prod *= array[i];
  68.             }
  69.  
  70.             return prod;
  71.         }
  72.  
  73.         static void Main(string[] args)
  74.         {
  75.             int[] array = { 1, 5, 8, -2, 6, -3 };    //Used for testing only, all methods can receive variable number of params
  76.  
  77.             dynamic min = GetMinimum(-2m, -4.6m, 1.1m);
  78.             Console.WriteLine("Minimal number: {0}", min);
  79.  
  80.             dynamic max = GetMaximum(-2, -4.6, 1.1);
  81.             Console.WriteLine("Max number: {0}", max);
  82.  
  83.             dynamic average = GetAverage(-2, -4.6, 1.1);
  84.             Console.WriteLine("Average: {0}", average);
  85.  
  86.             dynamic sum = GetSum(-2, -4.6, 1.1);
  87.             Console.WriteLine("Sum: {0}", sum);
  88.  
  89.             dynamic product = GetProduct(-2, -4.6, 1.1);
  90.             Console.WriteLine("Product: {0}", product);
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement