Numbers smaller than the average

Asked

Viewed 165 times

1

I need to average 20 numbers and present all the numbers below average.

package pag1;

import java.util.Arrays;
import java.util.Scanner;

public class ex2 {
public static void main (String[] args){
    Scanner x = new Scanner(System.in);

    int soma = 0;
    int posicao = 0;
    double [] numeros = new double [20];
    System.out.println("Digite 20 números para obter a média dos mesmos:");

    while (posicao < numeros.length){
        numeros [posicao] = x.nextDouble();
        posicao++;          
    }
    System.out.println(Arrays.toString(numeros));
    for(int i=0; i<numeros.length; i++){
        soma += numeros[i];

    }
    int media = soma / numeros.length;
    System.out.println("soma: " + soma);
    System.out.println(media);

}

}

We only missed the part about showing the numbers smaller than average, but I have no idea how.

  • Why not go through the list, compare each value with the average and, if it is smaller, display the number?

  • I’m a beginner, how could I do this?

  • The same way you did the rest of the code. What exactly is the difficulty? You made the code yourself?

  • Yes, I’m trying, the difficulty is I don’t know which command to use to compare each item of the array with the final mean.

  • 1

    Using the operator <: do if (x < y) returns true if the value of x is less than that of y.

3 answers

1

First of all, I think your average variable has to be double type. Solution: Just go through the array and check which numbers are smaller than the average!

stretch:

for(int i=0; i<numeros.length; i++){
    if(numeros[i] < media)
        System.out.println(numeros[i]);

}
  • 1

    Now, being numeros[i] of the kind double and media of the kind int, what are the precautions to be taken?

  • Actually, it doesn’t make much sense he’s using an integer variable for average. I think the best thing to do is to put average to double. But the way the code is, maybe we need to cast a.

  • So enjoy and edit the answer by adding this. What if the two are of type double, what care will be?

  • I don’t see any problem, what would be the problem ?

  • Some numbers cannot be accurately represented. Although it is difficult to do so, a comparison between two floating point values can generate an unexpected result. For example, dividing 26.55 by 3 gives 8.85, but in Java, doing 26.55f/3 is 8.849999. If the user typed 3 values and the sum resulted in 26.55, and being one of the values equal to 8.8499999, it would be expected that this value would be displayed in the output, because it is less than 8.85, which would be the average, but not. Because of this detail, the value 8.8499999 is greater than 26.55f/3 and would not be displayed.

  • ah, true. I’ve heard about it. Thank you for remembering. But I think his doubt is more about the logic of solving. Adding more this may confuse the solution a bit. Moreover, the problem is quite simple :)

  • But about the average being like int is fundamental to comment. I believe you can complete the answer at least with this.

Show 2 more comments

1

As a complement to the existing responses, I present a streams and Amble, that will make the printing of below average values in a more compact way.

Average:

double media = Arrays.stream(numeros).average().getAsDouble();

Printing below average values:

Arrays.stream(numeros).filter(num->num < media).forEach(num->System.out.println(num));

In which the filter kept only the elements below average with num->num < media, and with the forEach show yourself the ones who stayed.

See code working on Ideone

0

I found a solution:

package pag1;

import java.util.Arrays;
import java.util.Scanner;

public class ex2 {
public static void main (String[] args){
    Scanner x = new Scanner(System.in);

    double [] menoresque = new double [20];
    int soma = 0;
    int posicao = 0;
    double [] numeros = new double [20];
    System.out.println("Digite 20 números para obter a média dos mesmos:");

    while (posicao < numeros.length){
        numeros [posicao] = x.nextDouble();
        posicao++;          
    }
    System.out.println(Arrays.toString(numeros));
    for(int i=0; i<numeros.length; i++){
        soma += numeros[i];

    }
    int media = soma / numeros.length;
    System.out.println("soma: " + soma);
    System.out.println(media);
for(int i=0; i<numeros.length; i++){
    if (numeros[i] < media) {
    menoresque[i]=numeros[i];
    }

}for(int i=0; i<menoresque.length; i++){
    if(menoresque[i] != 0){
System.out.print(menoresque[i]+", ");
    }
}
}
}

Browser other questions tagged

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