2015-10-05 37 views
-2

我正在處理一個程序,該程序對數組進行排序,然後使用氣泡或選擇排序搜索數組,然後使用二進制或線性搜索。它應該詢問用戶將要使用哪個排序和搜索功能。它應該始終顯示搜索或排序數組所需的步驟。它是一個我必須使用的固定(常量)數組。我有一個爲此製作的程序的基礎,但它給了我一些錯誤。如果你們有機會,請讓我知道我做錯了什麼。我認爲這是一個簡單的錯誤,但我不確定。我是編程新手,請給我你的想法!搜索並排序未排序的數組

#include <iostream> 
#include <cmath> 
#include <string> 
#include <cstdlib> 

using namespace std; 
int answer; 
int answer2; 
int size; 
void bubbleSort(int[], int); 
void selectionSort(int[], int); 
int value; 

int unsorted[] = 
{ 
    85, 64, 16, 18, 1, 88, 48, 63, 54, 83, 79, 50, 0, 55, 
    17, 99, 69, 53, 65, 22, 93, 86, 9, 37, 56, 23, 21, 52, 
    78, 29, 14, 58, 35, 47, 68, 87, 3, 34, 5, 74, 4, 45, 
    41, 49, 67, 89, 92, 60, 72, 20, 8, 15, 42, 71, 31, 36, 
    90, 84,70, 51, 28, 32, 81, 10, 82, 40, 57, 24, 25, 91, 
    44, 66, 30, 62, 94, 6, 7, 46, 43,38, 75, 11, 39, 80, 
    98, 27, 12, 76, 96, 2, 77, 19, 26, 59, 33, 73, 13, 
    61, 95, 97 
}; 

int main() 
{ 
    cout << 
     "Which algorithm should be used to sort?" << 
     "(0: Bubble Sort, 1: Selection Sort)" << endl; 

    cin >> answer; 
    if (answer == 0) 
    { 
     bubbleSort; 
    } 
    else 
    { 
     selectionSort; 
    } 

    // Bubble Sort Fun 
    void bubbleSort(int unsorted[], int size) 
    { 
     bool swap; 
     int temp; 

     do 
     { 
      swap = false; 
      for (int count = 0; count < (size - 1); count++) 
      { 
       if (unsorted[ count ] > unsorted[ count - 1 ]; count++) 
       { 
        temp = unsorted[ count ]; 
        unsorted[ count ] = unsorted[ count + 1 ]; 
        unsorted[ count + 1 ] = temp; 
        swap = true; 
       } 
      } 
     } 
     while (swap); 
    } 
    // Selection Sort Function 
    void selectionSort(int unsorted[], int size) 
    { 
     int startScan, minIndex, minValue; 
     for (startScan = 0; startScan <(size - 1); startScan++) 
     { 
      minIndex = startScan; 
      minValue = unsorted[ startScan ]; 
      for (int index = startScan + 1; index < size; index++) 
      { 
       minValue = unsorted[ index ]; 
       minIndex = index; 
      } 
      unsorted[ minIndex ] = unsorted[ startScan ]; 
      unsorted[ startScan ] = minValue; 
     } 
    } 

    // Sorting completed 
    cout << 
     "Which algorithm should be used to search?" << 
     "(0: Linear Search, 1: Binary Search)" << endl; 

    cin >> answer2; 
    if (answer == 0) 
    { 
     answer == linearSearch; 
    else 
     answer == binarySearch; 
    } 
    // Linear Search Function 
    int linearSearch(int unsorted[], int numElements, int value) 
    { 
     int index = 0;      // Subscript to search array 
     int pos = -1;      // Record postion of search value 
     bool found = false;     // To indicate if value was found 

     while (index < numElements && !found) 
     { 
      if (unsorted[ index ] == value) // If value was found 
      { 
       found = true; 
       pos = index; // record value 
      } 
      index++; // Move onto next element in array 
     } 
     return pos; // Return the postion, or -1 
    } 


    // Binary Search Function 
    int binarySearch(int unsorted[], int size, int value) 
    { 
     int first = 0, 
      last = size - 1, 
      middle, 
      position = -1; 
     bool found = false; 

     while (!found && first + last)/2; // Calcualte the middle element 
     { 
      if (unsorted[ middle ] == value) // If value is the middle element 
      { 
       found = true; 
       position = middle; 
      } 
      else if (unsorted[ middle ] > value) // If value is in lower part 
       last = middle - 1; 
      else 
       first = middle - 1; // If value is in upper part 
     } 
     return position; 
    } 
    return 0; 
} 
+2

