matrix_1_variante/Program.cs
La Programmatrice Verde 5e2c571db6 Funziona tutto
2025-03-07 10:03:45 +01:00

78 lines
2.5 KiB
C#

namespace matrix_1_variante;
class Program {
static void Main(string[] args) {
MostraMinimoMatrice(MinimoMatrice(CreaMatrice()));
}
static int[,] CreaMatrice() {
int righe = 0, colonne = 0;
Console.Write("Inserire le righe della matrice: ");
righe = Convert.ToInt32(Console.ReadLine());
Console.Write("Inserire le colonne della matrice: ");
colonne = Convert.ToInt32(Console.ReadLine());
int[,] ritorno = new int[righe, colonne];
RiempiMatriceRandom(ritorno);
return ritorno;
}
static void RiempiMatriceRandom(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[,] MinimoMatrice(int[,] p_matrice) {
int min = int.MaxValue;
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] < min) {
min = p_matrice[r, c];
}
}
}
return TrovaMatrice(p_matrice, min);
}
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++) {
for (int c = 0; c < p_matrice.GetLength(1); c++) {
if (p_matrice[r, c] == p_DaTrovare) {
ritorno[r, c] = 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 MostraMinimoMatrice(int[,] p_matrice) {
bool trovato = false;
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 minimo è {p_matrice[r, c]} e si trova nelle posizioni:");
trovato = true;
}
Console.WriteLine($"{r}, {c}");
}
}
}
}
}