2016-04-07 5 views
0

今回は、バイナリでソートされた型を入力する必要がある場合に、ユーザーが使用したい検索の種類(バイナリまたはシーケンシャル)を尋ねるプログラムを作成する必要があります。それがソートされるまで配列を返します。ここでソートされた配列をユーザーに依頼するループC++

がメインです:ここでは

int main(int argc, char** argv) { 

    int n, decide, inicio, fin, key; 

    cout << " ¿De que tamaño será tu Arreglo?: ";//What size your array will be? 
    cin >> n; 
    int arreglo[n]; // Create the array 

    cout << " ¿Qué tipo de Busqueda realizaremos hoy?\n\n";//What kind of search we will use? 
    cout << "\n 1. Busqueda Secuencial.\n 2. Busqueda Binaria.\n\n"; 
    cin >> decide; 

    cout << "\t\n- - Tu arreglo sera de " << n << " elementos. - -\n";//Your arry will be of "n" elements 

    switch (decide) { 
    case 1: 
     inicializaLista(n, arreglo); // Fill the array. 

     cout << "\n¿Que numero buscas?"; // Ask for the number we want to search. 
     cin >> key; 

     busquedaSecuencial(arreglo, n, key); 
     break; 

    case 2: 
     inicio = 0; 
     fin = n - 1; 

     cout << "\nIngrese valores en orden\n";//Type sorted values 
     inicializaLista(n, arreglo); // fill the array 

     validarLista(n, arreglo); //validate List 



     cout << "\n¿Que numero buscas?"; // Ask for the number we want to search. 
     cin >> key; 
     busquedaBinaria(arreglo, n, inicio, fin); 

     break; 
    } 

    return 0; 
} 

は私の検証機能である:ここで

bool validarLista(int n, int arreglo[]) { 

    cout << "Validando Lista ... "; 

    for (int i = 0; i < n - 1; i++) { 
    if (arreglo[i] > arreglo[i + 1]) { 
     return false; // It is proven that the array is not sorted. 
    } 
    } 
    return true; // If this part has been reached, the array must be sorted.}; 
} 

は、私は、配列

int inicializaLista(int n, int arreglo[]) { 
    cout << "\n"; 

    for (int i = 0; i < n; i++) { 
    cout << "Ingrese dato " << i + 1 << ": "; 
    cin >> arreglo[i]; 
    } 
}; 

を埋めるところ、私はいくつかの「DO-をやっていますwhiles "を試し、" if "とたくさんの構造で試しましたが、解決策を手に入れることはできません。これを正しくループして、検証関数がソートされた配列を要求し続けるようにします。 すべての変数やスペイン語のものは翻訳できませんが、お手伝いできれば幸いです。

+0

私はC++ではありませんが、文字列入力をcinから整数に変換するべきではありませんか? –

+0

ユーザは、配列を入力するために配列をソートするのではなく、ユーザが任意の順序でデータを受け入れるのが煩わしくなく、バイナリ検索が要求されていれば配列をソートすることができます。 – Peter

+0

うん!私はそれが私の教師の要求だと思った、あなたのフィードバックに感謝します。 –

答えて

0
do { 
    cout << "\nIngrese valores en orden\n";//Type sorted values 
    inicializaLista(n, arreglo); // fill the array 
} while (!validarLista(n, arreglo)); 
+3

なぜこれが良い解決策であるか説明してください。 – djl

+0

ありがとう、それは本当に働いた、それは、bool関数が真以外の値を返すまで、プログラムが配列を初期化するために働くと思います。 –

関連する問題