2017-10-26 3 views
0

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; 
} 
+2

Ich kann keine Bilder kompilieren. –

+1

Array-Indizes beginnt bei 0. –

+1

Wenn es wirklich nichts kann nicht gedruckt werden (auch „aufsteigende Sortierung“), dann rufen Sie es nicht oder es ist Fenster und nicht eine Konsolenanwendung. –

Antwort

1

Es gab nur wenige Fehler in Ihrem Code. Überprüfen Sie dieses. Tun Sie es nur für 10 Nummern. Machen Sie es für 1000 oder was auch immer Sie bevorzugen, indem Sie Ihre Zufallszahlenlogik schreiben.

#include <iostream> 
int f10(int arr[],int size); 
using namespace std; 
int main() 
{ 
    int arr[10] = {22,53,14,11,75,6,7,1,8,88}; 
    int result_index; 
    result_index = f10(arr,10); 
    cout << "Result = "<<result_index<<endl;  
} 
int f10(int array[], int size) { 
    int temp; // temporary swap variable 
    cout << "Default Array:" << endl; 
    for (int x=0; x < size; x++) 
    { 
     cout<< array[x]<<", "; 
    } 
    cout << "Ascending Order:" << endl; 
    for (int x = 0; x < size; x++){ 

     for(int j = 0; j < size; j++) { 

      if(j != size-1 && array[j] > array[j+1]) { // Major Change 

       // Swapping 
       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=0; x < size; x++) 
    { 
     cout<< array[x]<<", "; 
    } 

    int value; 
    cout <<"\nGimme a number to search:\n\t"; 
    cin >> value; 

    int left = 0, right = 9; // Major change 
    while (left <= right){ // Major change 

     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; 

} 
+0

thank you! Ich schätze es sehr. – Alex