Advertisement
stanevplamen

02.3.15.Set.VarOperations

Jun 1st, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Threading;
  4.  
  5. class SetVarOperations
  6. {
  7.     // creates an integer between 3 and 20
  8.     static int GiveInt()
  9.     {
  10.         Random rndInt = new Random();
  11.         Thread.Sleep(15); // wait to generate different number
  12.         int random = rndInt.Next(3, 20);
  13.         return random;
  14.     }
  15.  
  16.     static dynamic GiveDifferentType()
  17.     {
  18.         // creates a float / decimal /int / long / etc
  19.         // just change the "cast"
  20.         decimal randTempOne = (decimal)(GiveInt() * 2.0);
  21.         decimal randTempTwo = (decimal)Math.Sqrt(GiveInt());
  22.         return (decimal)(randTempOne * randTempTwo);
  23.     }
  24.  
  25.     // calculate the max, min , sum and product
  26.     static dynamic[] MaxMinSum(dynamic[] elements)
  27.     {
  28.         dynamic[] output = new dynamic[5]; // here will be copied the five results (0-min, 1-max, 2-sum , 3-product), 4-average
  29.         output[0] = -1000000; // in this example (enought for the generation limit)
  30.         output[1] = 1000000;
  31.         output[2] = 0;
  32.         output[3] = 1;
  33.         int i = 0;
  34.         for (i = 0; i < elements.Length; i++)
  35.         {
  36.             if (output[0] < elements[i])
  37.             {
  38.                 output[0] = elements[i];
  39.             }
  40.             if (output[1] > elements[i])
  41.             {
  42.                 output[1] = elements[i];
  43.             }
  44.             output[2] = output[2] + elements[i];
  45.             output[3] = output[3] * elements[i];
  46.         }
  47.         output[4] = output[2] / i;
  48.         return output;
  49.     }
  50.    
  51.     static void Main()
  52.     {
  53.         // declare a random length
  54.         int length = GiveInt();
  55.         // declare the set ot integers (please see the method)
  56.         dynamic[] setOfNumbers = new dynamic[length];
  57.  
  58.         for (int i = 0; i < setOfNumbers.Length; i++)
  59.         {
  60.             //Console.Write("Please enter the {0} member value: ", i);
  61.             setOfNumbers[i] = GiveDifferentType(); //int.Parse(Console.ReadLine());
  62.         }
  63.  
  64.         dynamic[] results = MaxMinSum(setOfNumbers);
  65.  
  66.         PrintSet(setOfNumbers);
  67.         PrintAnsw(results);
  68.     }
  69.     // printing the starting set
  70.     private static void PrintSet(dynamic[] setOfNumbers)
  71.     {
  72.         Console.Write("The given set is: ");
  73.         foreach (var c in setOfNumbers)
  74.         {
  75.             Console.Write("{0:0.000}  ", c);
  76.         }
  77.         Console.WriteLine();
  78.     }
  79.  
  80.     // printing the results
  81.     private static void PrintAnsw(dynamic[] results)
  82.     {
  83.         Console.WriteLine("The max value is: {0:0.000} , type {1}", results[0], TypeOf(results[0]));
  84.         Console.WriteLine("The min value is: {0:0.000} type {1}", results[1], TypeOf(results[1]));
  85.         Console.WriteLine("The average is: {0:0.000} type {1}", results[4], TypeOf(results[4]));
  86.         Console.WriteLine("The sum is: {0:0.000} type {1}", results[2], TypeOf(results[2]));
  87.         Console.WriteLine("The product is: {0:0.000} type {1}", results[3], TypeOf(results[3]));    
  88.     }
  89.  
  90.     // gets the type of the result
  91.     static Type TypeOf(dynamic variable)
  92.     {
  93.         Type type = variable.GetType();
  94.         return type;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement