commit 792fc2f410eae00b6b11abc98ecc808bc2457ca6 Author: Simon Date: Mon Apr 22 17:24:13 2019 +0200 Inital commit diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..51a8bba --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..fc4447c --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + algueb1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + \ No newline at end of file diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..0c68a61 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..df0b2dd --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,11 @@ +{ + "configurations": [ + { + "type": "java", + "name": "CodeLens (Launch) - App", + "request": "launch", + "mainClass": "app.App", + "projectName": "algueb1" + } + ] +} \ No newline at end of file diff --git a/Uebungsblatt1.pdf b/Uebungsblatt1.pdf new file mode 100644 index 0000000..0b09c23 Binary files /dev/null and b/Uebungsblatt1.pdf differ diff --git a/bin/app/App.class b/bin/app/App.class new file mode 100644 index 0000000..1a78ced Binary files /dev/null and b/bin/app/App.class differ diff --git a/src/app/App.java b/src/app/App.java new file mode 100644 index 0000000..d2466eb --- /dev/null +++ b/src/app/App.java @@ -0,0 +1,169 @@ +package app; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class App { + public static void main(String[] args) throws Exception { + System.out.println(sumTo(30)); + System.out.println(sumToRecursive(30)); + System.out.println(fibonacciRecursive(new ArrayList(), 200)); + System.out.println(fibonacci(new ArrayList(), 200)); + System.out.println(euklRecursive(544, 391)); + System.out.println(eukl(544, 391)); + + int[] a = { 1, 2, 3, 4 }; + System.out.println(sumArray(a)); + int[] b = { 3, 2, 4, 1 }; + System.out.println(max(b)); + int[] c = { 1, 2, 3, 4 }; + System.out.println(Arrays.toString(reverse(c))); + double[][] d = { { 1, 2 }, { 10, 20 }, { -5, 5 } }; + System.out.println(Arrays.toString(average(d))); + double[][] e = { { 1, 2 }, { 3, 4 }, { 5, 6 } }; + double f = 2; + System.out.println(Arrays.deepToString(multiply(e, f))); + double[][] g = { { 1, 2 }, { 3, 4 } }; + double[][] h = { { 5, 6 }, { 7, 8 } }; + System.out.println(Arrays.deepToString(multiply(g, h))); + double[] i = { -2, 1, -3, 4, -1, 2, 1, -5, 4 }; + System.out.println(maxSum(i)); + } + + /** Aufgabe 1 */ + public static int sumTo(int target) { + int sum = 0; + for (int i = 1; i <= target; i++) { + sum += i; + } + return sum; + } + + public static int sumToRecursive(int target) { + if (target == 0) + return target; + return target + sumToRecursive(target - 1); + } + + /** Aufgabe 2.1 */ + public static List fibonacciRecursive(List fib, int max) { + // Initialize List + if (fib.size() == 0) { + fib.add(0); + fib.add(1); + fib.add(1); + } + fib.add(fib.get(fib.size() - 1) + fib.get(fib.size() - 2)); + if (fib.get(fib.size() - 1) >= max) + return fib; + return fibonacciRecursive(fib, max); + } + + /** Aufgabe 2.2 */ + public static List fibonacci(List fib, int max) { + // Initialize List + if (fib.size() == 0) { + fib.add(0); + fib.add(1); + fib.add(1); + } + /** Bessere Effizienz da Abbruchbedinngung einduetig erkennbar */ + while (fib.get(fib.size() - 1) <= max) { + fib.add(fib.get(fib.size() - 1) + fib.get(fib.size() - 2)); + } + return fib; + } + + /** Aufgabe 3 */ + public static int euklRecursive(int a, int b) { + int rest = a % b; + if (rest == 0) + return b; + return euklRecursive(b, rest); + } + + public static int eukl(int a, int b) { + int rest = a % b; + while (rest != 0) { + a = b; + b = rest; + rest = a % b; + } + return b; + } + + /** Aufgabe 4 */ + public static int sumArray(int[] a) { + int sum = 0; + for (int i = 0; i < a.length; i++) { + sum += a[i]; + } + return sum; + } + + /** Aufgabe 5 */ + public static int max(int[] a) { + int max = 0; + for (int i = 0; i < a.length; i++) { + if (a[i] > max) + max = a[i]; + } + return max; + } + + /** Aufgabe 6 */ + public static int[] reverse(int[] a) { + int[] b = new int[a.length]; + for (int i = a.length - 1; i >= 0; i--) { + b[a.length - 1 - i] = a[i]; + } + return b; + } + + /** Aufgabe 7 */ + public static double[] average(double[][] a) { + double[] b = new double[a.length]; + for (int i = 0; i < a.length; i++) { + b[i] = (a[i][0] + a[i][1]) / 2; + } + return b; + } + + /** Aufgabe 8 */ + public static double[][] multiply(double[][] a, double b) { + for (int i = 0; i < a.length; i++) + for (int ii = 0; ii < a[i].length; ii++) + a[i][ii] = a[i][ii] * b; + return a; + } + + /** Aufgabe 9 */ + public static double[][] multiply(double[][] a, double[][] b) { + double[][] c = new double[a.length][a.length]; + for (int i = 0; i < a.length; i++) + for (int ii = 0; ii < a[i].length; ii++) + // a[i][ii] = a[i][ii] * b[ii][i]; + for (int iii = 0; iii < a[i].length; iii++) + c[i][ii] = a[i][iii] * b[iii][ii] + c[i][ii]; + return c; + } + + /** Aufgabe 10 */ + public static int maxSum(double[] a) { + int chunkSize = 4; + int maxSum = 0; + int currSum = 0; + + for (int i = 0; i < a.length; i++) { + if (i < a.length - chunkSize) + for (int ii = 0; ii < chunkSize; ii++) { + currSum += a[i + ii]; + } + if (maxSum < currSum) + maxSum = currSum; + currSum = 0; + } + return maxSum; + } +} \ No newline at end of file