I am trying to solve an exercise with arrays in java my difficulty is to print the average of odd and odd Indian array

Asked

Viewed 51 times

1

  1. Make a java program do the following:

a) Fill the array with the heights of 30 individuals;

b) View the array;

c) Using the array, calculate and print the average of all the heights in the array’s indeces pairs;

d) Using the array, calculate and print the average of all times in odd indeces of the array.

e) find and print the highest and lowest individual. I tried to do so:

import java.util.Scanner;
public class Array02 {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int nrIndividuos = 6;
    
        int[] alturas = new int[nrIndividuos];
        
        
        for (int i = 0; i < nrIndividuos; i++) {
             System.out.println("Introduza a altura do " + (i + 1) + " individuo");
            alturas[i] = s.nextInt();
        } 
        
        int soma = 0;
        double mediaPar = 0;
        for(int j = 0; j < nrIndividuos; j++){
            if(alturas[j]%2==0){
                
                soma = soma + alturas[j];
                
                mediaPar = soma/nrIndividuos;
            }
        }
        System.out.println(mediaPar);
        
    }

}
  • What is your doubt?

2 answers

0

For the average of even indices, it is preferable to make a cycle that starts at 0 and goes from 2 to 2. The same is true for odd (starting at 1).

int cont = 0;
for(j=0; j < nrIndividuos; j=j+2){
    soma += alturas[j];
    cont++;
}
media = soma / cont

What you are doing in your program is the average of even heights. That is: In an array [200,150,195], your program adds 200 + 150 and divides by 3, since you are evaluating the array parity[j] and not j. However, the goal is to add 200 + 195 and divide by 2

Obs: it is possible to remove the cont variable and use whole divisions / division debris by 2, but to simplify and be valid for even and odd, I thought to use.

  • Understood thank you very much!

0

Scanner s = new Scanner(System.in);
int nrIndividuos = 6;

ArrayList<Integer> alturas = new ArrayList<>();

while (nrIndividuos != alturas.size()) {
    System.out.printf("Introduza a altura do %d individuo: ", alturas.size() + 1);
    alturas.add(s.nextInt());
}
s.close();

List<Integer> pares = alturas.stream().filter(value -> value % 2 == 0).collect(Collectors.toList());
List<Integer> impares = alturas.stream().filter(value -> value % 2 == 1).collect(Collectors.toList());


System.out.printf("Pares: %s \n", Arrays.toString(pares.toArray()));
System.out.printf("Impares: %s \n", Arrays.toString(impares.toArray()));

Integer totalPares = pares.stream().reduce(0, Integer::sum);
Integer totalImpares = impares.stream().reduce(0, Integer::sum);

System.out.printf("Media Par: %f \n", (double) totalPares / nrIndividuos);
System.out.printf("Media Impar: %f \n", (double) totalImpares / nrIndividuos);

System.out.printf("Menor individuo: %s \n", alturas.stream().min(Comparator.naturalOrder()).get());
System.out.printf("Maior individuo: %s \n", alturas.stream().max(Comparator.naturalOrder()).get());

A cool trick is to use Streams, they reduce the code a lot. For example I created a dynamic list "Arraylist".

It also replaces the for by a while, both have the same effect in that case, but while makes the code more enjoyable.

Now we go to the tricks, first using the list that contains all the heights we will apply a filter stream, separating all pairs and odd creating a new list for each of them.

Using the pair and odd list we will apply a stream of reduce, the goal of reduce is to reduce the list by only one value, in which case we will reduce the list by adding all the elements, so the Integer:.

Without much secrecy, the average calculation.

Now comes another cool trick of the stream, we will catch the biggest and smallest individual using the functions min and max, these functions they need a comparator in this case we will use the rising order "natural order"

Streams are functions that work on the Arrays / Lists, they can be used in chain, for example we filter all pairs and sum them:

int totalPares = alturas.stream()
                        .filter(value -> value % 2 == 0)
                        .reduce(0, Integer::sum);

I hope I’ve helped :-)

Note: Don’t forget to close the scanner when no longer using :-)

  • Thank you very much, you helped me a lot

Browser other questions tagged

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