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.

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.

This question has already been answered. Start a new discussion instead.