Array storage failure average function

0

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;

}
    
asked by Giovanny Rodriguez 09.09.2017 в 01:17
source

1 answer

0

The error you had was that you printed the arrangement twice, and when you went through it you were not saving the sum, then you just referenced the array (not added) that's why we went to odd operations

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//prototipo

int tamano_arreglo();
float llenar_arreglo(int tam);
//main

int main () {

int tam,arreglo = 0;
int *a;
float prom = 0;
tam = tamano_arreglo();
prom = llenar_arreglo(tam);
printf("El promedio de los datos ingresados es: %f \n",prom);


}

//desarrollo prototipos
//asigna el tamaño del arreglo
int tamano_arreglo(){

int tam = 0;
printf("Ingrese el tamano que desea tener el arreglo \n");
scanf("%d",&tam);
return tam;


}
//obtiene el tamaño de la funcion tamaño de arreglo
float llenar_arreglo(int tam){
 float suma, promedio = 0;

int i = 0;
int a[tam];

//pide los valores a promediar

for (i=0; i<tam;i++){

    printf("ingrese el valor %d:\n",i+1);

    scanf("%d",&a[i]);
 }
 //imprime el arreglo y va sumando los datos introducidos
 for (i=0; i<tam;i++){
    printf("Posicion # %d: %d\n",i,a[i]);
        suma += a[i];
 }
//imprime la suma
 printf("la suma es %f \n",suma);
//retorna al promedio, la suma de los datos introducidos / datos intoducidos
promedio = suma / tam;
return promedio;

 }
    
answered by 09.09.2017 в 06:58