"How will you merge these two arrays? Write the program
Array: A 1 18 22 43
Array: B 3 4 6 20 34 46 55
Output Array: C 1 3 4 6 18 20 22 34 43 46 55"
Answer Posted / sandeep
#include<stdio.h>
#include<conio.h">
#define max 10
void main()
{
int a[max],b[max],c[2*max],i,j,k,n,m;
clrscr();
printf("size of array a:");
scanf("%d",&n);
printf("enter a elements: \n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
printf("size of array b:");
scanf("%d",&m);
printf("enter a elements: \n");
for(i=0;i<m;i++)
scanf("%d", &b[i]);
for(i=0,j=0,k=0; i<n && j<m; k++)
if(a[i]<b[j])
c[k] = a[i++];
else
c[k]=b[j++];
while(i<n)
c[k++] = a[i++];
while(j<m)
c[k++] = b[j++];
for(i=0;i<m+n-1;i++) //place each element in to correct position
{
for(j=i+1;j<m+n;j++)
{
if(c[i]>c[j]) //swapping
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
printf("\n array c:\n");
for(i=0;i<(m+n); i++)
printf("%d", c[i]);
getch();
}
Is This Answer Correct ? | 10 Yes | 7 No |
Post New Answer View All Answers
Name the implicit member functions of a class.
Differentiate between declaration and definition.
Where can I run c++ program?
What is ifstream c++?
What is the best it certification?
Which bit wise operator is suitable for putting on a particular bit in a number?
How can you prevent accessing of the private parts of my class by other programmers (violating encapsulation)?
Write about the local class and mention its use?
How to declare a function pointer?
Eplain extern keyword?
What is meant by entry controlled loop? What all C++ loops are exit controlled?
When do we run a shell in the unix system? How will you tell which shell you are running?
Draw a flow chart and write a program for the difference between the sum of elements with odd and even numbers. Two dimensional array.
What are the differences between malloc() and calloc()?
Do you know what are the new features that iso/ansi c++ has added to original c++ specifications?