Jusqu'à présent, vous avez créé des tableaux statiques, de taille fixe. Les tableaux dynamiques peuvent utiliser des structures et malloc() pour changer leur taille. Lorsque le tableau est plein: Allouer un nouveau bloc de mémoire. Copiez les données d'un pointeur à l'autre. Libère l'ancien pointeur. Affectation du nouveau pointeur à la structure du tableau dynamiqueProgrammation en C créant un tableau dynamique
Il suffit d'implémenter les fonctions pour initialiser la matrice dynamique et étendre la matrice dynamique. Suivez les commentaires pour voir ce que vous devez coder. Les commentaires qui ont besoin de code ont TODO: écrits en eux memcpy (void * dest, void * src, int octets) Une fonction utile pour copier la mémoire entre les pointeurs. Paramètre 1: pointeur de destination que vous copiez. Paramètre 2: pointeur source à partir duquel vous copiez. Paramètre 3: nombre d'octets à copier
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//The dynamic array struct. This maintains my pointer to memory, effective size and maximum size
typedef struct
{
double *arrayPointer;
int effectiveSize;
int maximumSize;
} DynamicArray;
//Initialize the dynamic array
void CreateArray(DynamicArray *mArray, int initialSize)
{
//TODO: Use malloc to create an array of the initial size. Assign to the arrayPointer variable
//TODO: Set effective size to 0 and maximum size to the initial size
}
//Expand the array to a new size
void ExpandArray(DynamicArray *mArray, int newSize)
{
//TODO: Create a new pointer (double *newArray) and set it with a malloc call of the new size
//TODO: Using either memcpy or a for loop, copy all of the data from the old array to the new one.
//You are only copying to mArray->maximumSize, not the new size.
//TODO: Using the free function, release the previous mArray->arrayPointer
//TODO: Update mArray with the new pointer and the new maximum size. Effective size does not change.
}
//Add a new value from the user to the array
void AddValue(DynamicArray *mArray)
{
//Get the input
double input;
printf("Enter a new value: ");
scanf_s("%lf", &input);
//Assign the input to the array. Increase effective size
mArray->arrayPointer[mArray->effectiveSize] = input;
mArray->effectiveSize++;
//If effective size is now the same as maximum size we need to expand.
if (mArray->effectiveSize == mArray->maximumSize)
{
//Output that we are expanding
printf("Expanding array from %d to %d\n", mArray->maximumSize, mArray->maximumSize * 2);
//Double the size of the array
ExpandArray(mArray, mArray->maximumSize * 2);
}
}
//Print the array
void PrintArray(const DynamicArray *mArray)
{
int i;
//Walk through the array up to effective size and print the values
for (i = 0; i < mArray->effectiveSize; i++)
{
printf("%.2lf ", mArray->arrayPointer[i]);
}
printf("\n");
}
int main(void)
{
int i;
//Create my dynamic array of size 5
DynamicArray mArray;
CreateArray(&mArray, 5);
//Add five values to it
for (i = 0; i < 5; i++)
{
AddValue(&mArray);
}
//Print the array
PrintArray(&mArray);
//Add five more values
for (i = 0; i < 5; i++)
{
AddValue(&mArray);
}
//Print the array
PrintArray(&mArray);
system("pause");
}
l'image est de savoir comment son supposons ressembler.
S'il vous plaît aider comme je suis coincé et ne sais pas quoi faire
Salut franedu, bienvenue! Stackoverflow n'est pas un service d'écriture de code. Les questions sans [énoncé de problème clair] (http://stackoverflow.com/help/mcve) ne sont pas utiles aux autres lecteurs; voyez si vous pouvez être plus précis avec votre question. :) Voici d'autres pages d'aide pour vous aider à démarrer: [Poser une bonne question] (http://stackoverflow.com/help/how-to-ask), [Quels types de questions dois-je éviter de demander] (http : //stackoverflow.com/help/dont-ask), et [Quels sujets puis-je poser à propos ici?] (http://stackoverflow.com/help/on-topic) – binarysubstrate
Vous devriez voir la fonction 'realloc'. Voir: http://linux.die.net/man/3/realloc et http://stackoverflow.com/questions/3684288/understanding-realloc – ViniciusArruda
Cela semble être un devoir, étant donné qu'il commence par dire "Up maintenant vous avez créé des tableaux statiques ". Il est possible que l'auteur de la question parle simplement à la deuxième personne. –