_stan
Ominide
1 min. di lettura
Vota

Concetti Chiave

  • The method "creaArrayConElementiAlDiSopraDiagonalePrincipale" extracts elements above the main diagonal of a given square matrix.
  • The matrix elements are accessed row by row, starting from the second column of each row.
  • An array is prepared to store these elements, with its size calculated based on the matrix dimensions.
  • The method iteratively fills the array with elements from the specified upper triangle of the matrix.
  • The process involves managing indices to correctly navigate through the matrix and populate the array.

Tema 85

Scrivere un metodo creaArrayConElementiAlDiSopraDiagonalePrincipale che riceve in ingresso una matrice quadrata di interi M e restituisce un array V contenente tutti gli elementi appartenenti al triangolo al di sopra della diagonale principale.
Ad esempio, sia M la matrice così costituita
3 18 15 7 2
1 9 11 4 1
11 2 6 3 5
5 1 25 2 1
3 2 33 4 4
allora creaArrayConElementiAlDiSopraDiagonalePrincipale (M) darà
18 15 7 2 11 4 1 3 5 1
 public class tema85 { public static int[] creaArrayConElementiAlDiSopraDiagonalePrincipale (int[][] M){ // la dimensione dell’array sarà data dal seguente calcolo int dim = (M.length * M.length - M.length) / 2; int[] A = new int[dim]; // prepariamo un indice per l’array int indiceArray = 0; // prepariamo un indice per scorrere in maniera opportuna le colonne della // matrice // inizialmente è ovvio che si comincerà con la seconda colonna cioè di indice 1 int indiceColonna = 1; for (int i = 0; i 

Domande e risposte

Hai bisogno di aiuto?
Chiedi alla community