219 lines
8.2 KiB
C#
219 lines
8.2 KiB
C#
using System.Reflection.Metadata;
|
|
|
|
namespace matrix_2;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Menu();
|
|
}
|
|
|
|
static void Menu() {
|
|
Console.Clear();
|
|
int scelta;
|
|
int[,] matrix = null;
|
|
int[] array = null;
|
|
do {
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine("Inserire un'opzione:");
|
|
Console.WriteLine("1. Crea matrice");
|
|
Console.WriteLine("2. Stampa matrice");
|
|
Console.WriteLine("3. Somma diagonale principale");
|
|
Console.WriteLine("4. Media diagonale principale");
|
|
Console.WriteLine("5. Prodotto diagonale principale");
|
|
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("0. Esci");
|
|
Console.Write("Scelta: ");
|
|
scelta = Convert.ToInt32(Console.ReadLine());
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
Console.Clear();
|
|
matrix = CreaMatrice();
|
|
Pausa();
|
|
break;
|
|
case 2:
|
|
Console.Clear();
|
|
if (matrix != null) {
|
|
MostraMatrice(matrix);
|
|
}
|
|
else {
|
|
Console.WriteLine("È necessario creare la matrice prima di mostrarla.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 3:
|
|
Console.Clear();
|
|
if (matrix != null) {
|
|
Console.WriteLine($"La somma degli elementi della diagonale principale è {SommaMatriceDiagonale(matrix)}");
|
|
}
|
|
else {
|
|
Console.WriteLine("È necessario creare la matrice prima di calcolare la somma della diagonale principale.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 4:
|
|
Console.Clear();
|
|
if (matrix != null) {
|
|
Console.WriteLine($"La media degli elementi della diagonale principale è {SommaMatriceDiagonale(matrix) / matrix.GetLength(0)}");
|
|
}
|
|
else {
|
|
Console.WriteLine("È necessario creare la matrice prima di calcolare la media della diagonale principale.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 5:
|
|
Console.Clear();
|
|
if (matrix != null) {
|
|
Console.WriteLine($"Il prodotto degli elementi della diagonale principale è {ProdottoMatriceDiagonale(matrix)}");
|
|
}
|
|
else {
|
|
Console.WriteLine("È necessario creare la matrice prima di calcolare il prodotto della diagonale principale.");
|
|
}
|
|
Pausa();
|
|
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]}");
|
|
}
|
|
else {
|
|
Console.WriteLine("È necessario creare la matrice prima di calcolare il minimo e il massimo della diagonale principale.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 7:
|
|
Console.Clear();
|
|
if (matrix != null) {
|
|
if (TrovaDiagonale(matrix, 0)) {
|
|
Console.WriteLine("Sulla diagonale principale sono presenti degli zeri");
|
|
}
|
|
else {
|
|
Console.WriteLine("Sulla diagonale principale non sono presenti degli zeri");
|
|
}
|
|
}
|
|
else {
|
|
Console.WriteLine("È necessario creare la matrice prima di cercare gli zeri sulla diagonale principale.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 8:
|
|
Console.Clear();
|
|
if (matrix != null) {
|
|
Console.Write("Inserire un numero da cercare: ");
|
|
if (TrovaDiagonale(matrix, Convert.ToInt32(Console.ReadLine()))) {
|
|
Console.WriteLine("Elemento trovato");
|
|
}
|
|
else {
|
|
Console.WriteLine("Elemento non trovato");
|
|
}
|
|
}
|
|
else {
|
|
Console.WriteLine("È necessario creare la matrice prima di cercare un elemento sulla diagonale principale.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
default:
|
|
Console.WriteLine("Opzione non valida.");
|
|
Pausa();
|
|
break;
|
|
}
|
|
}
|
|
while (scelta != 0);
|
|
}
|
|
|
|
static void Pausa() {
|
|
Console.WriteLine("Premere un tasto per continuare. . .");
|
|
Console.ReadKey();
|
|
Console.Clear();
|
|
}
|
|
|
|
static int[,] CreaMatrice() {
|
|
int dimensione = 0;
|
|
Console.Write("Inserire le dimensioni della matrice: ");
|
|
dimensione = Convert.ToInt32(Console.ReadLine());
|
|
int[,] ritorno = new int[dimensione, dimensione];
|
|
RiempiMatrice(ritorno);
|
|
return ritorno;
|
|
}
|
|
static void MostraMatrice(int[,] p_matrice) {
|
|
for (int r = 0; r < p_matrice.GetLength(0); r++) {
|
|
for (int c = 0; c < p_matrice.GetLength(1); c++) {
|
|
Console.Write(p_matrice[r, c] + " ");
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
static void RiempiMatrice(int[,] p_matrice) {
|
|
Random rnd = new Random();
|
|
for (int r = 0; r < p_matrice.GetLength(0); r++) {
|
|
for (int c = 0; c < p_matrice.GetLength(1); c++) {
|
|
p_matrice[r, c] = rnd.Next(101);
|
|
}
|
|
}
|
|
}
|
|
|
|
static int SommaMatriceDiagonale(int[,] p_matrice) {
|
|
int ritorno = 0;
|
|
for (int r = 0; r < p_matrice.GetLength(0); r++) {
|
|
ritorno = ritorno + p_matrice[r, r];
|
|
}
|
|
return ritorno;
|
|
}
|
|
|
|
static int ProdottoMatriceDiagonale(int[,] p_matrice) {
|
|
int ritorno = 1;
|
|
for (int r = 0; r < p_matrice.GetLength(0); r++) {
|
|
ritorno = ritorno * p_matrice[r, r];
|
|
}
|
|
return ritorno;
|
|
}
|
|
|
|
static int[] MinimoMassimoMatriceDiagonale(int[,] p_matrice, int p_scelta) {
|
|
int[] ritorno = new int[3];
|
|
switch (p_scelta) {
|
|
case 0:
|
|
ritorno[0] = Int32.MaxValue;
|
|
for (int r = 0; r < p_matrice.GetLength(0); r++) {
|
|
if (p_matrice[r, r] < ritorno[0]) {
|
|
ritorno[0] = p_matrice[r, r];
|
|
ritorno[1] = r;
|
|
ritorno[2] = r;
|
|
}
|
|
}
|
|
|
|
break;
|
|
case 1:
|
|
ritorno[0] = Int32.MinValue;
|
|
for (int r = 0; r < p_matrice.GetLength(0); r++) {
|
|
if (p_matrice[r, r] > ritorno[0]) {
|
|
ritorno[0] = p_matrice[r, r];
|
|
ritorno[1] = r;
|
|
ritorno[2] = r;
|
|
}
|
|
|
|
}
|
|
break;
|
|
}
|
|
|
|
return ritorno;
|
|
}
|
|
|
|
static bool TrovaDiagonale(int[,] p_matrice, int p_DaTrovare) {
|
|
bool ritorno = false;
|
|
for (int r = 0; r < p_matrice.GetLength(0) && !ritorno; r++) {
|
|
if (p_matrice[r, r] == p_DaTrovare) {
|
|
ritorno = true;
|
|
}
|
|
}
|
|
return ritorno;
|
|
}
|
|
}
|