Reddevil
Reddevil

Reputation: 125

How to pre-define an array in c?

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

//so here the compiler says that expected error without giving me what the error actually is. and i suspect because of the bad definition of the vector that i have at the very beginning of my code. }

Yeah, so as i stated in my comment the problem is with the c preprocesses as its referred to here in stack overflow. is my predefinition for the array Hey wrong? and why?

Upvotes: 1

Views: 3243

Answers (3)

example: an array containing 4 integer values of type int called abc could be represented as: int abc [4]; by default, regular arrays of local scope (in this example, those declared inside the function) are ignored. But the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example: int abc [4] = { 1, 2, 47, 50, -371 }; in other case you can let empty the brackets so you can initialize them when the program is running e.g. int abc [] ; or you can use a matrix like :int abc [] []; with X lines & Y columns.

Upvotes: -1

Zorgatone
Zorgatone

Reputation: 4283

If you really want to, you can do something like this.

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

Upvotes: 1

tdao
tdao

Reputation: 17668

Usually this is how to predefine an array in C, omit the size and provide the values in brackets. Eg,

const float Hey[] = {0.9501, 0.2311, 0.6068 };

Upvotes: 2

Related Questions