Advertisement
wingman007

SortFIlterMinMaxC#

Oct 28th, 2020 (edited)
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.78 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SortFilterMinMax
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string[] cities = new string[3];
  10.             InputStringArray(cities);
  11.             Console.WriteLine("---------- I am sorting ------");
  12.             SortStringArray(cities);
  13.             OutpuStringArray(cities);
  14.             Console.WriteLine("------------- I am filtering ----------------");
  15.             OutpuStringArray(FilterStringArray(cities, 'P'));
  16.  
  17.             int[] intArray = new int[10];
  18.             Console.WriteLine("------------- I am entering the numbers-------------");
  19.             // InputIntArray(intArray);
  20.             SeedIntArray(intArray, 2, 100);
  21.             OutpuIntArray(intArray);
  22.             Console.Write("The max element is: ");
  23.             Console.WriteLine(MaxIntArray(intArray));
  24.             Console.WriteLine(MinIntArray(intArray));
  25.         }
  26.  
  27.         public static void InputStringArray(string[] array)
  28.         {
  29.             for (int i = 0; i < array.Length; i++)
  30.             {
  31.                 Console.Write("Please enter element[{0}]: ", i);
  32.                 array[i] = Console.ReadLine();
  33.             }
  34.         }
  35.  
  36.         public static void SortStringArray(string[] array)
  37.         {
  38.             string swap;
  39.             for (int i = 0; i < array.Length; i++)
  40.             {
  41.                 for (int j = i + 1; j < array.Length; j++)
  42.                 {
  43.                     if (String.Compare(array[i], array[j]) > 0)
  44.                     {
  45.                         swap = array[i];
  46.                         array[i] = array[j];
  47.                         array[j] = swap;
  48.                     }
  49.                 }
  50.             }
  51.         }
  52.  
  53.         public static void OutpuStringArray(string[] array)
  54.         {
  55.             for (int i = 0; i < array.Length; i++)
  56.             {
  57.                 Console.WriteLine(array[i]);
  58.             }
  59.         }
  60.  
  61.         public static string[] FilterStringArray(string[] array, char firstLetter) {
  62.             string[] filterdArray = new string[array.Length];
  63.             int counter = 0;
  64.             for (int i = 0; i < array.Length; i++)
  65.             {
  66.                 if (array[i][0] == firstLetter)
  67.                 {
  68.                     filterdArray[counter] = array[i];
  69.                     counter++;
  70.                 }
  71.             }
  72.             return filterdArray;
  73.         }
  74.  
  75.         public static void InputIntArray(int[] array)
  76.         {
  77.             for (int i = 0; i < array.Length; i++)
  78.             {
  79.                 Console.Write("Please enter element[{0}]: ", i);
  80.                 array[i] = int.Parse(Console.ReadLine());
  81.             }
  82.         }
  83.  
  84.         public static void OutpuIntArray(int[] array)
  85.         {
  86.             for (int i = 0; i < array.Length; i++)
  87.             {
  88.                 Console.WriteLine(array[i]);
  89.             }
  90.         }
  91.  
  92.         public static int MaxIntArray(int[] array)
  93.         {
  94.             int max = array[0];
  95.             for (int i = 0; i < array.Length; i++)
  96.             {
  97.                 if (max < array[i])
  98.                 {
  99.                     max = array[i];
  100.                 }
  101.             }
  102.             return max;
  103.         }
  104.  
  105.         public static int MinIntArray(int[] array)
  106.         {
  107.             int min = array[0];
  108.             for (int i = 0; i < array.Length; i++)
  109.             {
  110.                 if (min > array[i])
  111.                 {
  112.                     min = array[i];
  113.                 }
  114.             }
  115.             return min;
  116.         }
  117.  
  118.         public static void SeedIntArray(int[] array, int min, int max)
  119.         {
  120.             Random randomGen = new Random();
  121.             for (int i = 0; i < array.Length; i++)
  122.             {
  123.                 array[i] = randomGen.Next(min, max);
  124.             }
  125.         }
  126.     }
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement