I have this code for a university job but I have problems when calculating the average of the data that was entered into the array. Make an impression of the arrangement in the average function and it throws possibly random values and I do not understand why. I appreciate if you can help me solve the problem. Thanks
/ ********************************************* ******** * Problem 2: coding a function and giving use * * Input: Arrangement of 1D int type, array size * * Output: Average data * ********************************************** **** /
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//prototipo
int tamano_arreglo();
int llenar_arreglo(int tam);
float promedio (int *a, int tam);
//main
int main () {
int tam,arreglo = 0;
int *a;
float prom = 0;
tam = tamano_arreglo();
arreglo = llenar_arreglo(tam);
a = &arreglo;
prom = promedio(a,tam);
printf("El promedio de los datos ingresados es: %f \n",prom);
}
//desarrollo prototipos
int tamano_arreglo(){
int tam = 0;
printf("Ingrese el tamano que desea tener el arreglo \n");
scanf("%d",&tam);
return tam;
}
int llenar_arreglo(int tam){
int i = 0;
int a[tam];
for (i=0; i<tam;i++){
printf("ingrese el valor %d:\n",i+1);
scanf("%d",&a[i]);
}
for (i=0; i<tam;i++){
printf("Posicion # %d: %d\n",i,a[i]);
}
return *a;
}
float promedio (int *a, int tam)
{
int i=0;
float suma, promedio = 0;
for(i=0; i<tam; i++)
{
suma += a[i];
}
for (i=0; i<tam;i++)
{
printf("Posicion # %d: %d\n",i,a[i]);
}
printf("la suma es %f \n",suma);
promedio = suma / tam;
return promedio;
}