diff --git a/Program.cs b/Program.cs index 24121bd..a94d910 100644 --- a/Program.cs +++ b/Program.cs @@ -17,7 +17,8 @@ class Program { case 0: break; case 1: - CreaMatrice(); + StampaMatriciSeZero(TrovaSottomatriciQuadrate(RiempiMatrice(CreaMatrice()))); + Pausa(); break; default: @@ -93,16 +94,15 @@ class Program { } } while (error); - return RiempiMatrice(new int[righe, colonne]); + return new int[righe, colonne]; } static int[,] RiempiMatrice(int[,] p_matrix) { - int elemento; for (int r = 0; r < p_matrix.GetLength(0); r++) { for (int c = 0; c < p_matrix.GetLength(1); c++) { Console.Write($"Inserire l'elemento in posizione {r},{c}: "); try { - elemento = Convert.ToInt32(Console.ReadLine()); + p_matrix[r, c] = Convert.ToInt32(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("Errore: elemento non valido."); @@ -110,7 +110,82 @@ class Program { } } } - return p_matrix; } -} + + public static (int, int, int)[]? TrovaSottomatriciQuadrate(int[,] p_matrice) { + (int, int, int)[]? ritorno = null, prev = null; //riga iniziale, colonna iniziale e matrice quadrata + + int maxDimensione = (p_matrice.GetLength(0) < p_matrice.GetLength(1)) ? p_matrice.GetLength(0) : p_matrice.GetLength(1); // La massima dimensione possibile di una sottomatrice quadrata + + // Dimensioni delle sottomatrici quadrate: da 2 a maxDimensione + for (int dimensione = 2; dimensione <= maxDimensione; dimensione++) { + // Riga iniziale + for (int i = 0; i <= p_matrice.GetLength(0) - dimensione; i++) { + // Colonna iniziale + for (int j = 0; j <= p_matrice.GetLength(1) - dimensione; j++) { + + int[,] sottomatrice = new int[dimensione, dimensione]; + + for (int r = 0; r < dimensione; r++) { + for (int c = 0; c < dimensione; c++) { + sottomatrice[r, c] = p_matrice[i + r, j + c]; + } + } + + if (IsMatrixSumZero(sottomatrice) != null) { + if (prev == null) { + ritorno = new (int, int, int)[1]; + + ritorno[0] = (i, j, sottomatrice.GetLength(0)); + } + else { + ritorno = new (int, int, int)[prev.Length + 1]; + + //copia dell'array vecchio in quello nuovo + for (int k = 0; k < prev.Length; k++) { + ritorno[k] = prev[k]; + } + + ritorno[prev.Length] = (i, j, sottomatrice.GetLength(0)); + } + + prev = ritorno; + } + + + } + } + } + + return ritorno; + } + + public static int[,]? IsMatrixSumZero(int[,] p_matrice) { //null se falso, p_matrice se vero + int somma = 0; + int[,]? ritorno = null; + for (int r = 0; r < p_matrice.GetLength(0); r++) { + for (int c = 0; c < p_matrice.GetLength(1); c++) { + somma += p_matrice[r, c]; + } + } + + if (somma == 0) { + ritorno = p_matrice; + } + return ritorno; + } + + static void StampaMatriciSeZero((int, int, int)[]? p_matrici) { + if (p_matrici != null) { + for (int i = 0; i < p_matrici.Length; i++) { + Console.WriteLine("Sottomatrice quadrata a somma nulla trovata:"); + Console.WriteLine($"\tDimensione: {p_matrici[i].Item3} * {p_matrici[i].Item3}"); + Console.WriteLine($"\tPosizione iniziale: riga {p_matrici[i].Item1}, colonna {p_matrici[i].Item2}"); + } + } + else { + Console.WriteLine("Nessuna sottomatrice quadrata a somma nulla trovata."); + } + } +} \ No newline at end of file diff --git a/bin/Debug/net9.0/vacanzeEstive_sezione2.dll b/bin/Debug/net9.0/vacanzeEstive_sezione2.dll index 44383c7..ae679e5 100644 Binary files a/bin/Debug/net9.0/vacanzeEstive_sezione2.dll and b/bin/Debug/net9.0/vacanzeEstive_sezione2.dll differ diff --git a/bin/Debug/net9.0/vacanzeEstive_sezione2.pdb b/bin/Debug/net9.0/vacanzeEstive_sezione2.pdb index 4ed4062..c561462 100644 Binary files a/bin/Debug/net9.0/vacanzeEstive_sezione2.pdb and b/bin/Debug/net9.0/vacanzeEstive_sezione2.pdb differ diff --git a/obj/Debug/net9.0/ref/vacanzeEstive_sezione2.dll b/obj/Debug/net9.0/ref/vacanzeEstive_sezione2.dll index 815fe1e..c7f31e3 100644 Binary files a/obj/Debug/net9.0/ref/vacanzeEstive_sezione2.dll and b/obj/Debug/net9.0/ref/vacanzeEstive_sezione2.dll differ diff --git a/obj/Debug/net9.0/refint/vacanzeEstive_sezione2.dll b/obj/Debug/net9.0/refint/vacanzeEstive_sezione2.dll index 815fe1e..c7f31e3 100644 Binary files a/obj/Debug/net9.0/refint/vacanzeEstive_sezione2.dll and b/obj/Debug/net9.0/refint/vacanzeEstive_sezione2.dll differ diff --git a/obj/Debug/net9.0/vacanzeEstive_sezione2.AssemblyInfo.cs b/obj/Debug/net9.0/vacanzeEstive_sezione2.AssemblyInfo.cs index e65c2b4..a195dbb 100644 --- a/obj/Debug/net9.0/vacanzeEstive_sezione2.AssemblyInfo.cs +++ b/obj/Debug/net9.0/vacanzeEstive_sezione2.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("vacanzeEstive_sezione2")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+cafc48ce6407bf16c0d16b4fde5ee8673539a6ca")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0314f06fda80211ebc1f2be2511a8d52b6b43cad")] [assembly: System.Reflection.AssemblyProductAttribute("vacanzeEstive_sezione2")] [assembly: System.Reflection.AssemblyTitleAttribute("vacanzeEstive_sezione2")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/obj/Debug/net9.0/vacanzeEstive_sezione2.AssemblyInfoInputs.cache b/obj/Debug/net9.0/vacanzeEstive_sezione2.AssemblyInfoInputs.cache index ea7a29c..f77e0f9 100644 --- a/obj/Debug/net9.0/vacanzeEstive_sezione2.AssemblyInfoInputs.cache +++ b/obj/Debug/net9.0/vacanzeEstive_sezione2.AssemblyInfoInputs.cache @@ -1 +1 @@ -f51dd8e398f51ce3006057d64eb8817f1c96774015caa399a7ccd91231d29a3e +ebb98eae628fd33d11fbc6dc606c0272454799a56e9ea38c60adea94a82bcf21 diff --git a/obj/Debug/net9.0/vacanzeEstive_sezione2.dll b/obj/Debug/net9.0/vacanzeEstive_sezione2.dll index 44383c7..ae679e5 100644 Binary files a/obj/Debug/net9.0/vacanzeEstive_sezione2.dll and b/obj/Debug/net9.0/vacanzeEstive_sezione2.dll differ diff --git a/obj/Debug/net9.0/vacanzeEstive_sezione2.pdb b/obj/Debug/net9.0/vacanzeEstive_sezione2.pdb index 4ed4062..c561462 100644 Binary files a/obj/Debug/net9.0/vacanzeEstive_sezione2.pdb and b/obj/Debug/net9.0/vacanzeEstive_sezione2.pdb differ