How do I merge two arrays having different values into one array?

匿名 (未验证) 提交于 2019-12-03 01:08:02

问题:

Suppose you have one array a[]=1,2,4,6 and a second array b[]=3,5,7. The merged result should have all the values, i.e. c[]=1,2,3,4,5,6,7. The merge should be done without using functions from .

回答1:

I haven't compiled and tested the following code, but I am reasonably confident. I am assuming both input arrays are already sorted. There is more work to do to make this general purpose as opposed to a solution for this example only. No doubt the two phases I identify could be combined, but perhaps that would be harder to read and verify;

void merge_example() {     int a[] = {1,2,4,6};     int b[] = {3,5,7};     int c[100];     // fixme - production code would need a robust way                     //  to ensure c[] always big enough     int nbr_a = sizeof(a)/sizeof(a[0]);     int nbr_b = sizeof(b)/sizeof(b[0]);     int i=0, j=0, k=0;      // Phase 1) 2 input arrays not exhausted     while( i


回答2:

I am learning c myself at them moment, so don't take this as the perfect solution, but maybe you can get some ideas from what I did to solve your own problem.

#include  #include   int compare (const void * first, const void * second){     return  *(int*)first - *(int*)second ; }  int main(){     int a[] = {1,2,4,6};     int b[] = {3,5,7};     size_t sizeA =sizeof(a)/sizeof(a[0]);     size_t sizeB = sizeof(b)/sizeof(b[0]);      size_t sizeC = sizeA + sizeB;      /*allocate new array of sufficient size*/     int *c = malloc(sizeof(int)*sizeC);     unsigned i;     /*copy elements from a into c*/     for(i = 0; i


回答3:

In case the 2 given arrays are sorted:

while (true): {     if (a[i] 

i,j,k are indices and start at zero. Mind you, this code does not check for array lengths. Also you will need to break it when you reach end of both arrays. But is easy to deal with.

If the arrays are not pre-sorted, you can just easily concatenate them and call a search function on them such as BubbleSort or QuickSort. Google those.



回答4:

void merge(int *input1, size_t sz1,             int *input2, size_t sz2,            int *output, size_t sz3) {     int i = 0;     int index1 = 0, index2 = 0;      while (i 

which you use that way:

#define TAB_SIZE(x) (sizeof(x)/sizeof(*(x)))  int tab1[] = { 1, 2, 4, 6 }; int tab2[] = { 3, 5, 7 };  int tabMerged[TAB_SIZE(tab1)+TAB_SIZE(tab2)]; merge(tab1, TAB_SIZE(tab1), tab2, TAB_SIZE(tab2), tabMerged, TAB_SIZE(tabMerged)); 


回答5:

Merging 2 unsorted integer arrays:

void main() {     clrscr();     int A[10],B[10],C[26],a,b,n1,n2;     cout>n1;     cout>n2;     a=0;b=0;int i=0;     clrscr();     while(1)     {         if(a>A[a];             clrscr();             a++;         }         if(b>B[b]; clrscr();             b++;         }         if(a==n1&&b==n2)             break;     }     a=0;b=0;     cout=n1&&b


回答6:

this is just simple modification of bill fosters answers which would take n dimension array:

    int main(void)     {         int m,n;         int c[100];             printf("Enter Size of first Array: \n");         scanf("%d",&m);         printf("Enter Size of Second Array: \n");         scanf("%d",&n);          int a[m],b[n]; //Declaring array a and b with its size m and n accordingly          int myval=m+n; //Size of the new array          for(int i=0;i


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!