268 lines
10 KiB
C#
268 lines
10 KiB
C#
namespace matrix_3;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Menu();
|
|
}
|
|
|
|
static void Menu() {
|
|
Console.Clear();
|
|
int scelta;
|
|
int[,] matrix1 = null, matrix2;
|
|
do {
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine("Inserire un'opzione:");
|
|
Console.WriteLine("1. Crea e mostra matrice quadrata");
|
|
Console.WriteLine("2. Somma diagonale secondaria");
|
|
Console.WriteLine("3. Media diagonale secondaria");
|
|
Console.WriteLine("4. Prodotto diagonale secondaria");
|
|
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());
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
Console.Clear();
|
|
matrix1 = CreaMatriceQuadrata();
|
|
Console.Clear();
|
|
Console.WriteLine("La matrice creata è la seguente:");
|
|
MostraMatrice(matrix1);
|
|
Pausa();
|
|
break;
|
|
case 2:
|
|
Console.Clear();
|
|
if (matrix1 != null) {
|
|
Console.WriteLine($"La somma degli elementi della diagonale secondaria è {SommaMatriceDiagonale2(matrix1)}");
|
|
}
|
|
else {
|
|
Console.WriteLine("È necessario creare la matrice prima di calcolare la somma della diagonale secondaria.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 3:
|
|
Console.Clear();
|
|
if (matrix1 != null) {
|
|
Console.WriteLine($"La media degli elementi della diagonale secondaria è {double.Round(Convert.ToDouble(SommaMatriceDiagonale2(matrix1)) / Convert.ToDouble(matrix1.GetLength(0)), 3)}");
|
|
}
|
|
else {
|
|
Console.WriteLine("È necessario creare la matrice prima di calcolare la media della diagonale secondaria.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 4:
|
|
Console.Clear();
|
|
if (matrix1 != null) {
|
|
Console.WriteLine($"Il prodotto degli elementi della diagonale secondaria è {ProdottoMatriceDiagonale2(matrix1)}");
|
|
}
|
|
else {
|
|
Console.WriteLine("È necessario creare la matrice prima di calcolare il prodotto della diagonale secondaria.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 5:
|
|
Console.Clear();
|
|
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 secondaria.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 6:
|
|
Console.Clear();
|
|
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 {
|
|
MostraMatrice(SommaMatrici(matrix1, matrix2));
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 7:
|
|
Console.Clear();
|
|
matrix1 = CreaMatriceQuadrata();
|
|
Console.Clear();
|
|
Console.WriteLine("La matrice trasposta è");
|
|
MostraMatrice(CreaTrasposta(matrix1));
|
|
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[,] CreaMatriceQuadrata() {
|
|
int dimensione = 0;
|
|
Console.Write("Inserire la dimensione della matrice: ");
|
|
dimensione = Convert.ToInt32(Console.ReadLine());
|
|
int[,] ritorno = new int[dimensione, dimensione];
|
|
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 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 int SommaMatriceDiagonale2(int[,] p_matrice) {
|
|
int ritorno = 0;
|
|
for (int r = 0; r < p_matrice.GetLength(0); r++) {
|
|
ritorno = ritorno + p_matrice[r, p_matrice.GetLength(1) - 1 - r];
|
|
}
|
|
return ritorno;
|
|
}
|
|
|
|
static int ProdottoMatriceDiagonale2(int[,] p_matrice) {
|
|
int ritorno = 1;
|
|
for (int r = 0; r < p_matrice.GetLength(0); r++) {
|
|
ritorno = ritorno * p_matrice[r, p_matrice.GetLength(1) - 1 - r];
|
|
}
|
|
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;
|
|
}
|
|
|
|
static int[,] CreaTrasposta(int[,] p_matrice) {
|
|
int[,] ritorno = new int[p_matrice.GetLength(0), p_matrice.GetLength(1)];
|
|
for (int r = 0; r < p_matrice.GetLength(0); r++) {
|
|
for (int c = 0; c < p_matrice.GetLength(1); c++) {
|
|
ritorno[r, c] = p_matrice[c, r];
|
|
}
|
|
}
|
|
return ritorno;
|
|
}
|
|
|
|
}
|