2013-03-13 102 views
0

我想解決一個練習,要求一個數組並計算次數的數量等於2個數字鄰居的平均次數。我有一個愚蠢的錯誤,我不明白,這讓我很頭疼。函數與指針

#include<stdlib.h> 
#include<stdio.h> 
#include<string.h> 

#define N 5 

void neighbors(int *arr, int dim, int *equal){ 

int *t; 
int *equal=0; 
int i; 

for(i=1;t[i]<=5;i++){ 
    if(((t[i-1]+t[i+1])/2)==t[i]) 
     equal++; 
    else 
     continue; 
} 
printf("Elements -> %d", &equal); 
} 

int main(){ 


int array[N]={1,2,3,9,10}; 
int aux; 

int neighbors(*array,N,&aux);  // here it says "expected a ')' and "too many       //           initializers" 


system("pause"); 
} 

你能給我一個提示嗎? 謝謝!

+0

非常感謝mistapink! – falkon21 2013-03-13 23:39:20

+1

這段代碼中有更多的錯誤;)對於i = 5,t [i + 1]不會給你所期望的結果。重新定義平等也會給你帶來錯誤。最後增加指針相等但不是值。這些是你可能想要重新考慮的事情。我沒有提到你的頭文件不正確,也不希望使用std :: cout而不是printf(帶有錯誤的參數)。看看http://codepad.org/MRh6ckbL它可能會幫助你。 – mistapink 2013-03-13 23:39:24

回答

1

此代碼中還有很多錯誤: t[i+1] for i=5會給你沒有你可能期望的結果。重新定義equal也會給你錯誤。最後增加指針equal但不是值。這些是你可能想要重新考慮的事情。我沒有提到你的標題不正確,也不希望使用std::cout而不是printf(btw:帶有錯誤的參數)。看看http://codepad.org/MRh6ckbL它可能會幫助你。

2

掉落的intint neighbors(*array,N,&aux);,並更改爲neighbors(array,N,&aux);因爲要傳遞一個指針數組不是陣列的第一構件的值。

您也重新聲明,在第11行

而且學會等於閱讀你的編譯器警告/錯誤!

+0

它表示智能感知:類型「int」的參數與類型爲「int *」的參數不兼容 – falkon21 2013-03-13 23:29:01

1

只是這樣做。

neighbors(array,N,&aux); 

並注意aux是不確定的。

+1

'[]'在這裏做什麼? – 2013-03-13 23:37:32

+0

誰的[]?本Voigt – falkon21 2013-03-14 00:57:05