C# :: Aufgabe #7

15 Lösungen Lösungen öffentlich

Übung mehrdimensionales Datenfeld welches aus zufälligen Inhalten besteht darstellen

Anfänger - C# von Gustl - 30.07.2012 um 23:37 Uhr
Schreiben sie ein Programm, in dem den Elementen eines dreidimensionalen Feldes, welches 6 x 3 x4 int-Werte beinhaltet, zufällige Werte zugewiesen werden.
Anschließend sollen alle Positionen des kleinsten Elements des Felds ermittelt und ausgegeben werden, siehe Bild.

Lösungen:

vote_ok
von Isparia (690 Punkte) - 08.11.2012 um 23:18 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace aufgabe_7
{/*Aufgabe:
  * Schreiben sie ein Programm, in dem den Elementen eines dreidimensionalen Feldes, welches 6 x 3 x4 int-Werte beinhaltet, 
  *zufällige Werte zugewiesen werden.
  *Anschließend sollen alle Positionen des kleinsten Elements des Felds ermittelt und ausgegeben werden, siehe Bild. */
    public partial class Form1 : Form
    {   /*Definition eines Arrays of Labels um die Zufallswerte anzuzeigen*/
        Label[,] labels = new Label[6, 3];
        /*Definition eines Arrays of Labels um die vorkommen anzuzeigen*/
        Label[] ausgabeLabel;
        /*Hilfsvariabelen:  - y zum festlegen der Y position der in den Arrays gespeicherten Labels
                            - x zum festlegen der X Position der in den Arrays gespeicherten Labels und als Hilfsvariabele
                            - z hilfsvariabele um die ermittelten Positionen auszugeben
                            - minZahl variabele für den kleinsten Zufallswert
                            - vorkommenMinZahl Hilfsvariabele zum bestimmen der größe des Label[] ausgabeLabel */
        int y ,z=0,x=15, zahl, minZahl = 0, vorkommenMinZahl = 0;
        /*Zahlenfeld welches in der Aufgabe gefordert war*/
        int[, ,] zahlenFeld = new int[6, 3, 4];
        /*Liste in welche die Positionen der kleinsten zahlen Zahlen gespeichert werden*/
        List<string> ausgabe = new List<string>();
        Random r = new Random();
        /*hilfsbool um sicher zugehen das auch wirklich die kleinste zahl ermittelt wurde wenn mein gedanken gang richtig ist^^*/
        bool zahlErmittelt = false;

        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 6; i++)
            {
                y += 25; 
                for (int j = 0; j < 3; j++)
                {
                    
                    labels[i, j] = new Label();                             //Initialiesieren eines Neuen Labels im LabelArray für die zufallswerte
                    labels[i, j].AutoSize = false;                          
                    labels[i, j].Bounds = new Rectangle(2, 2, 90, 25);      //festlegen der Größe des Neuen Labels
                    this.Controls.Add(labels[i, j]);                        //hinzufügen des Labels zur From
                    labels[i, j].Location = new Point(x, y);                //Label Positionieren
                    x += 90;
                    for (int k = 0; k < 4; k++)
                    {

                        zahl = r.Next(10, 100);                             //ermitteln der zufallszahl
                        
                        zahlenFeld[i, j, k] = zahl;
                        switch (k)                                          //überprüfen der Oosition der Zahl in der Gruppe um Formatierung anzupassen
                        {
                            case 0:
                                labels[i, j].Text += "( " + Convert.ToString(zahl);
                                break;
                            case 3:
                                labels[i, j].Text += " " + Convert.ToString(zahl) + " )";
                                break;
                            default:
                                labels[i, j].Text += " " + Convert.ToString(zahl);
                                break;
                        }

                    }

                }
                x = 15;
            }

        }
           
        private void button1_Click(object sender, EventArgs e)
        {
            x = 0;
            while (!zahlErmittelt)                                          //ermitteln von Minzahl
            {
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        for (int k = 0; k < 4; k++)
                        {
                            if (i == 0 && j ==0 &&k==0 && x ==0)
                            {
                                minZahl = zahlenFeld[i, j, k];
                                
                            }
                            else if (minZahl > zahlenFeld[i, j, k])
                            {
                                minZahl = zahlenFeld[i, j, k];
                            }
                            if (minZahl == zahlenFeld[i,j,k] && x== 2)      //ermitteln der Häufigkeit der minZahl
                            {
                                vorkommenMinZahl +=1;               
                            }else
                            if (minZahl == zahlenFeld[i, j, k] && x == 3)   //schreiben der Position in die Liste
                            {
                                ausgabe.Add("Zeile: " + (i+1) + "   Spalte: " + (j+1) + "   Item: " + (k+1));  
                            }

                        }
                    }
                }
                x += 1;
                if (x == 4) { zahlErmittelt = true; }                       //springen aus der Schleife
                
            }
            x = 15;                                                         //festlegen des x wertes für das 2.LabelArray                      
            y = 215;                                                        //festlegen des x wertes für das 2.LabelArray
            ausgabeLabel = new Label[vorkommenMinZahl];                     //definieren des 2.Labelarrays
            for (int i = 0; i < vorkommenMinZahl; i++)                      //erstellen der Labels zur laufzeit
            {
                    ausgabeLabel[i] = new Label();
                    ausgabeLabel[i].AutoSize = false;                   
                    ausgabeLabel[i].Bounds = new Rectangle(2, 2, 150, 25);
                    this.Controls.Add(ausgabeLabel[i]);
                    ausgabeLabel[i].Location = new Point(x, y);                                
                    y += 25;
            }
            label1.Text = "Minnimum: "+minZahl+", an Position:";            //Ausgabe der kleinsten Zahl
            foreach (string item in ausgabe)                                //Ausgabe der Positionen
            {
                ausgabeLabel[z].Text = item;
                z++;
            }
            z = 0;
            this.Height += ((z + 1) * 25);                                  //vergrößert die Form um alle Positionen sichtbar zu machen falls es mal mehr als 1 ist
            
        }

       
                
    }
}

vote_ok
von Mexx (2370 Punkte) - 05.05.2014 um 13:10 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Random3DArray
{
    class Program
    {
        static void Main(string[] args)
        {
            int[, ,] array3D = new int[6, 3, 4];
            Random ran = new Random();

            string test = "";
            int minvalue = int.MaxValue;
            for (int i = 0; i < array3D.GetLength(0); i++)
            {
                for (int j = 0; j < array3D.GetLength(1); j++)
                {
                    test += "( ";
                    for (int k = 0; k < array3D.GetLength(2); k++)
                    {
                        array3D[i, j, k] = ran.Next(20, 30);
                        test += array3D[i, j, k].ToString() + " ";
                        if (array3D[i, j, k] <= minvalue)
                        {
                            minvalue = array3D[i, j, k];
                        }
                    }
                    test += ")";
                }
                test += "\n \n";
            }
            Console.WriteLine(test);
            Console.WriteLine("Der kleinste Wert ist: " + minvalue + "\n");

            int count = 0;
            for (int i = 0; i < array3D.GetLength(0); i++)
            {
                for (int j = 0; j < array3D.GetLength(1); j++)
                {
                    for (int k = 0; k < array3D.GetLength(2); k++)
                    {
                        if (array3D[i, j, k] == minvalue)
                        {
                            count++;
                            Console.WriteLine("Übereinstimmung an Zeile " + i + ", Gruppe " + j + ", Element " + k + " gefunden");
                        }
                    }
                }
            }
        }
    }
}
vote_ok
von wladi-g (1310 Punkte) - 05.06.2014 um 15:04 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Windows.Forms;

namespace DatenfeldMehrdimensional
{
    public partial class Form1 : Form
    {
        Random rnd;
        int[, ,] arr;
        int min = 100;

        public Form1()
        {
            InitializeComponent();
            arr = new int[6, 3, 4];
            rnd = new Random();

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    textBox1.Text += "( ";
                    for (int k = 0; k < arr.GetLength(2); k++)
                    {
                        arr[i, j, k] = rnd.Next(10, 99);
                        textBox1.Text += arr[i, j, k].ToString() + " ";
                    }
                    textBox1.Text += ") ";
                }
                textBox1.Text += "\r\n";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (int i in arr)
                if (i < min)
                    min = i;
            textBox2.Text = "Minimum: " + min.ToString() + ", an Position:\r\n";
            for (int i = 0; i < arr.GetLength(0); i++)
                for (int j = 0; j < arr.GetLength(1); j++)
                    for (int k = 0; k < arr.GetLength(2); k++)
                        if (arr[i, j, k] == min)
                            textBox2.Text += "Zeile " + i + ", Gruppe " + j + ", Element " + k + "\r\n";
        }
    }
}
vote_ok
von aheiland (650 Punkte) - 10.03.2015 um 17:41 Uhr
Quellcode ausblenden C#-Code
    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();
        }
    }
vote_ok
von Mentalist999 (680 Punkte) - 14.03.2015 um 16:20 Uhr
Quellcode ausblenden C#-Code
        public static List<uint[]> MinPos(uint[,,] Feld)
        {
            List<uint[]> Result = new List<uint[]>();

            for (uint Base = uint.MaxValue, i = 0; i < Feld.GetLength(0); i++)

                for (uint x = 0; x < Feld.GetLength(1); x++)

                    for (uint y = 0; y < Feld.GetLength(2); y++)
                    {
                        if (Feld[i, x, y] > Base) continue;

                        else if (Feld[i, x, y] < Base)
                        {
                            Base = Feld[i, x, y];
                            Result.Clear();
                        }

                        Result.Add(new uint[] {i, x, y});
                    }

            return Result;
        }
vote_ok
von thareck (430 Punkte) - 14.03.2015 um 21:53 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _07MehrdimensionalesDatenfeld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Random random = new Random();
            int[, ,] IntArray3D = new int[6, 3, 4];

            InitializeComponent();

            for (int x = 0; x < IntArray3D.GetLength(0); x++)
            {
                for (int y = 0; y < IntArray3D.GetLength(1); y++)
                {
                    label1.Text += "( ";
                    for (int i = 0; i < IntArray3D.GetLength(2); i++)
                    {
                        IntArray3D[x, y, i] = random.Next(20, 30);
                        label1.Text += IntArray3D[x, y, i].ToString() + " ";
                    }
                    label1.Text += ") ";
                }
                label1.Text += "\n";
            }

            int minValue = (from int i in IntArray3D select i).Min();
            label2.Text += "Minimum: " + minValue + ", an Position: \n";

            for (int x = 0; x < IntArray3D.GetLength(0); x++)

                for (int y = 0; y < IntArray3D.GetLength(1); y++)

                    for (int i = 0; i < IntArray3D.GetLength(2); i++)

                        if (IntArray3D[x, y, i] == minValue)
                            label2.Text += "Zeile " + x + ". Gruppe " + y + ". Element " + i + "\n";
        }
    }
}

vote_ok
von l1k3_m3 (240 Punkte) - 01.04.2015 um 01:55 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dreidimensional
{
    public partial class Form1 : Form
    {
        Random rand = new Random();
        int[, ,] randomArray = new int[6, 3, 4];
        int min = 1000;

        public Form1()
        {
            InitializeComponent();
            arrayLabel.Text = "";

            for (int i = 0; i < randomArray.GetLength(0); i++)
            {
                for (int j = 0; j < randomArray.GetLength(1); j++)
                {
                    for (int k = 0; k < randomArray.GetLength(2); k++)
                    {
                        randomArray[i, j, k] = rand.Next(10, 50);
                        arrayLabel.Text += randomArray[i, j, k] + " ";
                    }
                    arrayLabel.Text += " |  ";
                }
                arrayLabel.Text += "\n";
            }
        }

        private void showBtn_Click(object sender, EventArgs e)
        {
            foreach (int value in randomArray)
            {
                if (value < min)
                {
                    min = value;
                    
                }
            }
            showLabel.Text = "Minimum: " + min.ToString() + " an Position:\n";
            for (int i = 0; i < randomArray.GetLength(0); i++)
            {
                for (int j = 0; j < randomArray.GetLength(1); j++)
                {
                    for (int k = 0; k < randomArray.GetLength(2); k++)
                    {
                        if(randomArray[i,j,k] == min)
                        {
                            showLabel.Text += "Zeile: " + i + " Gruppe: " + j + " Element: " + k + "\n";
                        }
                    }
                }
            }
        }
    }
}
vote_ok
von niknik (1230 Punkte) - 14.08.2015 um 14:14 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MinAusFeld
{
    class Program
    {
        static void Main(string[] args)
        {
            int[, ,] myField = new int[6, 3, 4];

            Random rnd = new Random();

            // Initialisiere Feld
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        myField[i, j, k] = rnd.Next();
                    }
                }
            }
            
            // Finde Minimum
            int min = myField[0, 0, 0];
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        if (myField[i, j, k] < min)
                        {
                            min = myField[i, j, k];
                        }
                    }
                }
            }

            // Finde Positionen
            int zähler = 0;
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        if (myField[i, j, k] == min)
                        {
                            zähler++;
                            Console.WriteLine("{0}. Position: Zeile {1}, Gruppe {2}, Element {3}", zähler, i, j, k);
                        }
                    }
                }
            }

            Console.WriteLine("Das Minimum ist {0} und kommt {1} mal vor.", min, zähler);

            Console.ReadLine();
        }
    }
}
vote_ok
von mty (690 Punkte) - 29.09.2015 um 13:11 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3Dimensions
{
    class Program
    {
        static void Main(string[] args)
        {
            int[, ,] x = new int[6, 3, 4];
            int kleinsteZahl = 101;
            List<string> positionen = new List<string>();
            Random random = new Random();


            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        x[i, j, k] = random.Next(0, 100);

                        if (kleinsteZahl > x[i, j, k])
                        {
                            kleinsteZahl = x[i, j, k];
                            positionen = new List<string>();
                        }

                        if(kleinsteZahl == x[i, j, k])
                            positionen.Add("Position: " + i.ToString() + "," + j.ToString() + "," + k.ToString());
                    } 
                }
            }

           

            Console.WriteLine("Kleinste Zahl: "+kleinsteZahl.ToString());
            foreach (string entry in positionen)
            {
                Console.WriteLine(entry);
            }
            Console.ReadKey();
        }
    }
}
vote_ok
von Shirkova (190 Punkte) - 03.07.2016 um 15:59 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,,] arr = new int[6,3,4];
            Random r = new Random();
            string print_array = "";
            string print_position = "";
            int minimum = int.MaxValue;

            //Initialize the array and prepare String print_array for printing out the array. Also remember the lowest value inserted.
            for(int i = 0; i < arr.GetLength(0); i++)
            {
                for(int j = 0; j < arr.GetLength(1); j++)
                {
                    print_array += "( ";
                    for(int x = 0; x < arr.GetLength(2); x++)
                    {
                        arr[i, j, x] = r.Next(20, 31);
                        print_array += arr[i, j, x] + " ";
                        if(minimum > arr[i,j,x])
                        {
                            minimum = arr[i, j, x];
                        }
                    }
                    print_array += ") ";
                }
                print_array += "\n";
            }

            //Loop through array and find positions of lowest value. Prepare String print_position for printing out the positions of the lowest values.
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    for (int x = 0; x < arr.GetLength(2); x++)
                    {
                        if(arr[i,j,x] == minimum)
                        {
                            print_position += string.Format("Zeile: {0} Gruppe: {1} Element: {2} \n",i,j,x);
                        }
                    }
                }
            }

            Console.WriteLine(print_array);
            Console.WriteLine();
            Console.WriteLine("Minimum: {0}, an Position: ", minimum);
            Console.WriteLine(print_position);



            Console.Read();
        }
    }
}

vote_ok
von kjaenke (1140 Punkte) - 03.08.2017 um 13:07 Uhr
Quellcode ausblenden C#-Code

  public static List<uint[]> BuildField(uint[,,] Field)
        {
            List<uint[]> Result = new List<uint[]>();

            for (uint a = uint.MaxValue, i = 0; i < Field.GetLength(0); i++)

            for (uint x = 0; x < Field.GetLength(1); x++)

            for (uint y = 0; y < Field.GetLength(2); y++)
            {
                if (Field[i, x, y] > a) continue;

                else if (Field[i, x, y] < a)
                {
                    a = Field[i, x, y];
                    Result.Clear();
                }

                Result.Add(new uint[] { i, x, y });
            }

            return Result;
        }

vote_ok
von stcalvin (970 Punkte) - 21.12.2017 um 11:54 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainYourProgrammer7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int[,,] zahlen = new int[6, 3, 4];
            Random zufall = new Random();
            int erstewert = 0;
            int zweitewert = 0;
            label1.Text = "";
            int minimum = 20;
            int maximum = 30;
            label2.Text = "Minimum: " + minimum + ", an Position:\n";
            

            for(int i = 0; i <= 3; i++)
            {
                int zahlrandom = zufall.Next(minimum, maximum);
                zahlen[erstewert, zweitewert, i] = zahlrandom;

                if (zahlrandom == minimum)
                {
                    label2.Text += "Zeile " + erstewert + ", Gruppe " + zweitewert + ", Element " + i + "\n";
                }

                switch (i)
                {
                    case 0:
                        label1.Text += "( ";
                        label1.Text += Convert.ToString(zahlrandom) + " ";
                        break;
                    case 3:
                        label1.Text += Convert.ToString(zahlrandom) + " ";
                        label1.Text += ") ";
                        break;
                    default:
                        label1.Text += Convert.ToString(zahlrandom) + " ";
                        break;
                }

                if(i == 3 && zweitewert < 2)
                {
                    zweitewert++;
                    i = -1;
                }

                if (i == 3 && zweitewert == 2 && erstewert < 5)
                {
                    erstewert++;
                    zweitewert = 0;
                    i = -1;
                    label1.Text += "\n";
                }
            }
        }
    }
}
vote_ok
von stbehl (1640 Punkte) - 03.01.2018 um 11:42 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainYourProgrammer7
{
    public partial class Form1 : Form
    {
        int[,,] array = new int[6, 3, 4];
        int kleinsteZahl = 2147483647;
        string StellekleinsteZahl;
        Random random = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        

        private void generieren_Click(object sender, EventArgs e)
        {
            kleinsteZahl = 2147483647;
            StellekleinsteZahl = "";
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    for (int x = 0; x < 4; x++)
                    {
                        array[i, j, x] = random.Next(20, 30);
                        if (array[i, j, x] < kleinsteZahl)
                        {
                            kleinsteZahl = array[i, j, x];
                            StellekleinsteZahl = String.Format("Zeile {0}, Gruppe {1}, Element {2}", i, j, x);
                        }else if (array[i, j, x] == kleinsteZahl)
                        {
                            StellekleinsteZahl += String.Format("\nZeile {0}, Gruppe {1}, Element {2}", i, j, x);
                        }

                    }
                }
            }

            string ausgabe = "";
            for (int i = 0; i < 6; i++)
            {
                if (i > 0)
                {
                    ausgabe += "\n";
                }
                for (int j = 0; j < 3; j++)
                {
                    for (int x = 0; x < 4; x++)
                    {
                        if (x == 0)
                        {
                            ausgabe += "(";
                        }
                        ausgabe += Convert.ToString(array[i, j, x]) + " ";
                        if (x == 3)
                        {
                            ausgabe += ") ";
                        }
                    }
                }
            }
            label1.Text = ausgabe;
        }

        private void Ausgabe_Click(object sender, EventArgs e)
        {
            label2.Text = "Minimum: " + Convert.ToString(kleinsteZahl) + ", an Position:" + "\n" + StellekleinsteZahl;
        }
    }
}
vote_ok
von grinchyy (200 Punkte) - 01.03.2018 um 18:02 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace trainyourprogrammer_7
{
    public partial class frmMain : Form
    {
        // AUFGABE:

        //Schreiben sie ein Programm, in dem den Elementen eines dreidimensionalen Feldes, welches 6 x 3 x4 int-Werte beinhaltet, 
        //zufällige Werte zugewiesen werden. Anschließend sollen alle Positionen des kleinsten Elements des Felds ermittelt und ausgegeben werden.*/


        // Dreidimensionales Array (6 hoch, 3 breit, 4 tief)
        int[,,] multiArray = new int[6, 3, 4];

        // Random Klasse
        Random ran = new Random();
        
        // Integervariable zum Speichern des Minimalwerts
        int min = 100;

        public frmMain()
        {
            InitializeComponent();

            // Multidimensionales Array mit Zufallszahlen (0-99) befüllen

            for ( int i = 0; i < multiArray.GetLength(0); i++ )
            {
                for ( int j = 0; j < multiArray.GetLength(1); j++ )
                {
                    for ( int k = 0; k < multiArray.GetLength(2); k++ )
                    {
                        multiArray[i, j, k] = ran.Next( 100 );
                    }
                }
            }

            // Multidimensionales Array in der Textbox des frmMain-Fensters anzeigen

            string aktuelleZahl = String.Empty;

            for ( int i = 0; i < multiArray.GetLength( 0 ); i++ )
            {
                
                for ( int j = 0; j < multiArray.GetLength( 1 ); j++ )
                {
                    tbxNumbers.Text += "(";

                    for ( int k = 0; k < multiArray.GetLength( 2 ); k++ )
                    {
                        tbxNumbers.Text += " ";                        

                        aktuelleZahl = multiArray[i, j, k].ToString();

                        // Damit alles bündig aussieht
                        if ( aktuelleZahl.Length == 1 )
                            tbxNumbers.Text += " " + aktuelleZahl;
                        else
                            tbxNumbers.Text += aktuelleZahl;
                    }
                    tbxNumbers.Text += " ) ";
                }

                tbxNumbers.Text += Environment.NewLine;
            }

            // Ermittlung des kleinsten Elements aus dem Array
           
            for ( int i = 0; i < multiArray.GetLength( 0 ); i++ )
            {
                for ( int j = 0; j < multiArray.GetLength( 1 ); j++ )
                {
                    for ( int k = 0; k < multiArray.GetLength( 2 ); k++ )
                    {
                        if ( multiArray[i, j, k] < min )
                            min = multiArray[i, j, k];
                    }
                }
            }
           

        }

        // Anzeigen der Koordinaten des kleinsten Elements

        private void btnAnzeigen_Click( object sender, EventArgs e )
        {
            
            tbxResult.Text += "Der Minimalwert von " + min + " befindet sich an den folgenden Positionen: " + Environment.NewLine;

            for ( int i = 0; i < multiArray.GetLength( 0 ); i++ )
            {
                for ( int j = 0; j < multiArray.GetLength( 1 ); j++ )
                {
                    for ( int k = 0; k < multiArray.GetLength( 2 ); k++ )
                    {
                        if ( multiArray[i, j, k] == min )
                        {
                            tbxResult.Text += "Zeile " + (i+1) + ", Gruppe " + (j+1) + ", Element " + (k+1) + Environment.NewLine;

                        }
                            
                    }
                }
            }

        }
    }
}

vote_ok
von DanielWagner (220 Punkte) - 16.12.2019 um 14:35 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace _3DArray_Aufgabe7
{
    class Program
    {
        static Random rand = new Random();
        static void Main(string[] args)
        {
            int[,,] dreiDArray = new int[6, 3, 4];

            // Array füllen
            for (int a = 0; a < dreiDArray.GetLength(0); a++)
            {
                for (int b = 0; b < dreiDArray.GetLength(1); b++)
                {
                    for(int c = 0; c < dreiDArray.GetLength(2); c++)
                    {
                        dreiDArray[a,b,c] = rand.Next(1,101);
                    }
                }
            }

            // Array ausgeben und die kleinste Elemente finden
            int element = dreiDArray[0, 0, 0];
           

            for (int a = 0; a < dreiDArray.GetLength(0); a++)
            {
                for (int b = 0; b < dreiDArray.GetLength(1); b++)
                {
                    Console.Write("(");
                    for (int c = 0; c < dreiDArray.GetLength(2); c++)
                    {
                        Console.Write(dreiDArray[a, b, c] + " ");
                        // das kleinste Element merken
                        if (dreiDArray[a, b, c] <= element)
                            element = dreiDArray[a, b, c];
                                      
                    }

                    Console.Write(")");
                }
                Console.WriteLine();
            }

            // Treffer zählen
            int count = 0;

            for (int a = 0; a < dreiDArray.GetLength(0); a++)
            {
                for (int b = 0; b < dreiDArray.GetLength(1); b++)
                {
                    for (int c = 0; c < dreiDArray.GetLength(2); c++)
                    {
                        if (dreiDArray[a, b, c] == element)
                            count++;
                    }
                }
            }


            // die kleinste Elemente ausgeben
            Console.WriteLine("\nMinimum {0}, an Position: ", count);

            for (int a = 0; a < dreiDArray.GetLength(0); a++)
            {
                for (int b = 0; b < dreiDArray.GetLength(1); b++)
                {
                    for (int c = 0; c < dreiDArray.GetLength(2); c++)
                    {
                        if (dreiDArray[a, b, c] == element)
                            Console.WriteLine($"Zeile: {a}, Gruppe {b}, Element {c} -> Die Zahl: {dreiDArray[a, b, c]}");
                               
                    }
                }
            }


            Console.ReadKey();

        }
    }
}