# Thread: Calculating the average of average of a series of numbers using functions

1. ## Calculating the average of average of a series of numbers using functions

Hello, I have written this programme to calculate the average of a series of numbers. It works when I do not use functions but doesn't when I use functions. Could someone help me find out why using functions is not correct for this task ?

Code:
```CODE WITH FUNCTIONS THAT DOESN'T WORK
//This programme is just a modification of chapter15_exercice3.c such that the task is accomplished using functions

#include<stdio.h>

float average(int n, int numbers[n]) //a function that returns the average of the elements stored in a array
{

int i, sum = 0;
for (i = 0; i < n; i++)
{
sum += numbers[i];
}

return sum / n;
}

main()
{
int n,i;
printf("Please enter the number of numbers you wish to calculate the average\n");
scanf("%d", &n);
int numbers[n];
for (i = 0; i < n; i++)
{
printf("Enter number %d\n",i+1);
scanf("%d", &numbers[i]);
}

printf("Here are the numners that you've enter\nThe average of the following numbers are to be calculated\n");
for (i = 0; i < n; i++) //just to display to the user the numbers that they have entered
{
printf("%d, ", numbers[i]);
}

printf("\n");

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

return 0;

}```
Result:
chapter15_exercice3.c: In function ‘main’:
chapter15_exercice3.c:40:1: warning: passing argument 2 of ‘average’ makes pointer from integer without a cast [enabled by default]
printf("The average of the numbers that you've entered is %.2f\n", average(n, numbers[n]));
^
chapter15_exercice3.c:5:7: note: expected ‘int *’ but argument is of type ‘int’
float average(int n, int numbers[n]) //a function that returns the average of the elements stored in a array
^

CODE WITHOUT FUNCTIONS THAT WORKS
Code:
```#include<stdio.h>
/*
float average(int n, int numbers[n])
{

int i, sum = 0;
for (i = 0; i < n; i++)
{
sum += numbers[i];
}

return sum / n;
*/

main()
{
int n,i;
printf("Please enter the number of numbers you wish to calculate the average\n");
scanf("%d", &n);
int numbers[n];
for (i = 0; i < n; i++)
{
printf("Enter number %d\n",i+1);
scanf("%d", &numbers[i]);
}

printf("Here are the numners that you've enter\nThe average of the following numbers are to be calculated\n");
for (i = 0; i < n; i++)
{
printf("%d, ", numbers[i]);
}

printf("\n");

int sum = 0;
float average;
for (i = 0; i < n; i++)
{
sum += numbers[i];
}
average = sum / n;
printf("The average of the numbers that you've entered is %.2f\n", average);

return 0;

}```
Result:
Please enter the number of numbers you wish to calculate the average
5
Enter number 1
4
Enter number 2
4
Enter number 3
4
Enter number 4
4
Enter number 5
4
Here are the numners that you've enter
The average of the following numbers are to be calculated
4, 4, 4, 4, 4,
The average of the numbers that you've entered is 4.00 2. It's not such a big deal, first change the prototype of the function:
Code:
`int numbers[n]`
This is an actual integer, not an array of integers, since n is an expected number.
It should actually be:
Code:
`int numbers[]`
Because when calling the function you'll be giving it the length.

Now the thing is that when you call the function numbers[n] is not an array but an integer, you should be calling the function with the actual array that is:
'numbers' 3. Note that on line 43 you have integer division, so you will never get rational part with it. 4. Thanks @Fiskker and @vart for enlighting me on that, my code now works  5. Originally Posted by Fiskker It's not such a big deal, first change the prototype of the function:
Code:
`int numbers[n]`
This is an actual integer, not an array of integers, since n is an expected number.
It should actually be:
Code:
`int numbers[]`
Because when calling the function you'll be giving it the length.
This is actually not a problem. numbers is a pointer to int in all these cases:
Code:
```float average(int n, int numbers[n]);
float average(int n, int numbers[]);
float average(int n, int *numbers);``` 6. Thanks @christop for enlighting me on function prototypes, i guest i did not quit unerstand function prototype very well. Thanks man. 7. Originally Posted by christop This is actually not a problem. numbers is a pointer to int in all these cases:
Code:
```float average(int n, int numbers[n]);
float average(int n, int numbers[]);
float average(int n, int *numbers);```
It's not a problem per se, but why would you put the length of the array in there, unnecessary, specially because you pass that length in the first parameter, right? 8. Originally Posted by christop This is actually not a problem. numbers is a pointer to int in all these cases:
Code:
```float average(int n, int numbers[n]);
float average(int n, int numbers[]);
float average(int n, int *numbers);```
But in the case of the OP's code, it is a problem, since 'n' is not a valid identifier in the scope of the function prototype.

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

int sum_array(int numbers[n], int total);

int main(void)
{
int i,sum,n = 10;
int numbers[n];

for(i = 0; i < n; i++)
numbers[i] = i+1;

sum = sum_array(numbers,n);

printf("The sum is %d\n",sum);

return 0;
}

int sum_array(int numbers[n], int total)
{
int i,sum=0;

for(i = 0; i < total; i++)
sum += numbers[i];

return sum;
}```
Code:
```/*
main.c|3|error: 'n' undeclared here (not in a function)
*/``` 9. Originally Posted by Matticus But in the case of the OP's code, it is a problem, since 'n' is not a valid identifier in the scope of the function prototype.

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

int sum_array(int numbers[n], int total);

int main(void)
{
int i,sum,n = 10;
int numbers[n];

for(i = 0; i < n; i++)
numbers[i] = i+1;

sum = sum_array(numbers,n);

printf("The sum is %d\n",sum);

return 0;
}

int sum_array(int numbers[n], int total)
{
int i,sum=0;

for(i = 0; i < total; i++)
sum += numbers[i];

return sum;
}```
Code:
```/*
main.c|3|error: 'n' undeclared here (not in a function)
*/```
Where did you get that code from? It's not the OP's code.

OP's code has this function declaration:
Code:
`float average(int n, int numbers[n])`
Which is valid and self-documenting. 10. Originally Posted by christop Where did you get that code from? It's not the OP's code.

OP's code has this function declaration:
Code:
`float average(int n, int numbers[n])`
Which is valid and self-documenting.
Touche - I neglected to notice that 'n' was its own parameter. (My example was intended to be a mock-up, but an invalid one as it turns out.) Popular pages Recent additions [code] 