尝试仅使用指针将元素从一个数组复制到另一个数组

时间:2018-04-05 03:51:27

标签: c++

我的教授希望我只使用指针,任何函数都不允许下标 我想让用户将两个大小和元素输入到两个单独的数组中,然后创建一个联合函数,它找到所有相同的元素(在任何一个数组中都没有重复的值)

我想要做的就是将我的数组元素从第一个数组移到union数组中,但是当我执行程序时,我只是得到随机数

void get_union(short *set1,short size1,short *set2,short size2,short *union_array,short size_union)  // look at call statement to assist incompleting this statement
{
    short *end1=(set1+size1), //*end2=(set1+size1+size2);

    for( ;set1<end1;set1++)
    {
      union_array=set1;
      cout<<"Union array value number "<<count++<<" "<<*union_array++<<endl;
    }
}

我也试过memcpy,但我以前从未使用它,特别是没有使用指针

    memcpy(union_array,set1,sizeof(union_array));

这是我的前两个函数,它们允许用户输入数据,然后显示它。

void inputData(short *data, short size)   // function to enter data into the array
{
    short count=1;
    short *end=(data+size);
    for( ;data<end;data++)
    {
        cout<<"Enter Number "<<count++<<" : ";
        cin>>*data;
        cout<<endl;
    }
}

void displayData(short *data, short size) // function to display data in an array
{
    short count;
    short *end=(data+size);
    for( ;data<end;data++)
    {
        cout<<"Number "<<count++<<" : "<<*data<<endl;
    }
}

这是我运行程序时得到的结果。整个过程贯穿但联合数组得到随机数

enter the number of values to store in the data set 1
   or zero to terminate the program
3
Enter Number 1 : 2

Enter Number 2 : 4

Enter Number 3 : 6


there are 3 values in the array set1
Number 0 : 2
Number 1 : 4
Number 2 : 6
enter the number of values to store in the data set 2
3
Enter Number 1 : 10

Enter Number 2 : 11

Enter Number 3 : 12


there are 3 values in the array set2
Number 0 : 10
Number 1 : 11
Number 2 : 12
Union array value number 1 -5245
the union array contains 0 values
the intersection array contains -1 values

2 个答案:

答案 0 :(得分:1)

我不确定你的问题,但让我试着帮助你:

  

我想要做的就是将我的数组元素从第一个数组移到union数组中,但是当我执行程序时,我只是得到随机数

using namespace std;

void get_union(short *set1,short size1,short *union_array,short size_union) {
    for (; size1; --size1) {
        *union_array++ = *set1++;
    }
}

int main () {
    short set1[] = {1, 0, 15, 35, 200, 12};
    size_t size = sizeof(set1) / sizeof(*set1);
    short union_arr[size];
    get_union(set1, size, union_arr, size);

    for (size_t i = 0; i < size; i++) {
        cout << union_arr[i] << " ";
    }
    cout << endl;
    return 0;
}
  

创建一个union函数,它找到所有相同的元素(在任何一个数组中都没有重复的值)

size_t get_union(short *set1, unsigned size1, short *set2, unsigned size2, short *union_array, unsigned size_union) {
    size_t count = 0;

    // TODO:: Make sure that the arrays are already sort
    // todo:: or use the following two commands:
    qsort(set1, size1, sizeof(*set1), [](const void * a, const void * b) -> int{
        return ( *(short*)a - *(short*)b );
    });
    qsort(set2, size2, sizeof(*set2), [](const void * a, const void * b) -> int{
        return ( *(short*)a - *(short*)b );
    });

    while (size1 && size2) {
        if (*set1 > *set2) {
            ++set2;
            --size2;
        } else if (*set1 < *set2) {
            ++set1;
            --size1;
        } else {
            *union_array++ = *set1++;
            --size1;
            ++set2;
            --size2;
            ++count;
        }
    }

    return count;
}

