如何将数组中的值复制到新数组中?

 Chilldon螴暁鼕 发布于 2023-02-12 16:15

我一直在努力解决这个问题一直持续一周,我一直遇到问题.

我的目标:

编写一个为整数数组分配内存的函数.该函数将整数指针,数组大小和要分配的newSize作为参数.该函数返回指向已分配缓冲区的指针.首次调用该函数时,大小将为零,并将创建一个新数组.如果在数组大小大于零时调用该函数,则将创建一个新数组,并将旧数组的内容复制到新数组中.您的讲师已提供arrayBuilder.cpp作为此编程挑战的入门代码.此外,Lab9_1.exe是此应用程序的可执行文件,您可以测试它.

代码:

#include 
using namespace std;

int * arrayBuilder(int * arr, int size, int newSize);
void showArray(int * arr, int size);

int main()
{
int * theArray = 0;
int i;

cout << "This program demonstrates an array builder function." << endl << endl;

// create the initial array.  The initial size is zero and the requested size is 5.
theArray = arrayBuilder(theArray, 0, 5);

// show the array before values are added
cout << "theArray after first call to builder: " << endl;
showArray(theArray, 5);

// add some values to the array
for(int i = 0; i < 5; i++)
{
    theArray[i] = i + 100;
}

// show the array with added values
cout << endl << "Some values stored in the array: " << endl;
showArray(theArray, 5);

// expand the size of the array.  size is not the original size.  newSize
// must be greater than size.
theArray = arrayBuilder(theArray, 5, 10);

// show the new array with the new size
cout << endl << "The new array: " << endl;
showArray(theArray, 10);

cout << endl;

delete [] theArray; // be sure to do this a1t the end of your program!

system("pause");

return 0;
}

/*
FUNCTION: arrayBuilder
INPUTS Pointer to an array.  Size of the array. If size is zero, arr can be    NULL.
      Size of the new array.
OUTPUTS:  Returns a pointer to allocated memory.  If newSize is greater than size,
      an array of newSize is allocated and the old array is copied into the new
      array. Memory pointed to by the old array is deleted.  All new elements
      are initialized to zero.
*/


int * arrayBuilder(int * arr, int size, int newSize)
{
// TODO: Your code goes here


return NULL; // default return value.  No memory allocated!
}

/*
FUNCTION: showArray
INPUTS: Pointer to an array.  Size of the array. If size is zero, arr can be  NULL.
OUTPUTS:  Prints the contents of the array to the console.
*/


void showArray(int * arr, int size)
{
cout << "arr = ";

for(int i = 0; i < size; i++)
{
    cout << arr[i] << "  ";
}

cout << endl;

}

我的挣扎:我无法弄清楚如何切换"arr"和临时数组的值.

int * arrayBuilder(int * arr, int size, int newSize)
{
// TODO: Your code goes here
    int * temp = new int [newSize];

for (int i = size; i < newSize; i++)
{
        *arr = *temp;
        temp++;
}

return NULL; // default return value.  No memory allocated!
}

搜索答案时的另一种尝试:

int * arrayBuilder(int * arr, int size, int newSize)
{
// TODO: Your code goes here
int * temp = new int [newSize];
memcpy (temp, arr, size *sizeof(int));
// HINT: Design the function before writing it.
delete[]  arr;

for (int i = size; i < newSize; i++)
{
    temp[i] = i;
}

return NULL; // default return value.  No memory allocated!
}

基本上我的最终目标是让答案看起来像这样:

This program demonstrates an array builder function.

theArray after first call to the builder:
arr = 0 0 0 0 0

some values stored in the array:
arr = 100 101 102 103 104

the new array:
arr = 100 101 102 103 104 0 0 0 0 0

进展!!它不再崩溃:-)这就是我现在所处的位置:

This program demonstrates an array builder function.

theArray after first call to builder:
arr = -842150451  0  0  0  0

Some values stored in the array:
arr = 100  101  102  103  104

The new array:
arr = -842150451  -842150451  -842150451  -842150451  -842150451  -842150451  -8
42150451  -842150451  -842150451  -842150451

Press any key to continue . . .

如果我撞到墙上,我会继续修修补补,让大家知道!再次感谢你们!

好的!终于让它正确显示:

This program demonstrates an array builder function.

theArray after first call to the builder:
arr = 0 0 0 0 0

some values stored in the array:
arr = 100 101 102 103 104

the new array:
arr = 100 101 102 103 104 0 0 0 0 0

这就是我做的.当我为"临时"添加0值时,我觉得我可能在第二部分中作弊.我的理解是,我将从前一个数组中获取数据并将其放入新数组中,而我只是重新制作它.(因此它只适用于这组特定值[仅0]).有没有不同的方式我可以编写第二部分,所以它普遍适用于任何值抛出它?

int * arrayBuilder(int * arr, int size, int newSize)
{
int i = size;
int * temp = new int [newSize];
// What if the size is 0?
if (size <= 0)
{
    while (i < newSize)
    {
        temp[i] = 0;
        i++;
    }
}
// Assuming the size _isn't_ 0
else 
{
// "a new array will be created"  (good)

for (i = 0; i < newSize; i++)
{
    // The contents of the "old" array (arr) will be
    // copied into the "new" array (temp)
    while (i < size)
    {
        temp[i] = arr[i];
        i++;
    }
    while (i >= size && i < newSize)
    {
        temp[i] = 0;
        i++;
    }
    // as a hint, you can address the elements in 
    // both arrays using the [] operator:
    // arr[i]
    // temp[i]

}
}

// "The function returns a pointer to the allocated buffer."
// So, NULL is wrong, what buffer did you allocate?
return temp; // default return value.  No memory allocated!
}

woolstar.. 5

你已经得到了答案:

memcpy (temp, arr, size *sizeof(int));

但是之后你又犯了几个错误.首先,你需要return temp ;return NULL ;

但是你也不需要循环了 delete arr[] ;

delete arr[]如果大小为零也不要.

2 个回答
  • 你已经得到了答案:

    memcpy (temp, arr, size *sizeof(int));
    

    但是之后你又犯了几个错误.首先,你需要return temp ;return NULL ;

    但是你也不需要循环了 delete arr[] ;

    delete arr[]如果大小为零也不要.

    2023-02-12 16:17 回答
  • 既然你付出了一些努力.

    编写一个为整数数组分配内存的函数.

    为您提供了此功能的原型:

    int * arrayBuilder(int * arr, int size, int newSize);
    

    该函数将整数指针,数组大小和要分配的newSize作为参数.该函数返回指向已分配缓冲区的指针.

    这对于使用"旧"(传入)数组做任何事都没有任何说明,所以我们应该假设它需要保持独立.

    首次调用该函数时,大小将为零,并将创建一个新数组.

    鉴于上下文,上述文本毫无意义.随意告诉你的导师我这么说.如果大小为零,您如何知道要分配多少元素?

    如果在数组大小大于零时调用该函数,则将创建一个新数组,并将旧数组的内容复制到新数组中.

    好了,现在要做的是什么,需要胆量(你是如此接近)

    int * arrayBuilder(int * arr, int size, int newSize)
    {
        // What if the size is 0?
    
        // Assuming the size _isn't_ 0
        // "a new array will be created"  (good)
        int * temp = new int [newSize];
    
        for (int i = size; i < newSize; i++)
        {
            // The contents of the "old" array (arr) will be
            // copied into the "new" array (temp)
    
            // as a hint, you can address the elements in 
            // both arrays using the [] operator:
            // arr[i]
            // temp[i]
    
            // something is wrong here...
            *arr = *temp;
    
            // you definitely _don't_ want to do this
            temp++;
        }
    
        // "The function returns a pointer to the allocated buffer."
        // So, NULL is wrong, what buffer did you allocate?
        return NULL; // default return value.  No memory allocated!
    }
    

    2023-02-12 16:18 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有