How to make a matrix that already contains data? [closed]

0

In my review I was told that with the values contained in the table that was printed on a sheet I would get an arrangement with its percentages. My question is how do I create a matrix with those same values and then put percentages in an array. -Java

    
asked by 12.12.2018 в 07:11
source

1 answer

1

good Mark:)

To declare a one-dimensional arrangement (single line table) it could be as follows:

int arreglo[] = {4,3,35,-1,2000}; //Aquí van los valores de tu tabla

If you want to show it on the screen you can use the following code:

for(int i = 0; i<arreglo.length; i++){ 
    System.out.print(arreglo[i] + ", ");
}
//arreglo.length devuelve la cantidad de datos en el arreglo

However, if you want a two-dimensional array or array (table with one or more rows and columns), you could use:

int tabla[][] = {
                    { 95, 45, 37, 70, 85 },
                    { -4, 10, 92, 49, 48 },
                    { 2, 30, 51, 100, -9 }
                 };
//Se puede poner en una sola línea de código, pero así es un poco más visual

Where everything goes within a {} main, and you will use the brackets {} once again to enter each piece of each row.

The data is separated by commas, and also each line, except the last one.

And to show your entered data, you could use the following code:

for(int i = 0; i<tabla.length; i++){ //Al ser Bidimensional, tabla.length = 2;
        for(int j = 0; j<tabla[i].length; j++){          
            System.out.print(tabla[i][j] + ", ");
        }
        System.out.println();
    }

Finally, I must say that each row can have its own amount of data (the table does not have to be symmetric).

//Ejemplo de Arreglo bidimencional no simétrico.
int tabla[][] = {
                    { 95, 1, 35, 7, 85 },
                    { 4, 3, 5, 1, -4, 5, 56, 92 },
                    { 2, 30 },
                    { -5, 23, 48 }
                 };

Greetings:)

    
answered by 12.12.2018 / 09:07
source