如何将值从数组复制到新数组? [英] How to copy values from an array into a new one?

查看:116
本文介绍了如何将值从数组复制到新数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个星期以来,我一直在试图解决这个问题,但我一直遇到问题。

I've been trying to figure this out off and on for a week now and I keep running into problems.

我的目标:

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

Write a function that allocates memory for an integer array. The function takes as an argument an integer pointer, the size of the array, and newSize to be allocated. The function returns a pointer to the allocated buffer. When the function is first called, the size will be zero and a new array will be created. If the function is called when the array size is greater than zero, a new array will be created and the contents of the old array will be copied into the new array. Your instructor has provided arrayBuilder.cpp as starter code for this programming challenge. In addition, Lab9_1.exe is the executable for this application which you can test.

代码:

#include <iostream>
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 和一个临时数组的值。

My struggles: I cannot figure out how to switch "arr" and a temporary array's values.

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!
}

搜索答案时的另一尝试:

another attempt while searching for answers:

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!
}

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

Basically my end goal is to have the answer look like this:

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

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

PROGRESS!! Its not crashing anymore :-) This is where I'm at now:

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 . . .

我会不断修补,让所有人知道我撞墙了!再次感谢大家!

I'll keep tinkering and let everyone know if I hit a wall! Thanks again guys!

好!最终使其正确显示:

OKAY! finally got it to display properly:

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

这就是我所做的。当我为 temp输入0值时,我觉得我可能在第二部分中作弊。我的理解是,我将从上一个数组中获取数据并将其放入新数组中,而我只是重新制作了它。 (因此,它仅适用于这组特定的值[仅0)。我可以用另一种方式编码第二部分,以便它与抛出的任何值通用吗?

This is what I did. I feel like I may have cheated in the second part when i put 0 values in for "temp". It was my understanding that i was going to take the data from the previous array and put it into the new one, and instead I just remade it. (So it will only work with this particular set of values [only 0's]). Is there a different way I can code the second part so it works universally with whatever values are thrown at it???

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!
}


推荐答案

由于您付出了一些努力


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

Write a function that allocates memory for an integer array.

已为您提供了此函数的原型:

The prototype for this function was provided for you:

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




该函数将整数指针作为参数,
数组,并分配newSize。该函数返回一个指向分配的缓冲区
的指针。

The function takes as an argument an integer pointer, the size of the array, and newSize to be allocated. The function returns a pointer to the allocated buffer.

这与 old(通过)无关。

This says nothing about doing anything with the "old" (passed in) array, so we should assume it needs to be left alone.


首次调用该函数时,大小将为零,并且

When the function is first called, the size will be zero and a new array will be created.

上面的文字对于上下文没有意义。随时告诉您的老师我是这样说的。如果大小为零,您如何知道要分配多少个元素?

The above text is meaningless given the context. Feel free to tell your instructor I said so. If the size is zero, how do you know how many elements to allocate?


如果在数组大小大于零,将创建
新数组,并将旧数组的内容复制到新数组中。

If the function is called when the array size is greater than zero, a new array will be created and the contents of the old array will be copied into the new array.

好,现在您需要做些什么了(您已经如此关闭了)

OK, now the guts of what needs to be done (you're so close)

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!
}

这篇关于如何将值从数组复制到新数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