Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. namespace arraymarge
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.Title = "Array Merge By Comdet Phaudphut";
  10.             int[] arrayA = new int[15];
  11.             int[] arrayB = new int[20];
  12.             int[] arrayC = new int[35];
  13.             Console.WriteLine("===================================================");
  14.             Console.WriteLine("===       Array Merge By Comdet Phaudphut       ==="); Console.WriteLine("===================================================");
  15.             Console.WriteLine("\nInput number to ArrayA, separator by comma(,)");
  16.             arrayA = arrShort(Array.ConvertAll<string, int>(Console.ReadLine().Split(','), new Converter<string, int>(Convert.ToInt32)));
  17.             Console.Write("\nArrayA after short : "); arrayPrint(arrayA);
  18.             Console.WriteLine("\n\nInput number to ArrayB, separator by comma(,)");
  19.             arrayB = arrShort(Array.ConvertAll<string, int>(Console.ReadLine().Split(','), new Converter<string, int>(Convert.ToInt32)));
  20.             Console.Write("\nArrayB after short : "); arrayPrint(arrayB);
  21.             arrayC = arrShort(arrayMerge(arrayA, arrayB));
  22.             Console.Write("\n\n\nArrayC after merge and short : "); arrayPrint(arrayC);
  23.             Console.ReadLine();
  24.         }
  25.  
  26.         static int[] arrayMerge(int[] arrA, int[] arrB)//เรียงลำดับใน array จากน้อยไปมาก
  27.         {
  28.  
  29.             List<int> arrC = new List<int>();
  30.             int max = arrA.Length + arrB.Length;
  31.             for (int count = 0; count < max; count++)
  32.                 if (count < arrA.Length)
  33.                     arrC.Insert(count, arrA[count]);
  34.                 else
  35.                     arrC.Insert(count, arrB[count - arrA.Length]);
  36.             return arrC.ToArray();
  37.         }
  38.         static void arrayPrint(int[] arrTar)
  39.         {
  40.             foreach (int i in arrTar)
  41.                 Console.Write(i + " ");
  42.         }
  43.         static int[] arrShort(int[] arrTsh)
  44.         {
  45.             int counter = arrTsh.Length, tmp;
  46.             for (int i = 0; i < counter; i++)
  47.                 for (int j = i + 1; j < counter; j++)
  48.                     if (arrTsh[i] > arrTsh[j])
  49.                     { tmp = arrTsh[i]; arrTsh[i] = arrTsh[j]; arrTsh[j] = tmp; }
  50.             return arrTsh;
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement