diff --git a/Program.cs b/Program.cs index 7a8812c..5691651 100644 --- a/Program.cs +++ b/Program.cs @@ -8,7 +8,7 @@ class Program { static void Menu() { Console.Clear(); int scelta; - int[,] matrix = null; + int[,] matrix1 = null, matrix2 = null; int[] array = null; do { @@ -18,9 +18,9 @@ class Program { Console.WriteLine("2. Somma diagonale secondaria"); Console.WriteLine("3. Media diagonale secondaria"); Console.WriteLine("4. Prodotto diagonale secondaria"); - Console.WriteLine("6. Valore minimo e massimo diagonale principale"); - Console.WriteLine("7. Trova zeri sulla diagonale principale"); - Console.WriteLine("8. Cerca sulla diagonale principale"); + Console.WriteLine("5. Valore minimo e massimo diagonale secondaria"); + Console.WriteLine("6. Somma 2 matrici"); + Console.WriteLine("7. MOstra matrice trasposta"); Console.WriteLine("0. Esci"); Console.Write("Scelta: "); scelta = Convert.ToInt32(Console.ReadLine()); @@ -30,16 +30,16 @@ class Program { break; case 1: Console.Clear(); - matrix = CreaMatrice(); + matrix1 = CreaMatriceQuadrata(); Console.Clear(); Console.WriteLine("La matrice creata è la seguente:"); - MostraMatrice(matrix); + MostraMatrice(matrix1); Pausa(); break; case 2: Console.Clear(); - if (matrix != null) { - Console.WriteLine($"La somma degli elementi della diagonale principale è {SommaMatriceDiagonale2(matrix)}"); + if (matrix1 != null) { + Console.WriteLine($"La somma degli elementi della diagonale principale è {SommaMatriceDiagonale2(matrix1)}"); } else { Console.WriteLine("È necessario creare la matrice prima di calcolare la somma della diagonale principale."); @@ -48,8 +48,8 @@ class Program { break; case 3: Console.Clear(); - if (matrix != null) { - Console.WriteLine($"La media degli elementi della diagonale principale è {SommaMatriceDiagonale2(matrix) / matrix.GetLength(0)}"); + if (matrix1 != null) { + Console.WriteLine($"La media degli elementi della diagonale principale è {SommaMatriceDiagonale2(matrix1) / matrix1.GetLength(0)}"); } else { Console.WriteLine("È necessario creare la matrice prima di calcolare la media della diagonale principale."); @@ -58,8 +58,8 @@ class Program { break; case 4: Console.Clear(); - if (matrix != null) { - Console.WriteLine($"Il prodotto degli elementi della diagonale principale è {ProdottoMatriceDiagonale2(matrix)}"); + if (matrix1 != null) { + Console.WriteLine($"Il prodotto degli elementi della diagonale principale è {ProdottoMatriceDiagonale2(matrix1)}"); } else { Console.WriteLine("È necessario creare la matrice prima di calcolare il prodotto della diagonale principale."); @@ -68,8 +68,9 @@ class Program { break; case 5: Console.Clear(); - if (matrix != null) { - //Console.WriteLine($"Il prodotto degli elementi della diagonale principale è {ProdottoMatriceDiagonale(matrix)}"); + if (matrix1 != null) { + MostraMinimoMassimoMatrice(MinimoMassimoMatriceDiagonale2(matrix1, 0), 0); + MostraMinimoMassimoMatrice(MinimoMassimoMatriceDiagonale2(matrix1, 1), 1); } else { Console.WriteLine("È necessario creare la matrice prima di calcolare il prodotto della diagonale principale."); @@ -78,20 +79,19 @@ class Program { break; case 6: Console.Clear(); - if (matrix != null) { - //array = MinimoMassimoMatriceDiagonale(matrix, 0); - Console.WriteLine($"Il valore minimo è {array[0]} e si trova in posizione {array[1]},{array[2]}"); - //array = MinimoMassimoMatriceDiagonale(matrix, 1); - Console.WriteLine($"Il valore massimo è {array[0]} e si trova in posizione {array[1]},{array[2]}"); + matrix1 = CreaMatrice(); + matrix2 = CreaMatrice(); + if (matrix1.GetLength(0) != matrix2.GetLength(0) && matrix1.GetLength(1) != matrix2.GetLength(1)) { + Console.WriteLine("Per effettuare la somma tra due matrici, queste devono essere delle stesse dimensioni."); } else { - Console.WriteLine("È necessario creare la matrice prima di calcolare il minimo e il massimo della diagonale principale."); + MostraMatrice(SommaMatrici(matrix1, matrix2)); } Pausa(); break; case 7: Console.Clear(); - if (matrix != null) { + if (matrix1 != null) { if (true) { Console.WriteLine("Sulla diagonale principale sono presenti degli zeri"); } @@ -106,7 +106,7 @@ class Program { break; case 8: Console.Clear(); - if (matrix != null) { + if (matrix1 != null) { Console.Write("Inserire un numero da cercare: "); if (true) { Console.WriteLine("Elemento trovato"); @@ -135,7 +135,7 @@ class Program { Console.Clear(); } - static int[,] CreaMatrice() { + static int[,] CreaMatriceQuadrata() { int dimensione = 0; Console.Write("Inserire la dimensione della matrice: "); dimensione = Convert.ToInt32(Console.ReadLine()); @@ -172,4 +172,106 @@ class Program { } return ritorno; } + + static int[,] MinimoMassimoMatriceDiagonale2(int[,] p_matrice, int p_scelta) { + int[,] ritorno = null; + switch (p_scelta) { + case 0: + int min = int.MaxValue; + + for (int r = 0; r < p_matrice.GetLength(0); r++) { + if (p_matrice[r, p_matrice.GetLength(1) - 1 - r] < min) { + min = p_matrice[r, p_matrice.GetLength(1) - 1 - r]; + } + } + + ritorno = TrovaMatrice(p_matrice, min); + break; + case 1: + int max = int.MinValue; + + for (int r = 0; r < p_matrice.GetLength(0); r++) { + if (p_matrice[r, p_matrice.GetLength(1) - 1 - r] > max) { + max = p_matrice[r, p_matrice.GetLength(1) - 1 - r]; + } + } + + ritorno = TrovaMatrice(p_matrice, max); + break; + } + + return ritorno; + } + + static int[,] TrovaMatrice(int[,] p_matrice, int p_DaTrovare) { + int[,] ritorno = new int[p_matrice.GetLength(0), p_matrice.GetLength(1)]; + RiempiMatriceMax(ritorno); + + for (int r = 0; r < p_matrice.GetLength(0); r++) { + if (p_matrice[r, p_matrice.GetLength(1) - 1 - r] == p_DaTrovare) { + ritorno[r, p_matrice.GetLength(1) - 1 - r] = p_DaTrovare; + } + } + return ritorno; + } + + static void RiempiMatriceMax(int[,] p_matrice) { + for (int r = 0; r < p_matrice.GetLength(0); r++) { + for (int c = 0; c < p_matrice.GetLength(1); c++) { + p_matrice[r, c] = int.MaxValue; + } + } + } + static void MostraMinimoMassimoMatrice(int[,] p_matrice, int p_scelta) { + bool trovato = false; + string stringa = ""; + + switch (p_scelta) { + case 0: + stringa = "minimo"; + break; + case 1: + stringa = "massimo"; + break; + } + + for (int r = 0; r < p_matrice.GetLength(0); r++) { + for (int c = 0; c < p_matrice.GetLength(1); c++) { + if (p_matrice[r, c] != int.MaxValue) { + if (!trovato) { + Console.WriteLine($"Il valore {stringa} è {p_matrice[r, c]} e si trova nelle posizioni:"); + trovato = true; + } + Console.WriteLine($"{r}, {c}"); + } + } + } + } + + static int[,] CreaMatrice() { + int righe, colonne; + Console.Write("Inserire il numero di righe della matrice: "); + righe = Convert.ToInt32(Console.ReadLine()); + Console.Write("Inserire il numero di colonne della matrice: "); + colonne = Convert.ToInt32(Console.ReadLine()); + int[,] ritorno = new int[righe, colonne]; + for (int r = 0; r < ritorno.GetLength(0); r++) { + for (int c = 0; c < ritorno.GetLength(1); c++) { + Console.Write($"Inserire l'elemento in posizione {r}, {c}: "); + ritorno[r, c] = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine(); + } + } + return ritorno; + } + + static int[,] SommaMatrici(int[,] p_matrice1, int[,] p_matrice2) { + int[,] ritorno = new int[p_matrice1.GetLength(0), p_matrice1.GetLength(1)]; + for (int r = 0; r < p_matrice1.GetLength(0); r++) { + for (int c = 0; c < p_matrice1.GetLength(1); c++) { + ritorno[r, c] = p_matrice1[r, c] + p_matrice2[r, c]; + } + } + return ritorno; + } } diff --git a/bin/Debug/net9.0/matrix_3.dll b/bin/Debug/net9.0/matrix_3.dll index f6c0486..c482492 100644 Binary files a/bin/Debug/net9.0/matrix_3.dll and b/bin/Debug/net9.0/matrix_3.dll differ diff --git a/bin/Debug/net9.0/matrix_3.pdb b/bin/Debug/net9.0/matrix_3.pdb index 9f05a0c..c0606ba 100644 Binary files a/bin/Debug/net9.0/matrix_3.pdb and b/bin/Debug/net9.0/matrix_3.pdb differ diff --git a/obj/Debug/net9.0/matrix_3.AssemblyInfo.cs b/obj/Debug/net9.0/matrix_3.AssemblyInfo.cs index bdb5d63..09652fa 100644 --- a/obj/Debug/net9.0/matrix_3.AssemblyInfo.cs +++ b/obj/Debug/net9.0/matrix_3.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("matrix_3")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+6014c313b202cd6198ffad5fdc9677bf63ff7c11")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b00a9cea811f930eff832555c7d39711548491f2")] [assembly: System.Reflection.AssemblyProductAttribute("matrix_3")] [assembly: System.Reflection.AssemblyTitleAttribute("matrix_3")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/obj/Debug/net9.0/matrix_3.AssemblyInfoInputs.cache b/obj/Debug/net9.0/matrix_3.AssemblyInfoInputs.cache index 9bf0859..fd79203 100644 --- a/obj/Debug/net9.0/matrix_3.AssemblyInfoInputs.cache +++ b/obj/Debug/net9.0/matrix_3.AssemblyInfoInputs.cache @@ -1 +1 @@ -c406b3a08bd253a1bcf5b3ae92da91aa3d5278f12f55945bc57b8ccec301f14d +24011d8278f0ee5fff0d76f8ca60ae6d36888403cf7bf503a41a322503a8e8e7 diff --git a/obj/Debug/net9.0/matrix_3.dll b/obj/Debug/net9.0/matrix_3.dll index f6c0486..c482492 100644 Binary files a/obj/Debug/net9.0/matrix_3.dll and b/obj/Debug/net9.0/matrix_3.dll differ diff --git a/obj/Debug/net9.0/matrix_3.pdb b/obj/Debug/net9.0/matrix_3.pdb index 9f05a0c..c0606ba 100644 Binary files a/obj/Debug/net9.0/matrix_3.pdb and b/obj/Debug/net9.0/matrix_3.pdb differ diff --git a/obj/Debug/net9.0/ref/matrix_3.dll b/obj/Debug/net9.0/ref/matrix_3.dll index 38e67bc..480e259 100644 Binary files a/obj/Debug/net9.0/ref/matrix_3.dll and b/obj/Debug/net9.0/ref/matrix_3.dll differ diff --git a/obj/Debug/net9.0/refint/matrix_3.dll b/obj/Debug/net9.0/refint/matrix_3.dll index 38e67bc..480e259 100644 Binary files a/obj/Debug/net9.0/refint/matrix_3.dll and b/obj/Debug/net9.0/refint/matrix_3.dll differ