什麼樣的錯誤? –

+0

'bubbleSort;'看起來你錯過了一些重要的東西,可能是*參數*? – PaulMcKenzie

+0

一些作業的複製粘貼代碼(你沒有做)的味道。使用未聲明的變量......這只是一般的混亂。 –

回答

0

這會運行,但你需要檢查它,因爲我不知道什麼是什麼。此外,您的二進制搜索功能將需要修復,因爲它現在將被奇數分隔。有各種各樣的錯誤,我會嘗試在你的原始代碼中指出它們。

#include <iostream> 
#include <cmath> 
#include <string> 
#include <cstdlib> 

using namespace std; 

int answer; 
int answer2; 
int size; 
int value; 

void bubbleSort(int[], int); 
void selectionSort(int[], int); 
int linearSearch(int unsorted[], int numElements, int value); 
int binarySearch(int unsorted[], int size, int value); 



int unsorted[100] = { 85, 64, 16, 18, 1, 88, 48, 63, 54, 83, 79, 50, 0, 55, 17, 99, 69, 53, 65, 
         35, 47, 68, 87, 3, 34, 5, 74, 4, 45, 41, 49, 67, 89, 92, 60, 72, 20, 8, 
         15, 42, 71, 31, 36, 90, 84, 70, 51, 28, 32, 81, 10, 82, 40, 57, 24, 25, 58, 
         91, 44, 66, 30, 62, 94, 6, 7, 46, 43, 38, 75, 11, 39, 80, 98, 27, 12, 76, 
         96, 2, 77, 19, 26, 59, 33, 73, 13, 61, 95, 97, 22, 93, 86, 9, 37, 56, 14, 
         23, 21, 52, 78, 29 
        }; 

void bubbleSort(int unsorted[], int size); 
void selectionSort(int unsorted[], int size); 

int main() 
{ 
    cout << "Which algorithm should be used to sort? (0: Bubble Sort,1:Selection Sort) " << endl; 
    cin >> answer; 
    if (answer == 0) 
    { 
     bubbleSort(unsorted, 100); 
    } 
    else 
    { 
     selectionSort(unsorted, 100); 
    } 

    // Sorting completed 
    cout << "Sorting completed" << endl << endl; 
    cout << "Which algorithm should be used to search? (0:Linear Search, 1: Binary Search) " << endl; 
    cin >> answer2; 

    if (answer2 == 0) 
    { 
     answer2 = linearSearch(unsorted, 100, 4); 
     cout << "Binary search result: " << answer2 << endl; 
    } 

    else 
    { 
     //answer2 = binarySearch(unsorted, 100, 4);    // binarySearch() you'll have to fix 
     //cout << "Binary search result: " << answer2 << endl; // binarySearch() you'll have to fix 
    } 

} 

// Bubble Sort Fun 
void bubbleSort(int unsorted[], int size) 
{ 
    bool swap; 
    int temp; 

    do 
    { 
     swap = false; 
     for (int count = 0; count < (size - 1); count++) 
     { 
      if (unsorted[count] > unsorted[count + 1]) 
      { 

       temp = unsorted[count]; 
       unsorted[count] = unsorted[count + 1]; 
       unsorted[count + 1] = temp; 
       swap = true; 
       count++; 
      } 
     } 
    } while (swap); 
} 

