I cant get my function code to work. Its should return an average of however many numbers are entered, but it always returns 775863 no matte what... can anyone tell me whats wrong?

#include <stdio.h>
int avg(int av[], int y, int x, int z);
int main(){
    
    int x=0;
    int av[50];
    int y=0;
    int z=0;
    printf("Enter the number or numbers to average\n\n");
    scanf("%d", &x);
    do
    {
        y++;
        printf("\n");
        scanf("%d", av[x]);
    }
    while (y<x);
    printf("\n\n\n%d", avg(av, y, x, z));


return 0;}

int avg(int av[], int y, int x, int z)
{
    y=0;
    do
    {
        y++;
        z = z+av[y];
    }
    while (y<x);
return z/x;
}

>> scanf("%d", av[x]);

replace variable x with y.

there is no need to pass variables y or z to function avg. Just make them local variable in that function.

I changed that and now it just freezes after 2 inputted numbers...

Follow your values with a couple of test printf() statements to help you find the errors! Also use scanf("%d", &av[y]); Your function avg() should really calculate and return a double.

>> scanf("%d", &av[y]);

you have to pass a pointer to scanf() as shown above

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.