ere*_*sgs 2 c arrays initialization
我正在尝试为我的模拟项目编译代码,并尝试初始化并表示我的rho数组,但遇到了初始化错误:
无法在赋值中将“大括号封闭的初始化列表”转换为“浮动”。
我需要分配rho[0]给数组的第一个成员等等。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float lambda; // ratio of customers and time
float N; // mean number of customers in the system
float Q; // mean number of customers in queue
float res_time; // response time
float mu; // service rate , assuming mu = 10 customers/s for this assignment
int i;
float rho[10]; // array for rho values
rho[10] = { 0.1, 0.4, 0.5, 0.6, 0.7, 0.74, 0.78, 0.8, 0.85, 0.9 };
printf("\nPlease enter the mu : ");
scanf("%f", &mu);
FILE *f1, *f2, *f3, *f4, *f5, *f6, *f7, *f8, *f9, *f10;
if (i == 0)
{
N = rho[i] / (1 - rho[i]);
lambda = rho[i] * mu;
Q = (i * rho[i]) / (1 - rho[i]);
res_time = N / lambda;
printf("%.4f \t\t %.4f \t\t %.4f \t\t %.4f \n", rho[i], N, Q, res_time);
f1 = fopen("NvsRho[0.1].txt", "w");
if (f1 == NULL)
{
printf("Cannot open file.\n");
exit(1);
}
fprintf(f1, "RHO \t\t N \n--- \t\t ---\n");
fprintf(f1, "%.4f \t\t %.4f \n", i, N);
fclose(f1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
rho[10] = ...
那不是初始化程序。它是一个赋值语句。和一个无效的,因为rho[10]它是单个数组元素。
初始化器非常具体地指代作为变量声明一部分的赋值。所以只需更改为:
float rho[] = { 0.1 , 0.4 , 0.5 , 0.6 , 0.7 , 0.74 , 0.78 , 0.8 , 0.85 , 0.9 } ;
Run Code Online (Sandbox Code Playgroud)
请注意,我还删除了数组大小。最好让编译器为你解决这个问题(除非你想要一个大于初始化元素数量的数组)。