117 lines
3.5 KiB
C#
117 lines
3.5 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:
|
|
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 RiempiMatrice(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());
|
|
}
|
|
catch (FormatException) {
|
|
Console.WriteLine("Errore: elemento non valido.");
|
|
Pausa();
|
|
}
|
|
}
|
|
}
|
|
|
|
return p_matrix;
|
|
}
|
|
}
|