按升序合并两个数组

时间:2015-03-23 04:22:30

标签: c++

我知道如何合并两个数组的逻辑,但问题是如何编码。 这是我的代码,它给出了正确的答案,但我的先生告诉我再做一次,请告诉我在这段代码中要添加的内容,

#include<iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2);
int main()
{
    const int size1=8;
    const int size=12;
    int arrayA[size1]={10,25,37,49,50,51,55,60};
    int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
    mergeArrays(arrayA,size1,arrayB,size);
}

int mergeArrays(int array1[],int size1,int array2[],int size2)
{
    int size3=size1+size2;
    int *array3=new int[size3];
    int k=0;
    for(int i=0;i<size1;i++)
    {
       array3[k]=array1[i];
       cout<<" "<<array3[k];
   }
   int j=0;

   for(int i=size1;i<size2;i++)
   {
        array3[k]=array2[j];
   }
   for(int i=size1;i<size2;i++)
   {
        for(int j=0;j<size2;j++)
        {
            array3[i]=array2[j];
            cout<<" "<<array3[i];
        }
            cout<<endl;
            delete[]array3;
            return array3[k++];
    }
}

我在很多地方搜索过这个但是无法纠正我的代码 我写过这段代码,但它没有给出正确的答案。

#include<iostream>
using namespace std;
int merge(int *a,int *b,int aSize,int bSize);
int main()
{
    const int aSize={8};
    const int bSize={12};
    int arrayA[aSize]={10,25,37,49,50,51,55,60};
    int arrayB[bSize]={2,5,26,27,29,32,40,45,70,80,90,95};
    merge(arrayA,arrayB,aSize,bSize);

       return 0;

}

int merge(int *a,int *b,int aSize ,int bSize)
{
    int cSize=aSize+bSize;
    int *c=new int[cSize];
    int j=0,k=0;
    int i=0;
    while(i<=aSize&&j<=bSize )
       {
           if(a[aSize ]<=b[bSize])      
             {
                    c[k]=a[aSize];
                    k++;
                    i++;
                  }
              else      
                {
        c[k]=b[bSize];
        k++;
        j++;
    }
}

for(int i=0;i<k;i++)
{
    cout<<c[i]<<endl;
}

delete[]c;

return c[k++];

}

1 个答案:

答案 0 :(得分:0)

您的先生请求Merging two arrays in ascending order。所以我认为你应该返回一个新数组,填充array1和array2的元素,元素应该是升序。这是一个工具。(假设您的输入结构已按升序排列。)

#include <iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2, int outArray[]);
int main()
{
    const int size1=8;
    const int size=12;
    int arrayA[size1]={10,25,37,49,50,51,55,60};
    int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
    int outArray[size1+size];
    int len = mergeArrays(arrayA,size1,arrayB,size, outArray);
    cout <<" "<< len;
    for (int i = 0; i< size1+size; ++i){
      cout <<" " << outArray[i];
    }
}

int mergeArrays(int array1[], int size1, int array2[], int size2, int outArray[])
{
  int i=0, j=0, k=0;
  int retSize = size1+size2;
  while (k<retSize){
    if (i==size1){// only left array2, copy it
      for (; j<size2; ++j){ 
        outArray[k++] = array2[j];
      }
    }else if (j == size2) { // only left array1, copy it
      for (; i<size1; ++i){
        outArray[k++] = array1[i];
      }
    }
    else if (array1[i] > array2[j]){ // copy the min value to outArray
      outArray[k++] = array2[j++];
    }else{
      outArray[k++] = array1[i++];
    }
  }
  return k;
}

现在,让我们看看你的第一个代码:

int mergeArrays(int array1[],int size1,int array2[],int size2)
{
    int size3=size1+size2;
    int *array3=new int[size3];
    int k=0;
    for(int i=0;i<size1;i++)
    {
       array3[k]=array1[i]; // k is not changed, so you just assign array1's each value to array3[0]
       cout<<" "<<array3[k];
   }
   int j=0;

   // what's the purpose of this loop?
   // and in loop, you don't use i, you just repeat set array3[0] = array2[0]!!
   for(int i=size1;i<size2;i++) 
   {
        array3[k]=array2[j];
   }
   for(int i=size1;i<size2;i++) // if array2's length bigger than array1's, will enter this loop. 
   {
        for(int j=0;j<size2;j++)
        {
            array3[i]=array2[j]; // this just repeat assign array2's each value to array3[i]!!
            cout<<" "<<array3[i];
        }
            cout<<endl;
            delete[]array3;     
            return array3[k++];  // you delete array3, but at here you access it!! this will crash! 
            // also, in this for i loop, you have return, so it will only execute once.
    }
   // reach function end and no return if not enter for loop.
}

我没看过你的第二段代码。我想你还需要做更多的学习。