Comment faire pour copier les valeurs d'un tableau dans un nouveau?

J'ai essayé de comprendre cela et de décollage depuis une semaine maintenant et je continuer à courir dans des problèmes.

Mon objectif:

Écrire une fonction qui alloue de la mémoire pour un tableau d'entiers. La fonction prend comme argument un pointeur entier, la taille de la matrice, et newSize être attribués. La fonction renvoie un pointeur vers la mémoire tampon allouée. Lorsque la fonction est appelée en premier, la taille sera de zéro et un nouveau tableau sera créé. Si la fonction est appelée lorsque la taille du tableau, est supérieure à zéro, un nouveau tableau est créé et le contenu de l'ancien tableau seront copiés dans le nouveau tableau. Votre instructeur a fourni arrayBuilder.cpp en tant que starter code pour ce défi en termes de programmation. En outre, Lab9_1.exe est l'exécutable pour cette application que vous pouvez tester.

Le code:

#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;
}

Mes luttes: je ne peux pas comprendre comment le commutateur "arr" et un tableau temporaire de valeurs.

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

une autre tentative, alors que la recherche de réponses:

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

Fondamentalement, mon but final est d'avoir la réponse ressembler à ceci:

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

PROGRÈS!! Ses pas s'écraser plus 🙂 C'est là où je suis maintenant:

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

Je vais continuer à bricoler et de laisser tout le monde sais que si j'ai frappé un mur! Merci encore les gars!

D'ACCORD! on fini par s'afficher correctement:

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

C'est ce que j'ai fait. Je me sens comme si je peut avoir triché dans la deuxième partie quand j'ai mis des valeurs à 0 pour "temp". C'est ma compréhension que j'allais prendre les données du tableau précédent et le mettre dans le nouveau, et à la place j'ai juste refait. (Donc, il ne fonctionne qu'avec cet ensemble particulier de valeurs [0]). Est-il un autre moyen pour que je puisse le code de la deuxième partie de sorte qu'il fonctionne de manière universelle, à ce que les valeurs sont jetées???

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

OriginalL'auteur Peter | 2013-12-05