C Program To Merge Two Array and Sort Array Element

C Program To Merge Two Array and Sort Array Element

#include<stdio.h>

main()
{

 int arr1[5];       //{1,2,3,4,5}; you also intialize array elements
 int arr2[5];       //{23,34,6,9,8};
 int arr3[10];
 int i,j,k=0,temp;

 //array1
printf("Enter array1 Elements:\n");    //print msg to enter array elements

  for(i=0;i<5;i++)
  {

   scanf("%d",&arr1[i]);    //user give inputs to array1

  }

//array2
printf("Enter array2 Elements:\n");    //print msg to enter array elements

  for(i=0;i<5;i++)
  {

   scanf("%d",&arr2[i]);    //user give inputs to array2

  }

printf("\n");

//array3
 for(i=0;i<10;i++)
  {
  if(i>4)
   {
    arr3[i]=arr2[k];         //To merge array1 and array2 and i=5 to i=9 arr2 element in arr3
    k++;
   }
    arr3[i]=arr1[i];         //i=0 to 4  arr1 element into arr3
   }

for(i=0;i<10;i++)
  {

   printf("Elements after Merge:%d\n",arr3[i]);       //Array3 element after merging arr1 and arr2

  }

printf("\n");

 for(i=0;i<9;i++)     //loop i<9 bcuz second last element compare to last element(last element compare itself then)
  {
   for(j=i+1;j<10;j++)
   {
    if(arr3[i]>arr3[j])    //if condition true then swap
     {
      temp=arr3[i];
      arr3[i]=arr3[j];      //sorting array in asending order
      arr3[j]=temp;
      }
    }
   }

 for(i=0;i<10;i++)
  {

      printf("Elements after sorted: %d\n",arr3[i]);    //Array after sorting

   }
}


Related Posts