# Thread: Functions with array arguments

1. ## Functions with array arguments

I am trying to make some functions that take an array of n numbers and do certain things to them (like getting the sum, calculate statistical deviation, etc...)

Right now, I have:
Code:
```int n;
int main(void)
{
printf("How many numbers?:");
scanf("%f", n);
float numbers[n];
...
...
...
sum=sum(numbers[n]);
printf("Sum is:%f", sum);
}

float sum(float numbers[n])
{
int x;
float sum;
for(x=0; x==n; ++x)
{
sum=numbers[x]+sum;
}
return sum;
}```
But that isn't working. What's the problem?

2. Make that your function accepts a float pointer. That'll make it work.

3. What do you mean?

4. Code:
```float sum(float* numbers)
{
int x;
float sum;
for(x = 0; x == n; ++x)
{
sum = numbers[x] + sum;
}
return sum;
}```

5. Variable length arrays are were only introducted in the C99 standard, as with mixed declarations.

Naturally you'll have to include the length of the array as an argument to the function, since otherwise you'll have no way of knowing when it ends.

Ah, I see you are using globals. Disregard the above then, and continue on with your bad programming habits.
[/edit]

Quzah.

6. you need to initialize sum to zero aswell

7. better this way:
Code:
```float sum(float* numbers, int count)
{
int x;
float sum = 0.0;
for(x = 0; x < count; ++x)
{
sum += numbers[x];
}
return sum;
}```

Popular pages Recent additions