2011-10-23 18 views
0

這是main調用的代碼,用C語言中的選擇排序對數組進行排序。我在main中打開一個文件,並將前十個整型放入一個數組中,將第11個整型放入一個變量中,然後調用一堆簡單函數。整件事重複了三次。對於我的測試文件,最後兩個迭代具有正確的打印排序,但第一個以0開頭,但我的數組中沒有0。它也會丟棄最後一個int。對於這個選擇排序,爲什麼當我的數組中沒有使用0時,第一個數字是0? (in c)

在此先感謝您的幫助!

這裏是我的代碼:

void sortme (int arry [], int last_int) 
{ 
    int temp; 
    int smallest_int; 
    int current_int; 
    int target_searcher; 
    int numPrinted; 

    for(current_int = 0; current_int < last_int; current_int++) 
     { 
      smallest_int = current_int; 

      for(target_searcher = current_int + 1; target_searcher <= last_int; target_searcher++) 
       if(arry[target_searcher] < arry[smallest_int]) 
        smallest_int = target_searcher; 

      temp = arry[current_int]; 
      arry[current_int] = arry[smallest_int]; 
      arry[smallest_int] = temp; 
     }                  //end outter loop 

    numPrinted = 0; 

    printf("\nThe sorted array is: "); 
    for(current_int = 0; current_int < SIZE; current_int++) 
     { 
      printf("%4d", arry[current_int]); 
      if(numPrinted < COUNT) 
       numPrinted++; 
      else 
       { 
        printf("\n"); 
        numPrinted = 0; 
       } 
     } 
    printf("\n"); 

    return; 
} 

這裏是我的參考輸出(大部分的東西main.c中被commenetd出):

The file opened. 

Scanned into a[] and target is 33 

ARRAY[1] 
The contents in the array are: 40 32 57 27 67 6 3 89 2 99 
The sorted array is: 0 2 3 6 27 32 40 57 67 89 
The value searched, 33, was not found. 

Scanned into a[] and target is 3 

ARRAY[2] 
The contents in the array are: 86 43 89 32 45 12 1 58 98 4 
The sorted array is: 1 4 12 32 43 45 58 86 89 98 
The value searched, 3, was not found. 

Scanned into a[] and target is 11 

ARRAY[3] 
The contents in the array are: 1 2 3 4 5 6 7 8 9 10 
The sorted array is: 1 2 3 4 5 6 7 8 9 10 
The value searched, 11, was not found. 
Closing the file. 
The file closed. 
+1

你試過調試過嗎?嘗試一個非常小的數組(例如,只有1個條目)。通過你的'for(current_int = 0; current_int

+0

@Raymond是對的:用調試器進行調試和進入代碼絕對是**強制**。如果你不知道如何,你必須學習。如果你知道如何,並選擇不去,你錯了。 – abelenky

+0

調試與在printf語句中添加以幫助查看程序粘在哪裏相同?我們還沒有在課堂上進行調試(我在編程方面很新穎)。 – Piseagan

回答

1

你讓target_searcher等於last_int,同時搜索最小值。所以,有時你會在數組中注入一個隨機小數值(並且混淆了不屬於你的內存)。當然,我假設last_int是數組的長度。

當您處理「僅有效索引」時,範圍從0len-1。你可以看到長度爲1的數組(如果你再次懷疑)。由於只有1個元素,因此它的編號爲array[0]array[len-1]。儘管如此,通常習慣於將參數作爲數組和長度傳遞,而不是數組和索引的最後一個有效元素的數組和長度。這更自然。比方說,你有一個包含len1len2兩個塊的大陣列,並且有一個功能可以對這些分區進行一些操作。如果使用長度參數,可以使用:

processBlock(arr, len1); 
processBlock(arr + len1, len2); 

如果你使用最後一個有效的索引,就不會有你必須處理所有這些+/-1條款。因此,它可以是:

processBlockIdx(arr, len1 - 1); 
processBlockIdx(arr + len1, len2 - 1); 

或:

processBlockIdx(arr, lastIdx1); 
processBlockIdx(arr + lastIdx1 +1, lastIdx2 - lastIdx1 - 1); 

至於你的第二個問題的答案:是的,這個問題是通過訪問你的數組邊界之外的元素引起的。由於C沒有檢查數組邊界的安全網絡,所以這樣的錯誤通常會表現爲結果中出現的無法解釋的值,或者更糟糕的是應用程序崩潰。在某些情況下,你沒有那麼幸運,並且它在你的程序的一個完全不相關的部分顯示出問題。所以,最好是非常有效,肯定有關數組元素的訪問。

+0

我將last_int作爲數組大小傳遞給10,所以我應該將它傳遞爲小於數組大小的1(傳給last_int的9)? – Piseagan

+0

我試過了,它工作!我會嘗試一些我必須寫的其他功能。是否它注入一個隨機數的原因是因爲我已經超出了數組邊界(即,我只在一個只有十個元素的數組的元素11中)? – Piseagan

+0

@Piseagan答案會有點長,所以我正在編輯我的答案來詳細說明。 – vhallac

相關問題