C# :: Aufgabe #234
6 Lösungen

Array Vorkommen programmieren
Anfänger - C#
von Bryan
- 31.10.2018 um 20:20 Uhr
Hallo Zusammen
Kann Jemand von Euch mir da weiter helfen?
Fragestellung des Vorkommen.
Gegeben ist ein Array zum Beispiel mit Zahlen als Werten:
{2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14}
Schreiben Sie ein Programm, das von einem Array bestehend aus Zahlen prüft, wie viele dieser
Zahlen durch 3 teilbar sind.
Zudem sollen am Schluss die entsprechenden Zahlen ausgegeben werden. Das Programm soll für
beliebige Arrays funktionieren.
Vielen Dank für eure Lösung
Kann Jemand von Euch mir da weiter helfen?
Fragestellung des Vorkommen.
Gegeben ist ein Array zum Beispiel mit Zahlen als Werten:
{2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14}
Schreiben Sie ein Programm, das von einem Array bestehend aus Zahlen prüft, wie viele dieser
Zahlen durch 3 teilbar sind.
Zudem sollen am Schluss die entsprechenden Zahlen ausgegeben werden. Das Programm soll für
beliebige Arrays funktionieren.
Vielen Dank für eure Lösung
Lösungen:
Funktioniert für Listen und Arrays durch die überladene check Methode.
C#-Code

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ArrayElementeTeilbarDurchDrei_234 { class Program { private static List<int> results; private static List<int> numbers1; private static int[] numbers2; private static int divider; static void Main(string[] args) { Program.results = new List<int>(); Program.numbers1 = new List<int>() { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 }; Program.numbers2 = new int[16] { 100, 200, 300, 400, 500, 600, 777, 42, 999, 666, 123, 456, 789, 0, -5, 100000 }; Program.divider = 3; Program.check(numbers1); Program.check(numbers2); Program.printResults(); Console.ReadLine(); } /// <summary> /// Gibt die Ergebnisse aus. /// </summary> private static void printResults() { foreach (int result in Program.results) { Console.WriteLine(result); } Console.WriteLine("----------------------------"); Console.WriteLine("Es sind somit {0} Zahlen durch {1} teilbar.", Program.results.Count, Program.divider); } /// <summary> /// Prüft die übergebenen Zahlen, ob diese durch Program.divider teilbar sind. /// </summary> /// <param name="numbers">Die zu prüfenden Zahlen</param> private static void check(List<int> numbers) { foreach (int number in numbers) { if (number % Program.divider == 0) { Program.results.Add(number); } } } /// <summary> /// Prüft die übergebenen Zahlen, ob diese durch Program.divider teilbar sind. /// </summary> /// <param name="numbers">Die zu prüfenden Zahlen</param> private static void check(int[] numbers) { foreach (int number in numbers) { if (number % Program.divider == 0) { Program.results.Add(number); } } } } }
hier meine Lösung dazu...
C#-Code

using System; using System.Collections.Generic; using System.Text; namespace Aufgabe234 { class Program { static void Main(string[] args) { int[] source = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 }; MyArray testArray = new MyArray(source,3); Console.WriteLine(testArray); Console.ReadLine(); } } public class MyArray { private List<int> data; private int divisor; public int[] Data { get => data.ToArray(); } public MyArray(int[] source, int divisor) { this.divisor = divisor; data = new List<int>(); foreach (int item in source) if (item % divisor == 0) { if (!data.Contains(item)) data.Add(item); } data.Sort(); } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.AppendLine(string.Format("{0} Elemente sind durch {1} teilbar", data.Count, divisor)); sb.AppendLine("Folgende Elemente sind im Array enthalten:"); sb.Append("{ "); bool firstItem = true; foreach (int item in data) { if (!firstItem) sb.Append(", "); else firstItem = false; sb.Append(item); } sb.Append(" }"); return sb.ToString(); } } }
Die Aufgabenstellung "soll für beliebige Arrays funktionieren" wurde auf 1-, 2- und 3-dimensionale Arrays zur Demo begrenzt.
C#-Code

namespace EvaluateDevideByThree { class Program { static void Main(string[] args) { int[] numbers1D = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 }; int[,] numbers2D = { { 2, 17 }, { 10, 9 }, { 16, 3 }, { 9, 16 }, { 5, 1 }, { 17, 14 } }; int[,,] numbers3D = { { { 2, 17, 10 }, { 9, 16, 3 }, { 9, 16, 5 } }, { { 1, 17, 14 }, {44, 36, -9}, {123, 1, 8 } } }; Console.WriteLine("1-dimensionales Array:\n"); foreach (int i in numbers1D) { EvaluateDivision(i); } Console.WriteLine("\n2-dimensionales Array:\n"); foreach (int i in numbers2D) { EvaluateDivision(i); } Console.WriteLine("\n3-dimensionales Array:\n"); foreach (int i in numbers3D) { EvaluateDivision(i); } Console.ReadKey(); } static void EvaluateDivision(int number) { if (number % 3 == 0) { Console.WriteLine("\t{0} ist restlos durch 3 teilbar.", number); } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApp36 { class Program { #region Aufrufe(Main) static void Main(string[] args) { Berechnung(); Console.ReadKey(); } #endregion #region Berechnung private static void Berechnung() { double[] eingabeArray = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 }; ArrayList ergebnisse = new ArrayList(); foreach (double wert in eingabeArray) { double ber = wert % 3; if(ber == 0) { ergebnisse.Add(wert); } else { continue; } } foreach(double data in ergebnisse) { Console.WriteLine(data); } } #endregion } }

using System; namespace ConsoleApp68 { class Program { static void Main(string[] args) { int[] numbers = {2,17,10,9,16,3,9,16,5,1,17,14 }; int output = 0; for (int i = 0; i < numbers.Length; i++) { if(numbers[i] % 3 == 0) { output = output + numbers[i]; } } Console.WriteLine($"Alle Zahlen die durch 3 teilbar sind: {output}"); Console.ReadKey(); } } }
Ich bin mir nicht zu 100 Prozent sicher ob das Programm stimmt da ich erst seit 4 Monaten mit programmieren und mit C# begonnen habe

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _234 { class Program { static void Main(string[] args) { int[] numbers = { 1, 23, 63, 12, 78, 121, 346, 135, 43, 17, 85, 4, 98 }; List<int> results = new List<int>(); int counter = 0; foreach(int zahl in numbers) { if(zahl%3==0) { results.Add(zahl); counter++; } } Console.WriteLine(counter + " Zahlen sind durch 3 teilbar"); foreach (int zahl in results) Console.WriteLine(zahl); Console.ReadLine(); } } }