"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
Why c++ is better than c language?
What is function declaration in c++ with example?
What is the difference between global variables and local variable
How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array?how can you find the nodes with repetetive data in a linked list?
What is encapsulation in C++? Give an example.
how to access grid view row?
What is the type of 'this' pointer?
Describe the role of the c++ in the tradeoff of safety vs. Usability?
If a function doesn’t return a value, how do you declare the function?
What are the extraction and insertion operators in c++? Explain with examples.
How many characters are recognized by ANSI C++?
What is the use of map in c++?
How do you generate a random number in c++?
What is the default width for ouputting a long integer using the insertion operator?
State two differences between C and C++.