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
Your code does not compile.
=>
is not valid in the language. You also did not initialize the variablemaior
before the firstif
. And the division is whole, so you won’t be able to get the average that way– Jefferson Quesado
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
– Jefferson Quesado
If the n/n division gives a different result than 1 you will surely be with a tremendous bug!
– anonimo