# using arrays with functions

This is a discussion on using arrays with functions within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> float average(int values [], int array_size); int sum = 0; int i = 0; int main (void) ...

1. ## using arrays with functions

Code:
```#include <stdio.h>

float average(int values [], int array_size);

int sum = 0;
int i = 0;

int main (void)

{

float f;

int integers[5] = {0};

printf("Enter five numbers\n");

while (scanf("%d", &integers[i])!=EOF)
{
i++;
sum += integers[i];

if (i == 5)
break;
}

f = average(integers, i);

printf("The average of the numbers you entered is %.2f\n",f);

return (int) f;
}

float average(int values [], int array_size)

{
return (float) sum/array_size;

}```
Why is the average always coming out to be some crazy number?

2. You've got plenty of problems to play with :-)
Code:
```#include <stdio.h>

float average(float sum, int array_size);

int main (void)
{
int i = 0;
float f, sum = 0.0f;
float integers[5] = {0.0f};

printf("Enter five numbers\n");

while (scanf("%f", &integers[i])!=EOF)
{
sum += integers[i++];

if (i == 5)
break;
}

f = average(sum, i);

printf("The average of the numbers you entered is %.2f\n",f);

return 0;
}

float average(float sum, int array_size)
{
return sum/array_size;
}```

3. Without bothering to go through your code (since I'm both at work and very lazy), could I suggest that you write a short loop to print the contents of the array, this may give you a clue as to where your problem is. Either that, or wait until some magnanimous code god comes along and spots your issue straight away, and saves you the trouble of doing your own logical debugging.