2011-05-25 100 views
1

我需要編寫一個函數,如果它發現數組成員之間的差異將返回true。函數搜索數組成員之間的差異

我的代碼是:

int func1(int *str) 
{ 
    int i; 
    for(i=0;i<*(str+i);i++) { 
     if(*(str+i) == *(str+i+1)) 
     { 
      return 1; 
     } 
    } 
    return 0; 
} 

我有一個指針來實現它。

上面的代碼不起作用(邏輯上)。

任何人都可以幫忙嗎?

UPDATE:

我已經改變了我的代碼如下:

int func1(int *str) 
{ 
    int i,temp=0; 
    for(i=0;i<10-1;i++) { 
     if(*(str+i) == *(str+i+1)) 
     { 
      temp++; 
      if(temp == 10) 
      { 
       return 1; 
      } 
     } 
    } 
    return 0; 
} 

什麼是新代碼的問題?

+2

改變return 0這是功課? – 2011-05-25 07:50:34

+0

如果你更喜歡它,你可以使用數組符號:'str [i + 1]'和'*(str + i + 1)'在上面的代碼中完全一樣。 – pmg 2011-05-25 07:57:28

+0

這是一個保證數組是否被排序以開始?如果不是,你需要比鄰居多。 – pmg 2011-05-25 07:58:59

回答

2

這看起來像我的家庭作業,所以我不想破壞樂趣,但關於C的一件事我想提一提:有一個指向某個數組的指針並沒有告訴你關於陣列。所以你的函數將需要一個指針和第二個參數(或者可能是指向數組的最後一個元素的指針)。

+1

是的。但是,一個重要的例外是C風格的字符串。 – 2011-05-25 07:55:27

2

你的函數只需要一個數組指針,這對於比較來說似乎太少了。

您必須添加一個指定數組長度的參數,或者實現某種「策略」,例如,使用特定值終止數組。

您還應該考慮使用標準memcmp()函數。

+0

我還是不明白,代碼有什麼問題? – sdsdsdsdas 2011-05-25 08:11:36

0

我不明白的問題(目前還不清楚你想達到什麼)......

正如其他人已經說過,沒有邊界的陣列,這是不對的檢查...

這裏是你的代碼的一些其他反饋:

// func1 - consider giving functions a meaningful name, it helps people to 
// understand what the function is supposed to be doing.... 
// In this instance, it might have been helpful to identify what the expected 
// return values/inputs of the function are... 
int func1(int *str) 
{ 
    int i; 

    // Start a counter at 0, loop (adding 1) while 
    // the current value of the counter is less than, the value held in the 
    // array so, {1,2,3,4,0,7} Would terminate on the 0 
    // This: {1,20,7,14,0,7} Would also terminate on the 0 
    // This seems wrong, but again, it's unclear what you're trying to do here. 
    for(i=0;i<*(str+i);i++) { 

     // If the current element of the array 
     // is the same as the next element of the array 
     if(*(str+i) == *(str+i+1)) 
     { 
      // return 1 - two numbers next to each other in the 
      //    array are the same? 
      return 1; 
     } 
    } 

    // Either: The array contained a digit less than the counter, 
    // Or: It didn't contain two numbers that were the same next to each other. 
    // This seems a bit wrong?!? 
    return 0; 
} 

你的問題可以改進(以獲得更多有用的答案),如果你表現出你期待什麼樣的輸入返回什麼返回值。

基於此'我將需要編寫一個函數,如果它在數組成員之間發現差異,將返回true。

在僞代碼,好像你會想:

// Loop, checking we don't overflow. No point checking the last element as 
// there's nothing after it to check... 
for (count = 0 to arraysize -1) { 
    // If the current element != the next element, we've found a difference?!? 
    if(arrayElement[count] != arrayElement[count+1) { 
     return true 
    } 
} 
return false 

UPDATE:

在新的代碼...

// You're still assuming the size of 'str' 
int func1(int *str) 
{  
    int i,temp=0; 
    // Loop while i < 9, i.e. 9 times. 
    for(i=0;i<10-1;i++) {   
     if(*(str+i) == *(str+i+1))   
     { 
      temp++;    
      // Temp can never == 10, you're only going round the loop 9 times... 
      // Maybe it should be (temp == 10-1), but I don't know where the 
      // 10 comes from... 
      if(temp == 10) 
      { 
       return 1;    
      } 
     }  
    }  
    return 0; 
} 

此:

if(*(str+i) == *(str+i+1))   
{ 
    temp++;    
    // Temp can never == 10, you're only going round the loop 9 times... 
    if(temp == 10) 
    { 
     return 1;    
    } 
}  

可能是:

// return 0 (FALSE) on first difference 
if(*(str+i) != *(str+i+1))   
{ 
    return 0;    
}  

如果您在您的函數結束時return 1