Je suis assez nouveau pour C et je suis en train de faire un programme de tri de fusion de base. quand je compile ce code (en utilisant GCC)Erreur: initialiseur de tableau doit être une liste d'initialiseur ou large chaîne littérale dans le programme C Fusion Trier
int join(int arrayA[],int aLength,int arrayB[],int bLength)
{
int array[aLength+bLength];
int uptoA = 0; int uptoB = 0;//the upto variables are used as the index we have reached
while ((uptoA < aLength) && (uptoB < bLength))
{
if (arrayA[uptoA] < arrayB[uptoB])
{
array[uptoA+uptoB] = arrayA[uptoA];
uptoA++;
} else {
array[uptoA+uptoB] = arrayB[uptoB];
uptoB++;
}//else
}//while
if (uptoA!=aLength)//if A is the array with remaining elements to be added
{
for (int i = uptoA+uptoB; i < aLength+bLength; i++)
{
array[i] = arrayA[uptoA];
uptoA++;
}
} else {//if B is the array with elements to be added
for (int i = uptoB+uptoA; i < aLength+bLength; i++)
{
array[i] = arrayB[uptoB];
uptoB++;
}//for
}//else
return array;
}//int join
int merge_sort(int array[],int arrayLength)
{
if (arrayLength <= 1)
{
return array;
}
if (arrayLength == 2)
{
if (array[0] > array[1]) {return array;}//if it's fine, return the original array
int returningArray[2]; returningArray[0] = array[1]; returningArray[1] = array[0]; //just makes an array that is the reverse of the starting array
return returningArray;
}
int aLength = arrayLength/2;
int bLength = arrayLength - aLength;
//now I will create two arrays with each of the halves of the main array
int arrayAunsorted[aLength];
int arrayBunsorted[bLength];
for (int i = 0; i < aLength; i++)
{
arrayAunsorted[i] = array[i];
}
for (int i = aLength; i < arrayLength; i++)//this goes from the end of arrayA to the end of the main array
{
arrayBunsorted[i] = array[i];
}
int arrayA[aLength] = merge_sort(arrayAunsorted,aLength);
int arrayB[bLength] = merge_sort(arrayBunsorted,bLength);
printf("I can get this far without a segmentation fault\n");
return join(arrayA,aLength,arrayB,bLength);
}
Je sais que certaines parties de ce code sont sous forme terrible et mauvais, mais je vais résoudre ce problème une fois que je reçois le programme fonctionne réellement. Je suis très nouveau à C, donc j'espère que ce n'est pas une question stupide.
Il n'y a pas d 'initialiseur de tableau dans ce code. –
@EugeneSh. L'erreur est probablement de 'int ARRAYA [aLength] = merge_sort (arrayAunsorted, aLength);' – Barmar
@Barmar aha .. manqué celui- –