Könnte jemand mir helfen, diesen Code zu reparieren? Das Haupt füllt ein Array von 1000 Zufallszahlen von 1 bis 1000, und wenn es in die Funktion geht, sortiert es in aufsteigender Reihenfolge und es folgt eine Binärsuche, aber das Problem ist, dass es nichts ausdruckt.C++ Binärsuchfunktion druckt nicht
int f10(int array[], int size)
{
int temp; // temporary swap variable
cout << "Ascending Order:" << endl;
for (int x = 1; x <= size; x++)
{ //how times
for (int j = 1; j <= size; j++)
{
if (array[j] > array[j + 1])
{
//we need to swap
temp = array[j]; //temp is holding the first value
array[j] = array[j + 1]; //
array[j + 1] = temp; //temp holds the second value
}
}
}
for (int x = 1; x <= size; x++)
{
cout << array[x] << ", ";
}
int value;
cout << "\nGimme a number to search:\n\t";
cin >> value;
int left = array[1], right = array[1000];
while (left < right)
{
int middle = (left + right)/2;
if (array[middle] == value)
{
return middle;
}
else if (array[middle] > value)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return -1;
}
Ich kann keine Bilder kompilieren. –
Array-Indizes beginnt bei 0. –
Wenn es wirklich nichts kann nicht gedruckt werden (auch „aufsteigende Sortierung“), dann rufen Sie es nicht oder es ist Fenster und nicht eine Konsolenanwendung. –