简体   繁体   中英

Merging two sorted array on third by creating a new array on heap

I have a class array inside which I have declared an array its size and length. I am trying to merge two sorted arrays by creating the third array on the heap and both the sorted array will be merged on the third array. But whenever I create a new arr on heap the compiler gives me this error: request for member '..' in '..' which is of non-class type

class Array
{
public:

    int A[10];
    int length;
    int Size;

};
void display(Array arr)
{
    int i;
    for(i=0;i<arr.length;i++)
    {
        cout<<arr.A[i]<<" ";
    }
}
void Merge(Array *arr1,Array *arr2)
{
    int i,j,k;
    i=j=k=0;
   int *arr3;
   arr3=new int[10];
    while(i<arr1->length && j<arr2->length)
    {
        if(arr1->A[i]<arr2->A[j])
            arr3->A[k++]=arr1->A[i++];
        else
          arr3->A[k++]=arr2->A[j++];
    }
    for(;i<arr1->length;i++)
    {
         arr3->A[k++]=arr1->A[i];
    }
     for(;j<arr2->length;j++)
    {
         arr3->A[k++]=arr1->A[j];
    }

}
int main()
{

   Array arr1{{1,3,5,7},4,4};
Array arr2{{2,4,6,8},4,4};
Array *arr3;
arr3=Merge(&arr1,&arr2);
display(*arr3);
    return 0;
}

The root cause of all your problems is that you use C-Style array with a magical size 10. Like in int A[10]; . This is a major problem and should be avoided in C++.

Additionally, and the same, In C++ we usually do not use raw pointer for owned memories or new and such stuff.

Anyway. The design will never work, if the number of elements in both Array classes is greater then 5. Because then you will definitely get an out of bounds problem.

You must use a std::vector .

So, all bad. But I know that I will hear now, that the teacher said, no vector but new . The teacher should be fired or begin to teach C instead of C++.

Anyway again, I will fix the major bugs for you. But the sorting algorithm will work neither.

So,

  • If you want to return an Array, then change the signature of your function aand return an Array.
  • You do want to have a new Array, not new intes. So, please allocate a new Array instead.
  • Do not forget to release the newed Arrary at then end.
  • Set size and length of the new array.
  • Refactor your complete code.

Code example with some fixes:

#include <iostream>
class Array
{
public:

    int A[10];
    int length;
    int Size;
};
void display(Array arr)
{
    int i;
    for (i = 0; i < arr.length; i++)
    {
        std::cout << arr.A[i] << " ";
    }
}
Array* Merge(Array* arr1, Array* arr2)
{
    int i, j, k;
    i = j = k = 0;
   
    Array *arr3 = new Array;

    while (i < arr1->length && j < arr2->length)
    {
        if (arr1->A[i] < arr2->A[j])
            arr3->A[k++] = arr1->A[i++];
        else
            arr3->A[k++] = arr2->A[j++];
    }
    for (; i < arr1->length; i++)
    {
        arr3->A[k++] = arr1->A[i];
    }
    for (; j < arr2->length; j++)
    {
        arr3->A[k++] = arr1->A[j];
    }
    arr3->length = arr1->length + arr2->length;
    return arr3;
}
int main()
{

    Array arr1{ {1,3,5,7},4,4 };
    Array arr2{ {2,4,6,8},4,4 };
    Array* arr3;
    arr3 = Merge(&arr1, &arr2);
    display(*arr3);
    delete[]arr3;
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM