C# :: Aufgabe #7 :: Lösung #4
15 Lösungen
#7
Übung mehrdimensionales Datenfeld welches aus zufälligen Inhalten besteht darstellen
Anfänger - C#
von Gustl
- 30.07.2012 um 23:37 Uhr
#4
von aheiland (650 Punkte)
- 10.03.2015 um 17:41 Uhr
class Program
{
static void Main(string[] args)
{
ZahlenBox box = new ZahlenBox(6,3,4);
Console.WriteLine(box);
int lowest = box.find((x, y) => x < y);
Console.WriteLine(lowest);
Console.WriteLine(String.Join("\n", box.whereAre(lowest)));
Console.ReadKey();
}
}
class ZahlenBox
{
private int[,,] matrix;
public ZahlenBox(int p1, int p2, int p3)
{
Random zufall = new Random();
matrix = new int[p1, p2, p3];
for (int x = 0; x < matrix.GetLength(0); x++ )
{
for (int y = 0; y < matrix.GetLength(1); y++)
{
for (int z = 0; z < matrix.GetLength(2); z++)
{
matrix[x, y, z] = zufall.Next(100);
}
}
}
}
internal int find(Func<int,int,bool> Comperator)
{
int? save = null;
foreach (var cell in matrix)
{
if (save == null) save = cell; else if (Comperator( cell,(int)save)) save = cell;
}
return (int)save;
}
public override string ToString()
{
string res = "[";
for (int x = 0; x < matrix.GetLength(0); x++)
{
res = res + "{";
for (int y = 0; y < matrix.GetLength(1); y++)
{
res = res + "(";
for (int z = 0; z < matrix.GetLength(2); z++)
{
res = res + matrix[x, y, z].ToString("00") + ", ";
}
res = res.Substring(0, res.Length - 2);
res = res + "), ";
}
res = res.Substring(0, res.Length - 2);
res = res + "},\n ";
}
res = res.Substring(0, res.Length - 3);
res = res + "]";
return res;
}
internal string[] whereAre(int entry)
{
List<string> resL = new List<string>();
for (int x = 0; x < matrix.GetLength(0); x++)
{
for (int y = 0; y < matrix.GetLength(1); y++)
{
for (int z = 0; z < matrix.GetLength(2); z++)
{
if (matrix[x, y, z] == entry) resL.Add("Zeile: " + x + " Gruppe: " + y + " Element: " + z);
}
}
}
return resL.ToArray();
}
}
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
