1.write a program to merge the arrays
2.write efficient code for extracting unique elements from a
sorted list of array?
Answer Posted / nishant chauhan
//#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int merge(int A[],int B[],int C[],int m,int n)
{
int i=0, j=0, k=0,len=0;
while (i < m && j < n)
{
if (A[i] <B[j])
{
C[k] = A[i];
i++;
len++;
}
else if(A[i] >B[j])
{
C[k] = B[j];
j++;
len++;
}
else
{
C[k]=A[i];
i++;j++;
len++;
}
k++;
}
if (i < m)
{
for (int p = i; p < m; p++)
{
C[k] = A[p];
k++;len++;
}
}
else
{
for (int p = j; p < n; p++)
{
C[k] = B[p];
k++; len++;
}
}
return len;
}
main ()
{
int a[20],b[20],c[30],m,n;
cout<<"enter the length of first array: ";
cin>>m;
cout<<"enter the array: ";
for(int i=0;i<m;i++)
cin>>a[i];
cout<<"enter the length of second array: ";
cin>>n;
cout<<"enter the array: ";
for(int j=0;j<n;j++)
cin>>b[j];
int length=merge(a,b,c,m,n);
cout<<"resulting merging array is: ";
for(int k=0;k<length;k++)
cout<<c[k]<<" ";
getch();
}
| Is This Answer Correct ? | 1 Yes | 1 No |
Post New Answer View All Answers
What is the most efficient way to count the number of bits which are set in an integer?
Write a programme using structure that create a record of students. The user allow to add a record and delete a record and also show the records in ascending order.
What is a 'null pointer assignment' error? Explain what are bus errors, memory faults, and core dumps?
Read the following data in two different files File A: aaaaaaaadddddddd bbbbbbbbeeeeeeee ccccccccffffffff File B: 11111111 22222222 33333333 By using the above files print the following output or write it in the Other file as follows aaaaaaaa11111111dddddddd bbbbbbbb22222222eeeeeeee cccccccc33333333ffffffffffff
Differentiate between functions getch() and getche().
Explain the red-black trees?
How are pointers declared in c?
What are variables and it what way is it different from constants?
Is main a keyword in c?
write a c program thal will find all sequences of length N that produce the sum is Zero, print all possible solutions?
Why can’t constant values be used to define an array’s initial size?
any "C" function by default returns an a) int value b) float value c) char value d) a & b
What is meant by operator precedence?
What is the collection of communication lines and routers called?
What are external variables in c?