How to average N numbers greater than 6

Asked

Viewed 109 times

0

I have to make a program in C that shows at the end the average of N numbers greater than 6, but I’ve 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;
}
  • Your code does not compile. => is not valid in the language. You also did not initialize the variable maior before the first if. And the division is whole, so you won’t be able to get the average that way

  • If you shared your thoughts, it might be easier to help you, show you where the problems in the algorithm are and how to turn them into code

  • If the n/n division gives a different result than 1 you will surely be with a tremendous bug!

2 answers

3

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

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

2

You can implement a function capable of calculating the arithmetic mean of an integer vector using only elements that have a value below the set limit, see only:

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 value 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;
}

Exit:

Maior: 10
Menor: 1
Media: 2.66667
  • The average may not be whole.

  • @Victorstafusa: Well noted! Follows edition.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.