Program to merge two arrays in sorted order | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Monday, February 10, 2020

Program to merge two arrays in sorted order | My CS Tutorial

C program to merge two arrays in sorted order.
In this tutorial we will solve a problem to merge two arrays or two sorted arrays.

Here we will write a c program source code to merge array elements in sorted order.

This program will accept the range or size of two array that entered by user through the keyboard.
After it we will enter the elements of both array.

After these steps compiler check and execute the conditions and print a new array elements list in which both array elements are available and print another list in which all elements are in sorted order.

C PROGRAM CODE 


#include<stdio.h>
#include<conio.h>
void main()
{
 int i,size1,size2,j=0,temp;
  int a[50],b[50],c[50+50];
 clrscr();

 printf("\n ENTER THE SIZE OF FIRST ARRAY:\n ");
 scanf("%d",&size1);

 printf("\n ENTER THE SIZE OF SECOND ARRAY:\n ");
 scanf("%d",&size2);


 printf("\n ENTER ELEMENTS OF FIRST ARRAY:\n ");
 for(i=0;i<size1;i++)
 {
  scanf("%d",&a[i]);

 }

 printf("\n ENTER ELEMENTS OF SECOND ARRAY:\n ");
 for(i=0;i<size2;i++)
 {
  scanf("%d",&b[i]);
 }

 //merge these arrays

 for(i=0;i<size1;i++)
 {
  c[j]=a[i];
  j++;
 }
 for(i=0;i<size2;i++)
 {
  c[j]=b[i];
  j++;
 }

 printf("\n AFTER MERGING ARRAY IS:\n ");
 for(i=0;i<size1+size2;i++)
 {
  printf("%d\n ",c[i]);
 }

 //Sorting the merge array

 for(i=0;i<size1+size2;i++)
    {
 for(j=i+1;j<size1+size2;j++)
 {
  if(c[i]>c[j])
  {
   temp=c[i];
   c[i]=c[j];
   c[j]=temp;
  }
 }
}

 printf("\n AFTER SIRTING MERGED ARRAY IS:\n ");
  for(i=0;i<size1+size2;i++)
 {
  printf("%d\n ",c[i]);
 }
 getch();
}


OUTPUT:





Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment