One-dimensional arrays in c language

0

I have to capture the stature of 10 elements, it has to say which is the biggest and the smallest and get the average, but I have problems to get the minimum and the average.

Code

#include<stdio.h> 
#include<conio.h> 
int main() 
{ 
    int x[10]; 
    int i,j,a; 
    int max;
    int min;
    int prom,suma=0;
    /* Capturando los valores del arreglo */ 
    for (i=0; i <10; i=i+1) 
    { 
        printf("Capture valor estatura: [%d]: ", i+1); 
        scanf("%d",&x[i]); 
    } 
    /* Ordenando el arreglo */ 
    for (i=0; i <10; i=i+1) 
    for (j=0; j <10; j=j+1) 
    if ( x[i] < x[j] ) 
    { 
        a= x[i]; 
        x[i]=x[j]; 
        x[j]=a; 
    } 
    for (i = 1; i <= 10; i++)
    {

        if (x[i] > max)
        { 
            max = x[i];
        } 
        else if (x[i] < max)
        {
            min = x[i];
        } 
    }

    printf("Estatura maxima es: %d  Estatura minima es: %d \n",max,min);

    /* Desplegando el contenido del archivo */ 
    for(i=0; i <10; i=i+1) 
    printf(" %d", x[i]); 

    suma=suma+x[i];
    prom=suma/10;

    printf("\nPromedio estaturas: %d",prom);

    getch(); 
    return 0; 
}
    
asked by user109867 07.12.2018 в 19:28
source

2 answers

0

I do not think you need to order the arrangement to get the maximum, minimum and average. just using a variable that you can compare with the elements of the arrangement is enough.

I present my next code

include

include

int main () {     int x [10];     int i;     int max;     int min;     float sum = 0;     average float;

/* Capturando los valores del arreglo */ 
for (i=0; i <10; i++) 
{ 
    printf("Capture valor estatura: [%d]: ", i+1); 
    scanf("%d",&x[i]); 
}


//obteniendo el maximo

max = x[0];  //asigno el primer elemento del arreglo a la variable max

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

    if (max < x[i+1]) //si el valor almacenado en max es menor que el siguiente elemento del arreglo entonces max sera ese valor siguiente
    max=x[i+1];

}


//obteniedo el minimo

min = x[0];  //asigno el primer elemento del arreglo a la variable min

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

    if (min > x[i+1])//si el valor almacenado en min es mayor que el siguiente elemento del arreglo entonces min sera ese valor siguiente
    min=x[i+1];
}

printf("El valor maximo es: %d\n",max);
printf("El valor minimo es: %d\n",min);


//obteniendo el promedio

for(i=0;i<10;i++){
    suma = suma + x[i]; //Suma todos los elementos del arreglo y lo guarda en la variable suma
}

promedio = suma/10; //divide la suma de todos los elementos del arreglo entre la cantidad de elementos
printf("El promedio es: %.2f",promedio);

}

Here are some tips from my code

  • Declare the variables add and average as float, because if you declare it as int, at the time of obtaining the result it will not print with decimal figures.

  • To print the number of decimal places you want, place% .2f as shown in my code. If you want 3 decimal places you should only place% .3f, if you want 4 decimal places place% .4f and so on.

  • In the for cycle you can use i ++ instead of i = i + 1 for the increase. for (i = 0; i < 10; i ++)

  • answered by 08.12.2018 в 04:57
    0

    First, it is not necessary to order the arrangement,

    In the for search min and max , you typed:

    for (i = 1; i <= 10; i++)
    

    It must be like this, so as not to overflow the vector.

    for (i = 1; i < 10; i++)
    

    The problem of the minor, is in the if below, you use the max :

    else if (x[i] < max)
    {
        min = x[i];
    }
    

    It should be, using the min :

    else if (x[i] < min)
    {
        min = x[i];
    }
    

    And the problem of the average, is because you do not do the sum well, because the for of the sum, does not add, only prints:

    The code I would do it like this:

    #include<stdio.h>
    #include<conio.h>
    int main() 
    {
        int x[10];
        int i;
        int max;
        int min;
        int prom,suma;
        // Capturando los valores del arreglo
        for(i = 0; i < 10; i++)
        {
            printf("Capture valor estatura: [%d]: ", i+1);
            scanf("%d",&x[i]);
        }
        // Buscar mínimo, máximo, y calcular suma
        min = x[0];
        max = x[0];
        suma = x[0];
        for(i = 1; i < 10; i++)
        {
            if(x[i] > max)
            {
                max = x[i];
            }
            else if(x[i] < min)
            {
                min = x[i];
            }
            suma += x[i];
        }
        // Calcular promedio
        prom = suma/10;
    
        printf("\n");
        printf("Estatura maxima es: %d  Estatura minima es: %d \n", max, min);
        printf("Promedio estaturas: %d\n", prom);
    
        getch();
        return 0;
    }
    
        
    answered by 08.12.2018 в 06:10