191 lines
6.4 KiB
C#
191 lines
6.4 KiB
C#
namespace vacanzeEstive_sezione2;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Console.Clear();
|
|
int scelta = -1;
|
|
do {
|
|
Console.WriteLine("Scegliere un'opzione:");
|
|
Console.WriteLine("1. Trova matrici quadrate nulle");
|
|
Console.WriteLine("0. Esci");
|
|
Console.Write("Scelta: ");
|
|
|
|
try {
|
|
scelta = Convert.ToInt32(Console.ReadLine());
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
StampaMatriciSeZero(TrovaSottomatriciQuadrate(RiempiMatrice(CreaMatrice())));
|
|
|
|
Pausa();
|
|
break;
|
|
default:
|
|
Console.WriteLine("Errore: scelta non valida.");
|
|
Pausa();
|
|
break;
|
|
}
|
|
|
|
}
|
|
catch (FormatException) {
|
|
Console.WriteLine("Errore: scelta non valida.");
|
|
Pausa();
|
|
}
|
|
|
|
}
|
|
while (scelta != 0);
|
|
}
|
|
|
|
static void Pausa() {
|
|
Console.WriteLine("Premere un tasto per continuare. . .");
|
|
Console.ReadKey();
|
|
}
|
|
|
|
static int[,] CreaMatrice() {
|
|
uint righe = 0, colonne = 0;
|
|
bool error;
|
|
|
|
do {
|
|
error = false;
|
|
Console.Write("Inserire il numero di righe della matrice: ");
|
|
|
|
try {
|
|
righe = Convert.ToUInt32(Console.ReadLine());
|
|
if (righe < 1) {
|
|
error = true;
|
|
Console.WriteLine("Errore: non sono ammesse dimensioni inferiori a 2.");
|
|
Pausa();
|
|
}
|
|
}
|
|
catch (FormatException) {
|
|
error = true;
|
|
Console.WriteLine("Errore: dimensione non valida.");
|
|
Pausa();
|
|
}
|
|
catch (OverflowException) {
|
|
error = true;
|
|
Console.WriteLine("Errore: non sono ammesse dimensioni negative.");
|
|
Pausa();
|
|
}
|
|
} while (error);
|
|
|
|
do {
|
|
error = false;
|
|
Console.Write("Inserire il numero di colonne della matrice: ");
|
|
|
|
try {
|
|
colonne = Convert.ToUInt32(Console.ReadLine());
|
|
if (colonne < 1) {
|
|
error = true;
|
|
Console.WriteLine("Errore: non sono ammesse dimensioni inferiori a 2.");
|
|
Pausa();
|
|
}
|
|
}
|
|
catch (FormatException) {
|
|
error = true;
|
|
Console.WriteLine("Errore: dimensione non valida.");
|
|
Pausa();
|
|
}
|
|
catch (OverflowException) {
|
|
error = true;
|
|
Console.WriteLine("Errore: non sono ammesse dimensioni negative.");
|
|
Pausa();
|
|
}
|
|
} while (error);
|
|
|
|
return new int[righe, colonne];
|
|
}
|
|
|
|
static int[,] RiempiMatrice(int[,] p_matrix) {
|
|
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 {
|
|
p_matrix[r, c] = Convert.ToInt32(Console.ReadLine());
|
|
}
|
|
catch (FormatException) {
|
|
Console.WriteLine("Errore: elemento non valido.");
|
|
Pausa();
|
|
}
|
|
}
|
|
}
|
|
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.");
|
|
}
|
|
}
|
|
} |