2013-03-02 35 views
-1

該函數使用整數數組,數組中的元素數目爲 ,並嘗試查找 數組中的多數元素。如果存在多數元素,則將其置於 *結果中,並且函數返回true。 如果不存在多數元素,則函數返回 false。在這種情況下,不應使用*結果。在C中調試函數

我的輸出對於我正在編寫的程序無法正常工作,這是因爲我覺得這個findMajority函數。

這就是輸出應該是這樣的:http://pastebin.com/Q5ycXHrg

這是我的輸出是什麼樣子:http://pastebin.com/7P1ZTpML

這是輸入:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 
1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 
1 2 3 
1 1 1 
1 2 1 
1 2 
1 1 
2 
1 1 1 1 2 3 4 5 6 7 

下面是函數:

int findMajority(int *array, int count, int *result){ 
    int i, counter, bcount = 0, ccount = 0, candidate, j;  

    if(count == 1) { 
     *result = *array; 
     return true; 
    } 

    if(count % 2 != 0) { 
    for(i = 0; i < count; i++) { 
     if(*(array + i) == *(array + count)) { 
     counter++; 
     } 
    } 

    if(counter > (count/2)) { 
     *result = *(array + count); 
     return true; 
    } 
    else { 
     *(array + count) = 0; 
     count--; 
    } 
    } 

    for(j=0; j <= count; j += 2) { 
    if(*(array + j) == *(array + (j + 1))) { 
     *(array + (count + 1)) = *(array + j); 
     bcount++;//how many numbers on the end of the array 
    } 
    } 

    if(bcount == 1) { 
    int k = count; 
    while(*(array + k) == 0) { 
     candidate = *(array + k); 
    } 
    } 
    else 
    findMajority((array + count), count, result); 

    for(j=0; j <= count; j += 2) { 
    if(*(array + j) == candidate) { 
     ccount++; 
    } 
    } 

    if(ccount > (count/2)) { 
    *result = candidate; 
    return true; 
    } 
    else 
    return false; 
} 
+0

我在您的「問題」中看不到問題...... – 2013-03-02 01:33:45

+0

實際上這很清楚。我輸出的輸出與預期輸出相比是不正確的。 – 2013-03-02 01:36:45

+0

[C程序中的錯誤輸出]的可能重複(http://stackoverflow.com/questions/15169454/incorrect-output-in-c-program)。我知道你試圖改善你的問題以符合網站要求,但我們寧願你通過編輯你的原始問題來做到這一點(使用'edit'鏈接),而不是創建一個全新的問題。 – 2013-03-02 01:42:03

回答

1

你的函數有很多pr oblem。

  1. 沒有intialising counter你增加它
  2. array[count]檢查是否是有效的最後一個元素或array[count-1]是正確的
  3. 在這段代碼for(j=0; j <= count; j += 2){

    if(*(array + j) == *(array + (j + 1))){ 
         *(array + (count + 1)) = *(array + j); 
         bcount++;//how many numbers on the end of the array 
        }} 
    

    count= 3您正在訪問array[4]array[5]

  4. 這是一個無限循環。你沒有修改循環內的條件變量while(*(array + k) == 0) { candidate = *(array + k); }
1

我建議你學習如何使用調試器來調試你的程序。例如,下面的包裝你的代碼之後:

#include <stdio.h> 

typedef enum { false, true } boolean; 

// (((your function here))) 

int main(int argc, char* argv[]) 
{ 
    int result = 0; 
    int number = 0; 

    int test[] = { 1, 1, 1, 1, 1, 1, 1 }; 


    result = findMajority(&test[0], sizeof(test)/sizeof(int), &number); 

    printf("Result = %d, Number = %d\n", result, number); 

    return 0; 
} 

假設你把這個變成「question.c」,那麼你可以發出命令(假設你有gcc和GDB):

$ gcc -g -o question question.c 
$ gdb ./question 
(gdb) b findMajority 
Breakpoint 1 at 0x80483ea: file question.c, line 6. 
(gdb) run 
Starting program: ./question 
Breakpoint 1, findMajority (array=0xbffff4bc, count=7, result=0xbffff4d8) at question.c:6 
6  int i, counter, bcount = 0, ccount = 0, candidate, j;  

然後,您可以使用命令n進入下一行,並使用p命令打印變量以查看發生了什麼問題。例如,您可以找到Toms指出的一些問題:

39  while(*(array + k) == 0){ 
(gdb) n 
40   candidate = *(array + k); 
(gdb) n 
39  while(*(array + k) == 0){ 
(gdb) n 
40   candidate = *(array + k); 
(gdb) n 
39  while(*(array + k) == 0){ 
(gdb) n 

有您的無限循環。

(gdb) p counter 
$3 = -1207959944 

還有你的未初始化的計數器。

學習編程的一部分是找出確定出錯的策略。有些人喜歡使用基於文本的調試器,如gdb。一些人喜歡圖形調試器,就像你可以在Eclipse CDT中找到的一樣。有些人在其代碼中放入printf()聲明。

一旦你真的很好,就像湯姆斯一樣,你可以閱讀它並解決問題。 ;-)