Advertisement
n4wn4w

C# [HOMEWORK] - METODS

May 11th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.06 KB | None | 0 0
  1. Problem 1.  Bigger Number  ////////////////////////////////////////////////////////////////////////
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace BiggerNumber_1
  10. {
  11.     class BiggerNumber_1
  12.     {
  13.         static int GetMax(int a, int b)
  14.         {
  15.             int c = Math.Max(a, b);
  16.             return c;
  17.         }
  18.         static void Main(string[] args)
  19.         {
  20.             int first = int.Parse(Console.ReadLine());
  21.             int second = int.Parse(Console.ReadLine());
  22.  
  23.             int max = GetMax(first, second);
  24.             Console.WriteLine(max);
  25.         }
  26.     }
  27. }
  28.  
  29.  
  30. Problem 2.  Last Digit of Number   //////////////////////////////////////////////////////////////
  31.  
  32.  
  33. using System;
  34. using System.Collections.Generic;
  35. using System.Linq;
  36. using System.Text;
  37. using System.Threading.Tasks;
  38.  
  39. namespace LastDigit_2
  40. {
  41.     class LastDigit_2
  42.     {
  43.         static string[] number = { "zero", "one", "two", "three", "four", "five",
  44.                               "six", "seven", "eight", "nine"};
  45.  
  46.         static void PrintNumber(int num)
  47.         {
  48.             int lastDigit = num % 10;
  49.             Console.WriteLine(number[lastDigit]);
  50.         }
  51.         static void Main(string[] args)
  52.         {
  53.             int a = int.Parse(Console.ReadLine());
  54.  
  55.             PrintNumber(a);
  56.         }
  57.     }
  58. }
  59.  
  60. Problem 3.  Larger Than Neighbours   /////////////////////////////////////////////////////////////////////////////
  61.  
  62.  
  63. using System;
  64. using System.Collections.Generic;
  65. using System.Linq;
  66. using System.Text;
  67. using System.Threading.Tasks;
  68.  
  69. namespace LargerNeighbours_3
  70. {
  71.     class LargerNeighbours_3
  72.     {
  73.         static int[] arr;
  74.  
  75.         static bool CheckIfGreater(int idx)
  76.         {
  77.             bool isGreater;
  78.             if (idx == 0)
  79.             {
  80.                 isGreater = arr[idx] > arr[idx + 1];
  81.             }
  82.             else if (idx == arr.Length - 1)
  83.             {
  84.                 isGreater = arr[idx] > arr[idx - 1];
  85.             }
  86.             else
  87.             {
  88.                 isGreater = arr[idx] > arr[idx - 1] && arr[idx] > arr[idx + 1];
  89.             }
  90.             return isGreater;
  91.         }
  92.         static void Main(string[] args)
  93.         {
  94.             string inputArray = Console.ReadLine();
  95.             arr = inputArray.Split().Select(int.Parse).ToArray();
  96.  
  97.             for (int i = 0; i < arr.Length; i++)
  98.             {
  99.                 Console.WriteLine(CheckIfGreater(i));
  100.             }
  101.         }
  102.     }
  103. }
  104.  
  105.  
  106. Problem 4.  First Larger Than Neighbours   //////////////////////////////////////////////////////////////////
  107.  
  108.  
  109. using System;
  110. using System.Collections.Generic;
  111. using System.Linq;
  112. using System.Text;
  113. using System.Threading.Tasks;
  114.  
  115. namespace FirstLarger_4
  116. {
  117.     class FirstLarger_4
  118.     {
  119.         static int[] arr;
  120.  
  121.         static bool GetFirstLarger(int idx)
  122.         {
  123.             bool firstLarger;
  124.             if (idx == 0)
  125.             {
  126.                 firstLarger = arr[idx] > arr[idx + 1];
  127.             }
  128.             else if (idx == arr.Length - 1)
  129.             {
  130.                 firstLarger = arr[idx] > arr[idx - 1];
  131.             }
  132.             else
  133.             {
  134.                 firstLarger = arr[idx] > arr[idx - 1] && arr[idx] > arr[idx + 1];
  135.             }
  136.             return firstLarger;
  137.         }
  138.         static void Main(string[] args)
  139.         {
  140.             string inputArray = Console.ReadLine();
  141.             arr = inputArray.Split().Select(int.Parse).ToArray();
  142.             bool isGreater = false;
  143.  
  144.             for (int i = 0; i < arr.Length; i++)
  145.             {
  146.                 if (GetFirstLarger(i))
  147.                 {
  148.                     Console.WriteLine("First Larger element is {0}", i);
  149.                     isGreater = true;
  150.                     break;
  151.                 }
  152.             }
  153.             if (!isGreater)
  154.             {
  155.                 Console.WriteLine("-1");
  156.             }
  157.         }
  158.     }
  159. }
  160.  
  161. Problem 5.  Reverse Number    ///////////////////////////////////////////////////
  162.  
  163. using System;
  164. using System.Collections.Generic;
  165. using System.Linq;
  166. using System.Text;
  167. using System.Threading.Tasks;
  168.  
  169. namespace ReverseNum_5
  170. {
  171.     class ReverseNum_5
  172.     {
  173.         static double ReverseNumber(double num)
  174.         {
  175.             string number = num.ToString();
  176.             char[] reverse = number.ToCharArray();
  177.             Array.Reverse(reverse);
  178.             string revNum = new string(reverse);
  179.             double newNum = double.Parse(revNum);
  180.  
  181.             return newNum;
  182.         }
  183.         static void Main(string[] args)
  184.         {
  185.             double number = double.Parse(Console.ReadLine());
  186.  
  187.             Console.WriteLine(ReverseNumber(number));
  188.         }
  189.     }
  190. }
  191.  
  192.  
  193. Problem 6.  Number Calculations   ///////////////////////////////////////////////////////////////////////////
  194.  
  195.  
  196. using System;
  197. using System.Collections.Generic;
  198. using System.Linq;
  199. using System.Text;
  200. using System.Threading.Tasks;
  201.  
  202. namespace _06.NumberCalculations
  203. {
  204.     class NumberCalculations
  205.     {
  206.         static void Main(string[] args)
  207.         {
  208.             decimal[] decimals = Console.ReadLine().Split(' ').Select(decimal.Parse).ToArray();
  209.             double[] doubles = Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
  210.  
  211.             Console.WriteLine(FindMinValue(decimals));
  212.             Console.WriteLine(FindMinValue(doubles));
  213.             Console.WriteLine(FindMaxValue(decimals));
  214.             Console.WriteLine(FindMaxValue(doubles));
  215.             Console.WriteLine(FindAvgValue(decimals));
  216.             Console.WriteLine(FindAvgValue(doubles));
  217.             Console.WriteLine(FindSum(decimals));
  218.             Console.WriteLine(FindSum(doubles));
  219.             Console.WriteLine(FindProduct(decimals));
  220.             Console.WriteLine(FindProduct(doubles));
  221.         }
  222.  
  223.         static decimal FindMinValue(decimal[] numbers)
  224.         {
  225.             decimal result = Decimal.MaxValue;
  226.  
  227.             foreach (decimal n in numbers)
  228.             {
  229.                 result = (n < result) ? n : result;
  230.             }
  231.             return result;
  232.         }
  233.  
  234.         static double FindMinValue(double[] numbers)
  235.         {
  236.             double result = Double.MaxValue;
  237.  
  238.             foreach (double n in numbers)
  239.             {
  240.                 result = (n < result) ? n : result;
  241.             }
  242.             return result;
  243.         }
  244.  
  245.         static decimal FindMaxValue(decimal[] numbers)
  246.         {
  247.             decimal result = Decimal.MinValue;
  248.  
  249.             foreach (decimal n in numbers)
  250.             {
  251.                 result = (n > result) ? n : result;
  252.             }
  253.             return result;
  254.         }
  255.  
  256.         static double FindMaxValue(double[] numbers)
  257.         {
  258.             double result = Double.MinValue;
  259.  
  260.             foreach (double n in numbers)
  261.             {
  262.                 result = (n > result) ? n : result;
  263.             }
  264.             return result;
  265.         }
  266.  
  267.         static decimal FindAvgValue(decimal[] numbers)
  268.         {
  269.             decimal result = 0;
  270.  
  271.             foreach (decimal n in numbers)
  272.             {
  273.                 result += n;
  274.             }
  275.             result /= numbers.Length;
  276.             return result;
  277.         }
  278.  
  279.         static double FindAvgValue(double[] numbers)
  280.         {
  281.             double result = 0;
  282.  
  283.             foreach (double n in numbers)
  284.             {
  285.                 result += n;
  286.             }
  287.             result /= numbers.Length;
  288.             return result;
  289.         }
  290.  
  291.         static decimal FindSum(decimal[] numbers)
  292.         {
  293.             decimal result = 0;
  294.  
  295.             foreach (decimal n in numbers)
  296.             {
  297.                 result += n;
  298.             }
  299.             return result;
  300.         }
  301.  
  302.         static double FindSum(double[] numbers)
  303.         {
  304.             double result = 0;
  305.  
  306.             foreach (double n in numbers)
  307.             {
  308.                 result += n;
  309.             }
  310.             return result;
  311.         }
  312.  
  313.         static decimal FindProduct(decimal[] numbers)
  314.         {
  315.             decimal result = 1;
  316.  
  317.             foreach (decimal n in numbers)
  318.             {
  319.                 result *= n;
  320.             }
  321.             return result;
  322.         }
  323.  
  324.         static double FindProduct(double[] numbers)
  325.         {
  326.             double result = 1;
  327.  
  328.             foreach (double n in numbers)
  329.             {
  330.                 result *= n;
  331.             }
  332.             return result;
  333.         }
  334.     }
  335. }
  336.  
  337.  
  338. Problem 7.  * Generic Array Sort    ///////////////////////////////////////////////////////////////////////////////////
  339.  
  340.  
  341. using System;
  342. using System.Collections.Generic;
  343. using System.Linq;
  344. using System.Text;
  345. using System.Threading.Tasks;
  346.  
  347. namespace GenericArraySort_7
  348. {
  349.     class GenericArraySort_7
  350.     {
  351.         static void SortArray<T>(List<T> list) where T : IComparable
  352.         {
  353.             for (int a = 0; a < list.Count - 1; a++)
  354.             {
  355.                 int minValue = a;
  356.                 for (int b = a + 1; b < list.Count; b++)
  357.                 {
  358.                     if (list[minValue].CompareTo(list[b]) > 0)
  359.                     {
  360.                         minValue = b;
  361.                     }
  362.                 }
  363.                 T temp = list[a];
  364.                 list[a] = list[minValue];
  365.                 list[minValue] = temp;
  366.             }
  367.             foreach (var l in list)
  368.             {
  369.                 Console.Write(l + " ");
  370.             }
  371.         }
  372.         static void Main(string[] args)
  373.         {
  374.             List<int> number = new List<int> { 3, 2, 16, 24, 5, 1, 27, 80, 9 };
  375.             List<string> words = new List<string> { "am", "em", "omo", "zaz", "cba", "baa" };
  376.             List<DateTime> dates = new List<DateTime> { new DateTime(2012, 02, 01),
  377.                                                         new DateTime(2015, 05, 06), new DateTime(1875, 03, 03) };
  378.  
  379.             SortArray(number);
  380.             Console.WriteLine();
  381.             SortArray(words);
  382.             Console.WriteLine();
  383.             SortArray(dates);
  384.             Console.WriteLine();
  385.         }
  386.     }
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement