How do I do it so that instead of knowing the% of rows is that of columns?

1
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Xnorux
      {
   public static void main(String[] args)
   {
       float array []=new float [3];
       int SFil;
       int tabla[][] = {
                    { 95, 45, 37, 70, 85 },
                    { -4, 10, 92, 49, 48 },
                    { 2, 30, 51, 100, -9 }
       };
       for(int i = 0; i<tabla.length; i++)
       {
           SFil=0;

           for(int j = 0; j<tabla[i].length; j++)
           {
               if (tabla[i][j]%2!=0)
               {
                   SFil++;
               }
           }
           array[i]=(100*SFil)/tabla[i].length;
           System.out.println("\nLa fila "+i+" tiene "+SFil+" números impares ("+array[i]+"%)");
        }
    }
}
    
asked by Pablo Lozano 12.12.2018 в 12:35
source

1 answer

0

In your code you have two nested loops, the first uses i to iterate over the rows and the second uses j to iterate over the columns.

If you exchange them, you can go through the columns with i , resulting in something like:

int numFilas = tabla.length;
int numColumnas = tabla[0].length;
double array [] = new double [numColumnas];
for(int i = 0; i < numColumnas; i++) {
    SFil=0;

    for(int j = 0; j < numFilas; j++) {
        if (tabla[j][i]%2 != 0) {
               SFil++;
        }
    }
    array[i] = (100*SFil) / numColumnas;
    System.out.println("\nLa columna "+i+" tiene "+SFil+" números impares ("+array[i]+"%)");
    }
    
answered by 12.12.2018 / 12:53
source