0

我正在尝试解决一个要求数组并计算该数字等于两个数字邻居平均值的次数的练习。我遇到了一个我无法理解的愚蠢错误,这让我很头疼。

#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");
}

你能给我一个提示吗?谢谢!

4

3 回答 3

2

删除intatint neighbors(*array,N,&aux);并将其更改为,neighbors(array,N,&aux);因为您传递的是指向数组的指针,而不是该数组的第一个成员的值

您还在第 11 行重新声明了 equal 。

并学习阅读您​​的编译器警告/错误!

于 2013-03-13T23:26:53.397 回答
1

就这样就行了。

neighbors(array,N,&aux);  

请注意,这aux是未定义的。

于 2013-03-13T23:28:14.660 回答
1

There are many more errors in this code: t[i+1] for i=5 will give you not the result you might expect. Redefining equal will also give you errors. And finally to increment the pointer equal but not the value. These are things you might want to rethink. I did not mention that your headers are not correct nor that you might want to use std::cout and not printf (btw: with the wrong parameter). Have a look at http://codepad.org/MRh6ckbL it might help you.

于 2013-03-13T23:42:23.747 回答