除了线性搜索之外还运行其他所有内容..当我运行它时,它说我在这里遗漏了一些东西(错误C2143:语法错误:缺少';'在'type'之前>> ;> int result=linearsearch(lego,15,val);
#include<stdio.h>
void display(int[] ,int );
float average(int[], int );
int linearsearch (int lego[], int b, int val);
int main() // main() must return int
{
float avg;
int val;
int lego[15]= {2,4,6,8,10,12,14,16,18,20,22,24,26,28,30};
char muzik[20]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'};
float blu[10]= {1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0,2.1}; // float values not chars
display(lego,15);
average(lego,15);
avg=average(lego,15);
printf("Average = %.2f\n",avg); // added newline
printf("Please enter a number:\n");
scanf("%d", &val);
int result=linearsearch(lego,15,val);
if(result>=0) {
printf("the number is : %d" ,lego[result]);
} else {
printf("You have entered a wrong number %d\n",val);
}
}
void display(int lego[], int Se) {
int w;
for(w=0; w<Se; ++w) {
printf("%d ",lego[w]);
}
printf("\n"); // added newline
}
float average(int lego[], int b) {
int s, sum=0;
float avg;
for(s=0; s<b; ++s) {
sum+=lego[s];
}
avg=(float)sum/b; // (float) cast forces floating-point division
return avg;
}
int linearsearch (int lego[], int b, int val) {
int i=0;
for(i=0; i<b; ++i) {
if(val==lego[i]) {
return i;
}
}
return -1;
}
答案 0 :(得分:2)
这段代码为我编译,当我运行它时,我得到了预期的行为。你看到了什么信息?
修改强>
好吧,我想我明白了。您有C ++代码,并且您正在将其编译为C代码。在C代码中,必须在函数的开头声明所有变量。或者,将扩展名更改为.cpp或将结果声明移到val下面的行,稍后再更改:
float avg;
int val;
int result;
...
result = linearsearch (lego, 15, val)