New Android apps UniqueKey

Saturday 7 March 2015

Merge 2 arrays and then sort this array.


/*a c program to sort the merg of array*/

#include<stdio.h>

main()
{
  float num1[100],num2[100],num[300];                              //array declaration
  int n1,n2,i,j,t;
  
  printf("\nenter the number of elements in 1st array\n");
  scanf("%d",&n1);                                                 //get the number of 1st array element

  printf("\nenter first array in ascending order\n");
  for(i=0;i<n1;i++)
  {
    scanf("%f",&num1[i]);                                          //to read the element of array 

  }
  
  printf("\nenter the number of elements in 2nd array\n");
  scanf("%d",&n2);                                                 //get the number of 2nd array element

  printf("\nenter second array in descending order\n");
  for(j=0;j<n2;j++)
  {
    scanf("%f",&num2[j]);                                          //to read the element of array
  }
  i=0;j=0;

  while(i<n1)
   {
     num[i]=num1[i];
     i++;
   }
 
  while(j<n2)
   {
      num[i]=num2[j];
     j++;
     i++;
   }

  for(i=0;i<n1+n2-1;i++)
   {
    for(j=0;j<n1+n2-i-1;j++)
    { 
      if(num[j]==num[j+1])
      j++;
      if(num[j]<num[j+1])
      {
        t=num[j];                                                  //use bubble sort to arrage array element
        num[j]=num[j+1];
        num[j+1]=t;
      }
    }
  }

  printf("\nthe sorted and merged array in decending order is :\n");
  for(i=0;i<n1+n2;i++)
  
  if(num[i]!=num[i+1])
  printf("%6.0f\n",num[i]);                                        //to print sorted and merg array in decending order
}

No comments:

Post a Comment