erengsgs
erengsgs

Reputation: 53

Array initialization in C — "cannot convert '<brace-enclosed initializer list>' to 'float' in assignment" error

I am trying to compile a code for my simulation project and I tried to initialize and represent my rho array but I encountered the initializer error:

cannot convert 'brace-enclosed initializer list' to 'float' in assignment.

I need to assign rho[0] to the first member of the array and so on.

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

Upvotes: 3

Views: 12048

Answers (1)

kaylum
kaylum

Reputation: 14044

rho[10] = ...

That is not an initializer. It is an assignment statement. And an invalid one at that as rho[10] is a single array element.

An initializer very specifically refers to an assignment that is part of the variable declaration. So just change to:

float rho[] = { 0.1 , 0.4 , 0.5 , 0.6 , 0.7 , 0.74 , 0.78 , 0.8 , 0.85 , 0.9 } ;

Note that I have also removed the array size. Better to let the compiler work that out for you (unless you want an array larger than the number of initializer elements).

Upvotes: 6

Related Questions