diff --git a/BalkenDiagramm/BalkenDiagramm.cs b/BalkenDiagramm/BalkenDiagramm.cs new file mode 100644 index 0000000000000000000000000000000000000000..16c35931da85b802567fccbddad7272f04330b65 --- /dev/null +++ b/BalkenDiagramm/BalkenDiagramm.cs @@ -0,0 +1,27 @@ +namespace Uebung; + +class BalkenDiagramm +{ + // Zeichnet ein BalkenDiagramm mit den Werten des Feldes f + static void ZeichneBalkenDiagramm(int[] feld) + { + for (int i = 0; i < feld.Length; i++) + { + Console.Write("{0,2} ", feld[i]); + for (int j = 0; j < feld[i]; j++) + Console.Write('\u2584'); // Zeichen "Halbblock unten" + Console.WriteLine(); + } + } + + static void Main(string[] args) + { + Console.OutputEncoding = System.Text.Encoding.UTF8; + + int[] f = { 10, 20, 15, 18, 2, 6 }; + + ZeichneBalkenDiagramm(f); + + Console.WriteLine("\n\n\n"); + } +} diff --git a/BalkenDiagramm/BalkenDiagramm.csproj b/BalkenDiagramm/BalkenDiagramm.csproj new file mode 100644 index 0000000000000000000000000000000000000000..206b89a9a8b9320db4b017a262b565f104489193 --- /dev/null +++ b/BalkenDiagramm/BalkenDiagramm.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/FeldUebung/FeldUebung.cs b/FeldUebung/FeldUebung.cs new file mode 100644 index 0000000000000000000000000000000000000000..968a9e2d8a69d90088711d5081cd72e1798afa7d --- /dev/null +++ b/FeldUebung/FeldUebung.cs @@ -0,0 +1,65 @@ +namespace Uebung; + +class Program +{ + // Ausgabe Feld f + static void Ausgabe(int[] f) + { + Console.Write("f = ["); + for (int i = 0; i < f.Length; i++) + Console.Write((i != 0 ? ", " : "") + f[i]); // Trick für das Komma erspart ein "if" + Console.WriteLine("]\n"); + } + + // Statistik, berechnet summe, durchschnitt, min, max + static void FeldStatistik(int[] f, out int summe, out double durchschnitt, out int min, out int max) + { + summe = 0; + min = f[0]; + max = f[0]; + + for (int i = 0; i < f.Length; i++) + { + summe += f[i]; + if (f[i] < min) + min = f[i]; + if (f[i] > max) + max = f[i]; + } + + // Type-Cast zur Vermeidung von Integer-Division + durchschnitt = (double)summe / f.Length; + } + + static void Main() + { + int[] f = { 5, 8, 6, 4, 3, 5, 9, 2, 6, 9 }; + + Ausgabe(f); + + FeldStatistik(f, out int summe, out double durchschnitt, out int min, out int max); + Console.WriteLine("Summe: " + summe); + Console.WriteLine("Durchschnitt: " + durchschnitt); + Console.WriteLine("Maximum: " + max); + Console.WriteLine("Minimum: " + min); + + // Zahl einlesen + Console.Write("Zahl eingeben: "); + int zahl = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Größer sind: "); + + for (int i = 0; i < f.Length; i++) + { + if (f[i] > zahl) + Console.Write(f[i] + " "); + } + Console.WriteLine("\n"); + + // C#-Standard-Funktionalität mit Linq + Console.WriteLine($"Summe durch Linq: {f.Sum()}"); + Console.WriteLine(); + Console.WriteLine("Maximum durch Linq: " + f.Max()); + Console.WriteLine("Minimum durch Linq: " + f.Min()); + } +} diff --git a/FeldUebung/FeldUebung.csproj b/FeldUebung/FeldUebung.csproj new file mode 100644 index 0000000000000000000000000000000000000000..206b89a9a8b9320db4b017a262b565f104489193 --- /dev/null +++ b/FeldUebung/FeldUebung.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/QuersummeRekursiv/QuersummeRekursiv.cs b/QuersummeRekursiv/QuersummeRekursiv.cs new file mode 100644 index 0000000000000000000000000000000000000000..ba58d0e9e6c2fd63314bb1dd1b9f27e3f9ca1291 --- /dev/null +++ b/QuersummeRekursiv/QuersummeRekursiv.cs @@ -0,0 +1,36 @@ +namespace Uebung; + +class Program +{ + // Zum Vergleich nochmal iterativ + static int QuerSumme(int zahl) + { + int q = 0; + + while (zahl > 0) + { + q += zahl % 10; + zahl /= 10; + } + + return q; + } + + // Jetzt rekursiv. Elegant, oder? + static int QuerSummeRek(int zahl) + { + if (zahl < 10) + return zahl; + else + return zahl % 10 + QuerSummeRek(zahl / 10); + } + + static void Main(string[] args) + { + Console.Write("Zahl eingeben: "); + int zahl = Convert.ToInt32(Console.ReadLine()); + + Console.WriteLine($"QuerSumme({zahl}) = {QuerSumme(zahl)}"); + Console.WriteLine($"QuerSummeRek({zahl}) = {QuerSummeRek(zahl)}"); + } +} diff --git a/QuersummeRekursiv/QuersummeRekursiv.csproj b/QuersummeRekursiv/QuersummeRekursiv.csproj new file mode 100644 index 0000000000000000000000000000000000000000..206b89a9a8b9320db4b017a262b565f104489193 --- /dev/null +++ b/QuersummeRekursiv/QuersummeRekursiv.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/Reverse/Reverse.cs b/Reverse/Reverse.cs new file mode 100644 index 0000000000000000000000000000000000000000..a87f3e61503de8a1ef1b3c5b1831d47e42dedfcf --- /dev/null +++ b/Reverse/Reverse.cs @@ -0,0 +1,33 @@ +namespace Uebung; + +class Program +{ + // Dreht den Feld-Inhalt um + static void Reverse(int[] a) + { + for (int i = 0; i < a.Length / 2; i++) + { + int temp = a[i]; + a[i] = a[a.Length - 1 - i]; // oder a[^(1+i)] + a[a.Length - 1 - i] = temp; + } + } + + static void Main() + { + int[] f = { 6, 7, 4, 3, 2, 5, 9 }; + + Reverse(f); + + // jetzt sollte f = { 9, 5, 2, 3, 4, 7, 6 } sein + for (int i = 0; i < f.Length; i++) + Console.Write(f[i] + " "); + Console.WriteLine(); + + // Original-Reihenfolge mit Standard-Funktion wieder herstellen + Array.Reverse(f); + for (int i = 0; i < f.Length; i++) + Console.Write(f[i] + " "); + Console.WriteLine(); + } +} \ No newline at end of file diff --git a/Reverse/Reverse.csproj b/Reverse/Reverse.csproj new file mode 100644 index 0000000000000000000000000000000000000000..206b89a9a8b9320db4b017a262b565f104489193 --- /dev/null +++ b/Reverse/Reverse.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/SelectionSort/SelectionSort.cs b/SelectionSort/SelectionSort.cs new file mode 100644 index 0000000000000000000000000000000000000000..9d079cdbdf3c92d79a63c040e2f8dff052f61faf --- /dev/null +++ b/SelectionSort/SelectionSort.cs @@ -0,0 +1,36 @@ +namespace Uebung; + +class Program +{ + // Selection Sort + static void Sort(int[] f) + { + for (int i = 0; i < f.Length; i++) + { + // Suche Index der nächstkleinsten Zahl + int minIdx = i; + for (int j = i + 1; j < f.Length; j++) + { + if (f[j] < f[minIdx]) + minIdx = j; + } + + // tausche Feldwert mit nächstkleinster Zahl + int tmp = f[i]; + f[i] = f[minIdx]; + f[minIdx] = tmp; + } + } + + // Hauptprogramm + static void Main() + { + int[] a = { 5, 8, 3, 2, 9, 8 }; + + Sort(a); + + for (int i = 0; i < a.Length; i++) + Console.Write(a[i] + " "); + Console.WriteLine(); + } +} diff --git a/SelectionSort/SelectionSort.csproj b/SelectionSort/SelectionSort.csproj new file mode 100644 index 0000000000000000000000000000000000000000..206b89a9a8b9320db4b017a262b565f104489193 --- /dev/null +++ b/SelectionSort/SelectionSort.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/Uebung07.sln b/Uebung07.sln new file mode 100644 index 0000000000000000000000000000000000000000..6487b2bc434112df527ba95a1ae7a031b1bbcb58 --- /dev/null +++ b/Uebung07.sln @@ -0,0 +1,49 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34616.47 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FeldUebung", "FeldUebung\FeldUebung.csproj", "{859CF903-14E3-479E-844D-E9EBBFADCA71}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Reverse", "Reverse\Reverse.csproj", "{E122E1CB-44BC-4E4B-AA9E-1DC9CAD50E9F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BalkenDiagramm", "BalkenDiagramm\BalkenDiagramm.csproj", "{BC9C48C1-69EA-43D9-BAB0-C528DDC06484}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SelectionSort", "SelectionSort\SelectionSort.csproj", "{0A0E6444-24E7-4AB1-84F8-F8D539009DE1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuersummeRekursiv", "QuersummeRekursiv\QuersummeRekursiv.csproj", "{70BC7F95-242F-4839-8D1D-7BE15FC787D5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {859CF903-14E3-479E-844D-E9EBBFADCA71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {859CF903-14E3-479E-844D-E9EBBFADCA71}.Debug|Any CPU.Build.0 = Debug|Any CPU + {859CF903-14E3-479E-844D-E9EBBFADCA71}.Release|Any CPU.ActiveCfg = Release|Any CPU + {859CF903-14E3-479E-844D-E9EBBFADCA71}.Release|Any CPU.Build.0 = Release|Any CPU + {E122E1CB-44BC-4E4B-AA9E-1DC9CAD50E9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E122E1CB-44BC-4E4B-AA9E-1DC9CAD50E9F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E122E1CB-44BC-4E4B-AA9E-1DC9CAD50E9F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E122E1CB-44BC-4E4B-AA9E-1DC9CAD50E9F}.Release|Any CPU.Build.0 = Release|Any CPU + {BC9C48C1-69EA-43D9-BAB0-C528DDC06484}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BC9C48C1-69EA-43D9-BAB0-C528DDC06484}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BC9C48C1-69EA-43D9-BAB0-C528DDC06484}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BC9C48C1-69EA-43D9-BAB0-C528DDC06484}.Release|Any CPU.Build.0 = Release|Any CPU + {0A0E6444-24E7-4AB1-84F8-F8D539009DE1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0A0E6444-24E7-4AB1-84F8-F8D539009DE1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0A0E6444-24E7-4AB1-84F8-F8D539009DE1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0A0E6444-24E7-4AB1-84F8-F8D539009DE1}.Release|Any CPU.Build.0 = Release|Any CPU + {70BC7F95-242F-4839-8D1D-7BE15FC787D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {70BC7F95-242F-4839-8D1D-7BE15FC787D5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {70BC7F95-242F-4839-8D1D-7BE15FC787D5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {70BC7F95-242F-4839-8D1D-7BE15FC787D5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {234FECDE-169F-4E44-B22F-4EA84C8F2E7D} + EndGlobalSection +EndGlobal