1.write a program to merge the arrays
2.write efficient code for extracting unique elements from a
sorted list of array?

Answers were Sorted based on User's Feedback



1.write a program to merge the arrays 2.write efficient code for extracting unique elements from a ..

Answer / whyname

To merge to arrays ( Note the question has no mention of
sorting the array elements, hence the program below just
merges two arrays)

int array1[5] = {1,2,3,4,5};
int array2[7] = {6,7,8,9,10,11,12};
int i;
int merged[(sizeof(array1)+ sizeof(array2))/sizeof(int)];
memcpy( merged, array1, sizeof(array1));
memcpy( (merged+5), array2, sizeof(array2));

for(i=0;i<(sizeof(merged)/sizeof(int)); i++)
{
printf("%d\n",merged[i]);
}

Is This Answer Correct ?    20 Yes 15 No

1.write a program to merge the arrays 2.write efficient code for extracting unique elements from a ..

Answer / 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

1.write a program to merge the arrays 2.write efficient code for extracting unique elements from a ..

Answer / jaggu

#include<stdio.h>
#include<conio.h>
main()
{
int a[5]={1,2,3,4,5},b[4]={-6,-7,-8,-9},i,j=0;
for(i=5;i<=5+4-1;i++)
{

a[i]=b[j];
j++;
}
for(i=0;i<9;i++)
printf("a[%d]=%d \t",i,a[i]);

getch();
}

Is This Answer Correct ?    17 Yes 55 No

Post New Answer

More C Interview Questions

main() { char ch='356'; Printf("%d",ch); } *OUTPUT*:- -18 *Why?*

1 Answers  


Given an array of 1s and 0s arrange the 1s together and 0s together in a single scan of the array. Optimize the boundary conditions?

0 Answers   Infosys,


how to find out the union of two character arrays?

2 Answers  


how 2 compile & execute c program with out using editor?

2 Answers   HP,


main() { enum _tag{ left=10, right, front=100, back}; printf("%d, %d, %d, %d", left, right, front, back); }

1 Answers   Accenture, Vector,






can any one please explain, how can i access hard disk(physical address)? it is possible by the use of far,near or huge pointer? if yes then please explain......

0 Answers  


C passes By value or By reference?

5 Answers   Geometric Software, Infosys,


Which one to choose from 'initialization lists' or 'assignment', for the use in the constructor?

0 Answers  


c program for searching a student details among 10 student details

0 Answers  


If I have a char * variable pointing to the name of a function ..

0 Answers  


Can a function argument have default value?

0 Answers   Genpact,


Why c is known as a mother language?

0 Answers  


Categories