为什么会报错“array subscript is not a integer”
#include <stdio.h>#include <stdlib.h>
float i,average,max,min,result;
float calculate (int a[])
{
for(i=1;i<10;i++)
{
average=0;
average+=a;
max=min=a;
if (min>a)
{
min=a;
}
else if(max<a)
{
max=a;
}
}
average/=10;
return average;
}
int main()
{
int a= {89,67,98,67,98,67,56,79,54,34};
result=calculate (a);
printf("the average is %.21f\nthe max is %f.21\nthe min is %f.21",result,max,min);
}
array subscript is not a integer,数组的下标不是整形。你的i是float类型的,换成int就可以了 #include <stdio.h>
#include <stdlib.h>
float average,max,min,result;
float calculate (int a)
{
int temp=a;
max=min=(float)temp;
for(int i=0;i<10;i++)
{
average=0;
temp = a ;
average=average+(float)temp;
if (min>temp)
{
min=temp;
}
else if(max<temp)
{
max=temp;
}
}
average/=10;
return average;
}
void test()
{
int a= {89,67,98,67,98,67,56,79,54,34};
result= calculate (a);
printf("the average is %.21f\nthe max is %f.21\nthe min is %f.21",result,max,min);
}
稍微调整了下
几个方面请注意
float i
然后你让a确实要报错了 4goodworld 发表于 2020-4-16 10:41
稍微调整了下
几个方面请注意
float i
谢谢啦 耻思lhj 发表于 2020-4-16 09:11
array subscript is not a integer,数组的下标不是整形。你的i是float类型的,换成int就可以了
谢谢
页:
[1]