C# :: Aufgabe #7 :: Lösung #10

15 Lösungen Lösungen öffentlich
#7

Ü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.
#10
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();
        }
    }
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben