Program for merged two arrays and display it in ascending order

Hi,This program is for merging two arrays and display those values in order.
Hope it will helpful for you.

class Program
    {
        static void Main(string[] args)
        {
            int[] array_one = new int[] { };
            int[] array_two = new int[] { };
            int[] merged_array = new int[] { };
           
            int i, j;
            int Array_One_Total, Array_Two_Total;
            Console.Write("Enter total no of element for array_one:-");
            Array_One_Total = Convert.ToInt32(Console.ReadLine());
            array_one = new int[Array_One_Total];
            //Insert into first array
            for (i = 0; i <= array_one.Length - 1; i++)
            {
                Console.Write("Array one element {0} - ", i);
                array_one[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("-----------");
            Console.Write("Enter total no pf element for array_two:-");
            Array_Two_Total = Convert.ToInt32(Console.ReadLine());
            array_two = new int[Array_Two_Total];
            //Insert into second array
            for (j = 0; j <= array_two.Length - 1; j++)
            {
                Console.Write("Array two element {0} - ", j);
                array_two[j] = Convert.ToInt32(Console.ReadLine());
            }
            i = 0; j = 0;
            //Declare merged array
            merged_array = new int[Array_One_Total + Array_Two_Total];
            //Set values in merged array

          
            for (i = 0; i < array_one.Count(); i++)
            {
                merged_array[i] = array_one[i];
            }
            for (j = 0; j < array_two.Count(); j++)
            {
                merged_array[i] = array_two[j];
                i++;
            }
           
            //Display merged array
            Console.WriteLine("Merged array");
            Array.Sort(merged_array); //Sort array to display it in ascending order
            for (int k = 0; k < merged_array.Length; k++)
            {
                Console.WriteLine("{0}", merged_array[k]);
            }
            Console.ReadLine();
        }
    }

   For ex input: Enter total no of element for array_one:-3
Array one element 0 - 3
Array one element 1 - 2
Array one element 2 - 1
-----------
Enter total no pf element for array_two:-3
Array two element 0 - 6
Array two element 1 - 5
Array two element 2 - 4

Output:
Merged array
1
2
3
4
5
6

No comments:

Post a Comment

Please let me know if you have any feedback or doubts.

Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...