#include <stdio.h>
#include <math.h>
#define Hey {0.9501, 0.2311, 0.6068, 0.4860, 0.8913, 0.7621, 0.4565, 0.0185, 0.8214, 0.4447, 0.6154, 0.7919, 0.9218, 0.7382, 0.1763, 0.4057, 0.9355, 0.9169, 0.4103, 0.8936, 0.0579, 0.3529, 0.8132, 0.0099, 0.1389, 0.2028, 0.1987, 0.6038, 0.2722, 0.1988, 0.0153, 0.7468, 0.4451, 0.9318, 0.4660, 0.4186, 0.8462, 0.5252, 0.2026, 0.6721, 0.8381, 0.0196, 0.6813, 0.3795, 0.8318, 0.5028, 0.7095, 0.4289, 0.3046, 0.1897, 0.1934, 0.6822, 0.3028, 0.5417, 0.1509, 0.6979, 0.3784, 0.8600, 0.8537, 0.5936, 0.4966, 0.8998, 0.8216, 0.6449, 0.8180, 0.6602, 0.3420, 0.2897, 0.3412, 0.5341, 0.7271, 0.3093, 0.8385, 0.5681, 0.3704, 0.7027, 0.5466, 0.4449, 0.6946, 0.6213, 0.7948, 0.9568, 0.5226, 0.8801, 0.1730, 0.9797, 0.2714, 0.2523, 0.8757, 0.7373, 0.1365, 0.0118, 0.8939, 0.1991, 0.2987, 0.6614, 0.2844, 0.4692, 0.0648,0.9883}
float average(float Hello[]){
int i;
float sum;
for (i=0; i<100;i++) {
sum+= Hello[i];
}
return sum/100;
}
int main(){
printf("%f\n",average(Hey));
//所以这里编译器说出了预期的错误而没有告诉我实际上是什么错误。我怀疑是因为我在代码的最开始时对矢量的定义不好。 }
是的,正如我在评论中所述,问题在于c预处理,因为它在堆栈溢出中引用。是我对阵列的预定义嘿错了吗?为什么?
答案 0 :(得分:2)
通常这是如何在C中预定义数组,省略大小并在括号中提供值。例如,
const float Hey[] = {0.9501, 0.2311, 0.6068 };
答案 1 :(得分:0)
如果你真的想,你可以做这样的事情。
#include <stdio.h>
#include <math.h>
#define HEY_INIT { 0.9501, 0.2311, 0.6068, 0.4860, 0.8913, 0.7621, 0.4565, 0.0185, 0.8214, 0.4447, 0.6154, 0.7919, 0.9218, 0.7382, 0.1763, 0.4057, 0.9355, 0.9169, 0.4103, 0.8936, 0.0579, 0.3529, 0.8132, 0.0099, 0.1389, 0.2028, 0.1987, 0.6038, 0.2722, 0.1988, 0.0153, 0.7468, 0.4451, 0.9318, 0.4660, 0.4186, 0.8462, 0.5252, 0.2026, 0.6721, 0.8381, 0.0196, 0.6813, 0.3795, 0.8318, 0.5028, 0.7095, 0.4289, 0.3046, 0.1897, 0.1934, 0.6822, 0.3028, 0.5417, 0.1509, 0.6979, 0.3784, 0.8600, 0.8537, 0.5936, 0.4966, 0.8998, 0.8216, 0.6449, 0.8180, 0.6602, 0.3420, 0.2897, 0.3412, 0.5341, 0.7271, 0.3093, 0.8385, 0.5681, 0.3704, 0.7027, 0.5466, 0.4449, 0.6946, 0.6213, 0.7948, 0.9568, 0.5226, 0.8801, 0.1730, 0.9797, 0.2714, 0.2523, 0.8757, 0.7373, 0.1365, 0.0118, 0.8939, 0.1991, 0.2987, 0.6614, 0.2844, 0.4692, 0.0648, 0.9883 }
static const float Hey[] = HEY_INIT; // global array
float average(float Hello[]) {
int i;
float sum;
for (i = 0; i < 100; i++) {
sum += Hello[i];
}
return sum / 100;
}
int main(void) {
printf("%f\n", average(Hey));
return 0;
}
答案 2 :(得分:-1)
示例:包含4个int类型的整数值的数组(称为abc)可以表示为:int abc [4]; 默认情况下,忽略本地范围的常规数组(在此示例中,在函数内声明的那些)。 但是,通过将这些初始值括在大括号{}中,数组中的元素可以在声明时显式初始化为特定值。例如: int abc [4] = {1,2,47,50,-371}; 在其他情况下,您可以清空括号,以便在程序运行时初始化它们,例如int abc [];或者您可以使用如下矩阵:int abc [] []; X线&amp; Y列。