int main () {
    short set1[] = {1, 0, 15, 35, 200, 12};
    short set2[] = {50, 0, 15, 0, 200, 12};
    size_t size = sizeof(set1) / sizeof(*set1);
    short union_arr[size];
    size_t count;
    count = get_union(set1, size, set2, size, union_arr, size);

    cout << count << endl;
    for (size_t i = 0; i < count; i++) {
        cout << union_arr[i] << " ";
    }
    cout << endl;

    return 0;
}
顺便说一句,它是C问题,然后是C ++问题。在C ++中,您可以简单地使用向量,并使代码尽可能简单(并且union数组将与最小大小完全一致)。当你处理速度情况和API时,C ++中的指针在不同的情况下更相关..

答案 1 :(得分:1)

  

他想从我们这里写一个函数来比较两个数组(我拥有的set1和set2)并将所有数字都放在其中。所以如果第一个数组中有0个元素而另一个中有5个元素,那么union数组应该有5个元素

size_t get_unique_union(short *arr, size_t size, short *target) {
    size_t target_bigger = 0;
    short *curr, *curr_test;
    //size_t dup = 0; (1)

    if (!size) {
        return target_bigger;
    }

    curr = arr + 1; // Current place in the array.
    curr_test = curr; // Current place with the offset of the duplicate elements.
    while (curr_test < arr + size) {
        while (curr_test < arr + size && *arr == *curr_test) {
            curr_test++;
            //dup++; // | (1) Update global size. see comment.
        }
        *curr++ = *curr_test++;
    }
    size -= curr_test - curr; // Update global size | (1) size -= dup;

    if (curr == curr_test) { // If it is a unique appearance (If there were no founded duplicates).
        *target = *arr; // Set target in the current place the appearance.
        target_bigger = 1; // Mention that in the next recursive calling,
                            // it will be called from the next place in the unique array.
    }

    for (size_t i = 0; i < size; i++) { // Display the current checked array (Without the doubles of the current appearance).
        cout << arr[i] << " ";
    }
    cout << endl;

    return target_bigger + get_unique_union(arr + 1, size - 1, target + target_bigger); // Recursive call with the next element in the array.
}

size_t get_union(short *set1, unsigned size1, short *set2, unsigned size2, short *uniq_arr) {
    size_t global_size = size1 + size2;
    size_t uniq_size = 0;
    short union_array[global_size];

    for (size_t i = 0; i < size1; i++) {
        union_array[i] = set1[i];
    }

    for (size_t i = 0; i < size2; i++) {
        union_array[i + size1] = set2[i];
    }

    for (size_t i = 0; i < global_size; i++) {
        cout << union_array[i] << " ";
    }
    cout << endl;

    return get_unique_union(union_array, global_size, uniq_arr);
}

int main () {
    short set1[] = {12, 0, 2, 1, 12, 12, 6, 8};
    short set2[] = {3, 0, 300, 12, 12, 12};
    size_t size1 = sizeof(set1) / sizeof(*set1);
    size_t size2 = sizeof(set2) / sizeof(*set2);
    short union_arr[size1  + size2];
    size_t count;
    count = get_union(set1, size1, set2, size2, union_arr);

    cout << "Results:" << endl;
    cout << "Count: " << count << endl;
    cout << "Arr: [";
    for (size_t i = 0; i < count; i++) {
        cout << union_arr[i] << ((i < count - 1) ? ", " : "");
    }
    cout << "]" << endl;

    return 0;
}

此代码甚至可以处理同一阵列中存在重复的情况。它将数组组合到同一个大数组中,删除重复项,并仅插入完全没有重复项的元素。 注意函数“get_unique_union”是递归的,并返回唯一元素的计数,而不是唯一的union数组的实际大小。 代码格式是C one,而不是C ++(它适用于C ++,已经用cpp文件编写并用g ++编译器编译。但在C ++中,它将使用向量编写,并且它将在唯一的联合数组中保存更多无用的位置)。