Concetti Chiave
- The method "scambiaDiPostoDueRighe" swaps two specified rows in a matrix of integers.
- It takes a matrix M and two row indices R1 and R2 as input parameters.
- The method returns the same matrix after exchanging the contents of the two specified rows.
- A temporary container variable is used to facilitate the swapping of elements between the rows.
- The swapping process involves iterating only over the columns since the rows to be swapped are predetermined.
Tema 97
Scrivere un metodo scambiaDiPostoDueRighe che riceve una matrice di interi M e due numeri R1 e R2 e restituisce la stessa matrice dopo aver effettuato lo scambio tra le righe R1 ed R2.Ad esempio, sia M la matrice così costituita
| 3 | 18 | 15 | 7 | 2 |
| 1 | 9 | 11 | 4 | 6 |
| 7 | 2 | 6 | 3 | 5 |
| 5 | 1 | 25 | 2 | 1 |
| 3 | 2 | 33 | 4 | 4 |
| 3 | 18 | 15 | 7 | 2 |
| 5 | 1 | 25 | 2 | 1 |
| 7 | 2 | 6 | 3 | 5 |
| 1 | 9 | 11 | 4 | 6 |
| 3 | 2 | 33 | 4 | 4 |
public class tema97{ public static int[][] scambiaDiPostoDueRighe (int[][] M, int R1, int R2){ // approntiamo un contenitore che ci servirà per posizionare momentaneamente un // elemento da spostare int contenitoreTemporaneo; // facciamo partire un ciclo che interesserà soltanto le colonne visto che le // righe su cui lavorare sono note for (int j = 0; j
Domande da interrogazione
- Qual è la funzione del metodo scambiaDiPostoDueRighe nella classe tema97?
- Qual è l'effetto dell'applicazione del metodo scambiaDiPostoDueRighe sulla matrice di esempio fornita?
Il metodo scambiaDiPostoDueRighe riceve una matrice di interi e due numeri che rappresentano le righe da scambiare. Utilizza un contenitore temporaneo per effettuare lo scambio degli elementi tra le due righe specificate, restituendo la matrice modificata.
Applicando il metodo scambiaDiPostoDueRighe alla matrice di esempio con le righe 1 e 3, le righe vengono scambiate. La seconda riga originale diventa la quarta e viceversa, mantenendo inalterati gli altri elementi della matrice.