// Selection Sort Function 
void selectionSort(int unsorted[], int size) 
{ 
    int startScan, minIndex, minValue; 

    for (startScan = 0; startScan < (size - 1); startScan++) 
    { 
     minIndex = startScan; 
     minValue = unsorted[startScan]; 

     for (int index = startScan + 1; index < size; index++) 
     { 
      minValue = unsorted[index]; 
      minIndex = index; 
     } 
     unsorted[minIndex] = unsorted[startScan]; 
     unsorted[startScan] = minValue; 
    } 
} 

// Linear Search Function 
int linearSearch(int unsorted[], int numElements, int value) 
{ 
    int index = 0;      // Subscript to search array 
    int pos = -1;      // Record position of search value 
    bool found = false;     // To indicate if value was found 

    while (index < numElements && !found) 
    { 
     if (unsorted[index] == value) // If value was found 
     { 
      found = true; 
      pos = index;   // record value 
     } 
     index++;     // Move onto next element in array 
    } 
    return pos;      // Return the position, or -1 
} 



//You're going to have to fix this, this will end up dividing odd numbers by two, I don't think that'll work. 

//// Binary Search Function 
//int binarySearch(int unsorted[], int size, int value) 
//{ 
//  int first = 0, 
//   last = size - 1, 
//   middle = 0, 
//   position = -1; 
//  bool found = false; 
// 
//  while (!found) 
//  { 
//   middle = (first + last)/2;  // Calculate the middle element 
//   
//   if (unsorted[middle] == value)  // If value is the middle element 
//   { 
//    found = true; 
//    position = middle; 
//   } 
//   else if (unsorted[middle] > value) // If value is in lower part 
//    last = middle - 1; 
//   else 
//    first = middle + 1;    // If value is in upper part 
//  } 
//  return position; 
// 
// return 0; 
//} 
+1

真的很糟糕的電話回答這個問題與削減和可以解。 OP現在將繼續進行剪切和粘貼,並浪費他們的時間不學習。 – user4581301

+0

我即將評論他的代碼,呵呵。我確切知道你在說什麼。 – Zebrafish

+0

非常感謝!我仍在編寫這個程序,當它完成時我會發布它。 – Pandaman427

0

我不能評論,所以我必須在這裏回答。 你的代碼很長,所以我會告訴我第一次看到的東西。

  1. 您必須聲明所有函數。你沒有調用二進制搜索功能。

  2. 您定義的函數和您的原型函數是不一樣的。它必須是一樣的。例如:

    int foo(int a); //這是原型 int foo(int a)//這是您定義的函數 int b; 返回b = a + 5; }

  3. 你的錯誤很少是未使用的變量,沒關係,但它會讓你的代碼變得複雜。

  4. 當您調用函數時,您必須調用正確。就拿我上面的例子,如果我調用的函數,我會打電話:cout << foo(5);// cout << foo; doesn't work.

呀,剛修好的第一,它剩下的只是小問題。 確保您瞭解教科書使用哪些內容。可以使用,但沒有明白,你找不到錯誤。

0

好的,我已經評論過你的代碼,所有東西都在這個地方。

#include <iostream> 
#include <cmath> 
#include <string> 
#include <cstdlib> 

using namespace std; 
int answer; 
int answer2; 
int size; 
void bubbleSort(int[], int); 
void selectionSort(int[], int); 

// int binarySearch(int unsorted[], int size, int value) <--- You need to prototype/declare this function before it is called 
// int linearSearch(int unsorted[], int numElements, int value) <--- Same here 

int value; 

int unsorted[] = 
{ 
    85, 64, 16, 18, 1, 88, 48, 63, 54, 83, 79, 50, 0, 55, 
    17, 99, 69, 53, 65, 22, 93, 86, 9, 37, 56, 23, 21, 52, 
    78, 29, 14, 58, 35, 47, 68, 87, 3, 34, 5, 74, 4, 45, 
    41, 49, 67, 89, 92, 60, 72, 20, 8, 15, 42, 71, 31, 36, 
    90, 84,70, 51, 28, 32, 81, 10, 82, 40, 57, 24, 25, 91, 
    44, 66, 30, 62, 94, 6, 7, 46, 43,38, 75, 11, 39, 80, 
    98, 27, 12, 76, 96, 2, 77, 19, 26, 59, 33, 73, 13, 
    61, 95, 97 
}; 

int main() 
{ 
    cout << 
     "Which algorithm should be used to sort?" << 
     "(0: Bubble Sort, 1: Selection Sort)" << endl; 

    cin >> answer; 
    if (answer == 0) 
    { 
     bubbleSort;   // Saying bubbleSort doesn't do anything. bubbleSort() is a function that takes two arguments 
    } 
    else 
    { 
     selectionSort;  // Same here, it should be selectionSort(arg1, arg2) 
    } 



// Bubble Sort Fun 
void bubbleSort(int unsorted[], int size) 
{ 
    bool swap; 
    int temp; 

    do 
    { 
     swap = false; 
     for (int count = 0; count < (size - 1); count++) 
     { 
      if (unsorted[count] > unsorted[count - 1]; count++) 
      { 
       temp = unsorted[count]; 
       unsorted[count] = unsorted[count + 1]; 
       unsorted[count + 1] = temp; 
       swap = true; 
      } 
     } 
    } while (swap); 
} 

// Selection Sort Function 
void selectionSort(int unsorted[], int size) 
{ 
    int startScan, minIndex, minValue; 
    for (startScan = 0; startScan < (size - 1); startScan++) 
    { 
     minIndex = startScan; 
     minValue = unsorted[startScan]; 
     for (int index = startScan + 1; index < size; index++) 
     { 
      minValue = unsorted[index]; 
      minIndex = index; 
     } 
     unsorted[minIndex] = unsorted[startScan]; 
     unsorted[startScan] = minValue; 
    } 
} 


// __________________________________ This whole section should be in main()_____________________________________// 

    // Sorting completed 
    cout << 
     "Which algorithm should be used to search?" << 
     "(0: Linear Search, 1: Binary Search)" << endl; 

    cin >> answer2; 
    if (answer == 0) // <----- This should be answer2, not answer. 
    { 
     answer == linearSearch;  // <---- == is wrong. == is when checking equality, like in the if brackets. Here you want answer2 = linearSearch(arg1, arg2, arg3); // Also you'll need to close the bracket to the if code before the "else". thanks to user4581301 for pointing this out 
    else 
     answer == binarySearch;  // Same here. 
    } 
// ______________________________________________________________________________________________________________// 


    // Linear Search Function 
int linearSearch(int unsorted[], int numElements, int value) 
{ 
    int index = 0;      
    int pos = -1;      
    bool found = false;     

    while (index < numElements && !found) 
    { 
     if (unsorted[index] == value) 
     { 
      found = true; 
      pos = index; 
     } 
     index++; 
    } 
    return pos; 
} 




// The binary search function might not work, because once you increment or decrement one from middle, 
// next time you'll be dividing an odd number by two, I think that's a bad idea. 

// Binary Search Function 
int binarySearch(int unsorted[], int size, int value) 
{ 
    int first = 0, 
     last = size - 1, 
     middle, 
     position = -1; 
    bool found = false; 

    while (!found && first + last)/2; // <--- I don't think this makes sense. In the while brackets all you want is !found. 
    { 
     if (unsorted[middle] == value) 
     { 
      found = true; 
      position = middle; 
     } 
     else if (unsorted[middle] > value) 
      last = middle - 1; 
     else 
      first = middle - 1; 
    } 
    return position; 
} 
return 0; 
} 
+0

實際上,OP不會在'main'上丟失一個右括號。 OP將函數直接粘貼到'main'中。您可以通過排序和搜索函數之間的代碼位以及最後的返回0來判斷。你選擇了「answer」/「answer2」問題,但錯過了「if」的包圍。 – user4581301

+0

@ user4581301啊,是的,你說得對。謝了哥們。如果可以,請隨意編輯它。 – Zebrafish