matrici - wpage.unina.itwpage.unina.it/flora.amato/ei/11_matrici.pdf · j=1 j
TRANSCRIPT
![Page 1: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/1.jpg)
Matrici
![Page 2: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/2.jpg)
Array a più dimensioni.
Sintassi generale ::
tipo nome [dimensione 1][dimensione 2]…[dimensione n]
int a[10][15]int a[10][15]
Lettura matrice N*M
-----------------------------for (i=0; i <N;i++)for (j=0;j<M;j++)
cin >>Matrice[i] [j];----------------------------
Produzione matrice N*M
-----------------------------for (i=0; i <N;i++){for (j=0;j<M;j++)
cout <<Matrice[i] [j];cout <<endl;
}----------------------------
![Page 3: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/3.jpg)
-----------------------------for (i=0; i <N;i++)for (j=0;j<M;j++)
cin >>Matrice[i] [j];----------------------------
N=3 M=2
i=0 i<Nj=0Lettura di Matrice[ 0 ][ 0 ]J=1 j<M VeroLettura di Matrice[ 0 ][ 1 ]J=2 j<M Falso
i=1 i<N Veroj=0Lettura di Matrice[ 1 ][ 0 ]J=1 j<M VeroLettura di Matrice[ 1 ][ 1 ]J=2 j<M Falso
i=2 i<N Veroj=0Lettura di Matrice[ 2 ][ 0 ]J=1 j<M VeroLettura di Matrice[ 2 ][ 1 ]J=2 j<M Falso-------------------------------i=3 i<N Falso
![Page 4: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/4.jpg)
Esercizio
Assegnata una matrice di riempimenti N ed M, determinare la somma dei suoi elementi
1 2 3 4 52 7 8 9 109 8 7 6 5
N = 3M = 5
Somma = 86
![Page 5: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/5.jpg)
![Page 6: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/6.jpg)
Assegnata una matrice di max 10*10 valori interi, determinare valore max e valore min e rispettive posizioni (indici)
Esercizio 31
![Page 7: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/7.jpg)
![Page 8: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/8.jpg)
Esercizio
Dato un array A[ N ][ M] di elementi interi, produrre:
Le somme di ogni rigaLe somme di ogni colonnaLa riga di somma massimaLa colonna di somma massima
N=4M=4
1 2 3 42 6 7 83 1 2 34 5 6 7
Somme righe 10 23 9 22Somme colonne 10 14 18 22Riga max 1Colonna max 3
Somme righe 10 23 9 22Somme colonne 10 14 18 22Riga max 1Colonna max 3
![Page 9: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/9.jpg)
![Page 10: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/10.jpg)
}
![Page 11: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/11.jpg)
![Page 12: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/12.jpg)
Esercizio proposto
Dato un array A[ N ][ M] di elementi reali, produrre:
La riga di somma massimaLa riga di somma minimaL’inversione delle due righeLa colonna di somma minimaL’inversione con la prima colonna
N=4M=4
1 2 3 42 6 7 83 1 2 34 5 6 7
Riga somma max 2Riga somma min 1Colonna somma min 2
Riga somma max 2Riga somma min 1Colonna somma min 2
6 5 7 82 1 3 41 9 2 35 4 6 7
6 5 7 82 1 3 41 9 2 35 4 6 7
![Page 13: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/13.jpg)
Esercizio
Assegnata una matrice quadrata di ordine N prefissato, calcolarela somma degli elementi della diagonale principale e quella degli elementi della diagonale secondaria
L’algoritmo deve avere validità sia per matrici d’ordinepari che dispari
![Page 14: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/14.jpg)
4,44,34,24,14,0
3,4
2,4
1,4
0,4
3,33,23,13,0
2,32,22,12,0
1,31,21,11,0
0,30,20,10,0
El. Diag. Principale 0,0 1,1 2,2 3,3 4,4 j=iEl. Diag.Secondaria 0,4 1,3 2,2 3,2 4,0 j=N – i -1
N = 5
i=0 mat[0][0] mat[0][4]i=1 mat[1][1] mat[1][3]i=2 mat[2][2] mat[2][2]i=3 mat[3][3] mat[3][1]i=4 mat[4][4] mat[4][0]
Sommap=Sommap+mat [ i ][ i ] Sommad=Sommad+mat [ i ][ N – i -1 ]
![Page 15: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/15.jpg)
![Page 16: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/16.jpg)
![Page 17: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/17.jpg)
Esercizio proposto
Dato un array A[ N ][ N] di elementi interi, determinare:
La somma degli elementi della diagonale principaleL’elemento massimo e posizione fra gli elementi delladiagonale principale
N=4
Somma 16Elem max 7Pos max 4,4
Somma 16Elem max 7Pos max 4,4
6 5 7 82 1 3 41 9 2 34 6 5 7
6 5 7 82 1 3 41 9 2 34 6 5 7
![Page 18: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/18.jpg)
Esercizio 135
Assegnata in ingresso una matrice quadrata A d’ordine N, si generi la matrice trasposta
1 2 3 2 5 67 8 9
1 4 72 5 83 6 9
matrice input matrice trasposta
Metodo di soluzioneScambio degli elementi A[ i ] [ j ] con gli elementi A[ j ] [ i ](viene scambiata ogni riga d’ordine N con la colonna di pariordine)
![Page 19: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/19.jpg)
A00 A01 A02
A10 A11 A12
A20 A21 A22
A00 A00A01 A10A02 A20A11 A11A12 A21A22 A22
i j j i
i da 0 a <Nj da i a <N
![Page 20: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/20.jpg)
![Page 21: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/21.jpg)
![Page 22: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/22.jpg)
Esercizio 124
Assegnata una matrice A di interi e di riempimenti RIEMP1 e RIEMP2 si generino da essa due vettori VMAX e VMIN contenentirispettivamente i valori massimi di ogni riga e i minimi di ognicolonna
1 12 -7 6 51 9 8 4 3
1 22 23 25 47 44 12 2 62 3 26 18 9
1 12 -7 6 51 9 8 4 3
1 22 23 25 47 44 12 2 62 3 26 18 9
VMAX 12 9 25 44 26VMAX 12 9 25 44 26
VMIN 1 3 -7 2 3VMIN 1 3 -7 2 3
![Page 23: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/23.jpg)
Ipotesi algoritmo
Leggi Riempimenti e matrice APer ogni riga della matrice for ( i=0;<Riemp1;i++)
blocco1Assumi primo elemento riga come max corrente max=A[i] [1]Per indice corrente di colonna da 1 a <Riemp2blocco2se max corrente <elemento corrente matrice
aggiorna max corrente con elemento correntefine blocco2
memorizza max corrente in VMAXfine blocco1
for (j=1;<Riemp2;j++)
If (max <A[ i] [j] ) max= A[ i] [j]
VMAX[i] = max
![Page 24: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/24.jpg)
![Page 25: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/25.jpg)
![Page 26: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/26.jpg)
Esercizio 106
Assegnata una matrice di interi di riempimenti N ed M, eliminare la riga in posizione K
N=4 M=5 K=2
1 2 3 4 52 7 8 9 03 8 3 1 56 0 2 9 4
1 2 3 4 52 7 8 9 06 0 2 9 4
![Page 27: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/27.jpg)
![Page 28: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/28.jpg)
Caso k sia ultima riga
![Page 29: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/29.jpg)
Esercizio 105
Assegnata una matrice di interi di riempimenti N ed M, eliminarela colonna in posizione K
N=4 M=5 K=2
1 2 3 4 52 7 8 9 03 8 3 1 56 0 2 9 4
1 2 4 52 7 9 04 8 1 56 0 9 4
![Page 30: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/30.jpg)
![Page 31: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/31.jpg)
![Page 32: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/32.jpg)
Esercizio 137
Data una matrice di interi A (N * M) determinare gli elementi cherisultano contemporaneamente massimo della riga e della colonna di appartenenza
8 17 12 51 12 44 615 4 9 2
3 5 2 1
1 0 12 1 215 2 0
![Page 33: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/33.jpg)
Ipotesi algoritmo
Per ogni rigaValuta elemento max e colonna di appartenenzaSe è anche il max di tale colonna
Produci stampa
![Page 34: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/34.jpg)
![Page 35: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/35.jpg)
![Page 36: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/36.jpg)
Esercizio 122
Assegnate due matrici di interi A e B, determinare la matrice prodotto C
Condizione necessaria per il prodotto di due matrici:
Nrocolonne_primamatrice = Nrorighe_secondamatrice
A[m][n] B[n][k] prodotto possibile
A[m][n] B[m][k] prodotto impossibile
![Page 37: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/37.jpg)
Prodotto di due matrici A[m][n] * B[n][k] è una terza matrice C[m][k] con il generico elemento
n
C i , j = ∑ A i, k * B k ,jK=1
i 1, mj 1, l
C11=a11*b11 + a12*b21 + a13*b31
C12=a11*b12 + a12*b22 + a13*b32C21=a21*b11 + a22*b21 + a23*b31
C22=a21*b12 + a22*b21 + a23*b32
per m=2n=3l=2
1 2 34 5 61 2 34 5 6
1 23 45 6
1 23 45 6
22 2849 6422 2849 64
A(2,3)m,n B(3,2)
n, l
C(2,2)m,l
for (int i=0; i<m; i++)for (int j=0; j<l; j++){C[ i ][ j ]=0;for (int k=0; k<n; k++)C[ i ][ j ]=C[ i][ j]+A[ i] [ k ]+
B[ k ][ j ];
for (int i=0; i<m; i++)for (int j=0; j<l; j++){C[ i ][ j ]=0;for (int k=0; k<n; k++)C[ i ][ j ]=C[ i][ j]+A[ i] [ k ]+
B[ k ][ j ];
![Page 38: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/38.jpg)
![Page 39: matrici - wpage.unina.itwpage.unina.it/flora.amato/EI/11_matrici.pdf · J=1 j](https://reader034.vdocuments.pub/reader034/viewer/2022052408/5f174ca7b65de831832edbc4/html5/thumbnails/39.jpg)