2014-10-27 126 views
-5

我寫了一個基本的線性搜索C++代碼。每當我運行這個時,我得到的結果總是與預期結果相反。
例如,我想要搜索4.在它存在的數組中,它會說沒有找到數字,但是在搜索一個缺失元素時,它會說該元素在位置0找到。C++代碼故障

即使經過一個小時左右的時間,我仍然沒有找到任何解決方案。

#include <iostream> 
using namespace std; 

//scanning program 
int linearsearch (int A[] , int z, int n, int srchElement) { 
    for (int z = 0; z < n; z++) { 
     if (A[z] == srchElement) { 
      return z; 
     } 
    } 
    return -1; 
} 

//main program 
int main() { 
    int i, n, A[1000], z; 
    //asking for size of array 
    cout << "give size of the array needed to be scanned: "; 
    cin >> n; 
    cout << endl; 
    if (n > 999) { 
     cout << "invalid value"; 
     return -1; 
    } 
    //making sure of the size of the array 
    cout << "enter " << n << " integers: "; 
    //asking for the array 
    for (i = 0; i < n; i++) { 
     cin >> A[i]; 
    } 
    int srchElement, index; 
    do { 
     cout << endl << "enter element to search (-1 to exit): "; 
     //srchElement is defined here 
     cin >> srchElement; 
     if (srchElement == -1) break; 
     index = linearsearch(A, n, srchElement, z); 
     //calling thscanning function 
     if (index == -1) { 
      cout << srchElement << " not present" << endl; 
     } 
     //outputting the results of the scan 
     else { 
      cout << srchElement << " present " << index << endl; 
     } 
    } while (true); 
    return 0; 
} 
+2

這段代碼是絕對不可讀的;你應該格式化它。 – tenfour 2014-10-27 10:38:03

回答

0

這是正確格式化您的搜索功能:

int linearsearch (int A[] , int z, int n, int srchElement) 
{ 
    for (int z = 0; z < n; z++) 
    { 
     if(A[z] == srchElement) 
      {return z;} 
    } 
    return -1; 
} 

,這裏是你怎麼稱呼它:

index=linearsearch(A, n, srchElement, z); 
  • 您在呼叫中傳遞價值z。它是單元化的,並且在main()或函數中什麼都不做。
  • 您以錯誤的順序將參數傳遞給函數。您的位置:
    • 傳遞n(從main())到未使用z
    • 搜索的未初始化z(從main()
    • 傳遞您正在尋找爲n(數組大小)的元素。 (這很可能會導致超出界限的錯誤,例如如果搜索-1

試試這個:

int linearsearch (int A[], int n, int srchElement) 
{ 
    for (int z = 0; z < n; z++) 
    { 
     if(A[z] == srchElement) 
      {return z;} 
    } 
    return -1; 
} 

,這裏是你怎麼稱呼它:

index=linearsearch(A, n, srchElement); 
1

你的參數linearsearch不按正確的順序 - 你逝去的n到未使用z參數。根據您目前的功能,你應該把它想:

index=linearsearch(A, 8675309, n, srchElement); 

我建議你擺脫z作爲參數,那麼你就不會需要一個值傳遞給它。

另請注意:空格和縮進不會使您的程序運行速度變慢,但它們確實使它更容易閱讀。

1

函數定義中的參數順序與函數調用中的順序不同。 應該是這樣(線4號):

int linearsearch (int A[] , int n, int srchElement, int z) 
0

你直接的問題:爲暗斑,這叫:

index=linearsearch(A, n, srchElement, z); 

不匹配的聲明用C

int linearsearch (int A[] , int z, int n, int srchElement) 

函數的參數++的位置:只是因爲最後調用參數和第二個聲明參數都稱爲z並不意味着什麼。

現在,有風格的幾個地方問題:

  1. 這種功能是擺在首位的風險

    int linearsearch (int[],int,int,int) 
    

    因爲它依賴於你記住正確的順序爲最後三個整數參數。如果您必須做到這一點,你應該多加小心給他們各具特色的名字,很清楚哪個是哪個,並保持秩序跨職能家屬一致。

    它的更好,如果可能的話,可以幫到你的編譯器幫幫忙,方法是給參數不同類型(或枚舉,或其他),或將它們分組到結構。

    例如,使用std::vector<int>,而不是你的陣列有效組int A[]int n一起在一個對象,所以他們無法獲得同步,並且n不能與其他整數左右浮動

  2. 感到困惑

    你不應該首先通過z。您立即與環中本地int z隱藏它,所以它不能做任何事情。從聲明和通話中刪除它。這種簡化足以解決你的錯誤。


你的第二個問題是,代碼是醜陋。它格式不好,很難閱讀,而且這使得發現錯誤更加困難。儘量讓你的代碼簡單易讀:事情出錯的機會較少,並且在發生問題時更容易看到問題。

你的第三問題是代碼。其中大部分工作可以使用標準庫工具完成(使代碼更簡單),這些工具本身已經過很好的測試,並且通常都有精心設計的接口。首先使用它們,並在必要時進行更換。