How to make an average of N numbers greater than 6

0

I have to do a program in C that shows in the end the average of N numbers greater than 6, but I have already exhausted my ideas on how to solve this.

What I've been able to do so far is this:

#include <stdio.h>
#include <math.h>
int main()
{
    int n,maior,menor,media;

    do
    {
        scanf ("%d",&n);
        if (n>maior)

            maior=n;
        if (n<menor && n!=0)

            menor=n;

    } while (n != 0);
    media = n/n;
    printf("%d e o maior\n",maior);
    printf("%d e o menor\n",menor);
    for (int i = 0; i => 6 && i<maior; ++i)
    {
        media = n/i;
    }
    printf("%d e a media\n",media);
return 0;
}
    
asked by anonymous 01.07.2018 / 21:07

2 answers

3

Knowing which is the greatest or which is the smallest number does not interest you. Your program does not need this.

Within this do-while , you could read a number and if it is greater than 6 (use if ), then add in a soma variable and increase i . If not, jump and let it go (that is, do not put any else ). In the end, it would be enough to send printf to ((double) soma) / i .

    
01.07.2018 / 21:24
2

You can implement a function that can calculate the arithmetic mean of a vector of integers using only elements that have a value below the established limit, see:

double media( int v[], int tam, int limite )
{
    int i = 0;
    double n = 0.0;
    int soma = 0;
    double med = 0.0;

    for( i = 0; i < tam; i++ )
    {
        if( v[i] > limite )
        {
            soma += v[i];
            n++;
        }
    }

    med = soma / n;

    return med;
}

To determine the highest and lowest values contained in an integer vector you can implement specific functions:

int maior( int v[], int tam )
{
    int i = 0;
    int n = INT_MIN;

    for( i = 0; i < tam; i++ )
        if( v[i] > n )
            n = v[i];

    return n;
}

int menor( int v[], int tam )
{
    int i = 0;
    int n = INT_MAX;

    for( i = 0; i < tam; i++ )
        if( v[i] < n )
            n = v[i];

    return n;
}

Putting it all together:

#include <stdio.h>
#include <limits.h>

double media( int v[], int tam, int limit )
{
    int i = 0;
    double n = 0.0;
    int soma = 0;
    double med = 0.0;

    for( i = 0; i < tam; i++ )
    {
        if( v[i] < limit )
        {
            soma += v[i];
            n++;
        }
    }

    med = soma / n;

    return med;
}

int maior( int v[], int tam )
{
    int i = 0;
    int n = INT_MIN;

    for( i = 0; i < tam; i++ )
        if( v[i] > n )
            n = v[i];

    return n;
}

int menor( int v[], int tam )
{
    int i = 0;
    int menor = INT_MAX;

    for( i = 0; i < tam; i++ )
        if( v[i] < menor )
            menor = v[i];

    return menor;
}

int main( void )
{
    int vet[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 1 };
    int tam = sizeof(vet) / sizeof(int);

    printf("Maior: %d\n", maior( vet, tam ) );
    printf("Menor: %d\n", menor( vet, tam ) );
    printf("Media: %g\n", media( vet, tam, 6 ) );

    return 0;
}

Output:

Maior: 10
Menor: 1
Media: 2.66667
    
01.07.2018 / 21:34