GSD Questions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /**
  2. *
  3. * @author todten
  4. */
  5. import java.math.*;
  6. import java.util.*;
  7. public class GSDSolution {
  8. /* Main Program */
  9. public static void main(String[] args) {
  10. int someCows = 10;
  11. System.out.println(someCows + " cows have " + countLegs(someCows) + " legs \n");
  12. System.out.println("Calculate Row Vector");
  13. calcAbsRowVector(inputMatrix);
  14. System.out.println("Merge and Sort Arrays");
  15. mergeAndSortArrays(input1, input2);
  16. }
  17. /* Third Exercise */
  18. public static int countLegs(int cows) {
  19. if (cows == 0) {
  20. return 0;
  21. } else {
  22. return 4 + countLegs(cows - 1);
  23. }
  24. }
  25. /* First Exercise */
  26. static int[][] inputMatrix = new int[][]{
  27. {2, 2},
  28. {4, 4},
  29. {6, 5}};
  30. public static double[] calcAbsRowVector(int[][] aMatrix) {
  31. double[] result = new double[aMatrix.length];
  32. double temp = 0.0;
  33. for (int i = 0; i < aMatrix.length; i++) {
  34. for (int j = 0; j < aMatrix[i].length; j++) {
  35. temp = temp + Math.pow(aMatrix[i][j], 2);
  36. //System.out.println(" I = " + i + " J = " + j + " Temp = " + temp);
  37. }
  38. result[i] = Math.sqrt(temp);
  39. temp = 0.0;
  40. //System.out.println(" Result at I " + i + " Result " + result[i]);
  41. }
  42. return result;
  43. }
  44. /* Second Exercise */
  45. static int[] input1 = new int[]{2, 7, 5, 34};
  46. static int[] input2 = new int[]{3, 48, 4, 72};
  47. public static int[] mergeAndSortArrays(int[] firstArray, int[] secondArray) {
  48. int[] resultArray = new int[firstArray.length * 2];
  49. /* OK, both Arrays are the same size */
  50. int offset = firstArray.length;
  51. for (int i = 0; i < firstArray.length; i++) {
  52. resultArray[i] = firstArray[i];
  53. }
  54. for (int i = 0; i < secondArray.length; i++) {
  55. resultArray[i + offset] = secondArray[i];
  56. }
  57. /* Just for debugging */
  58. System.out.println("Result Array " + Arrays.toString(resultArray));
  59. /*
  60. * Die haeufigste Antwort wird die Verwendung der build - in Funktion
  61. * von Java zum Sortieren von Arrays sein
  62. */
  63. Arrays.sort(resultArray);
  64. /*
  65. * Dann nachfragen und den Bewerber das Array manuell mit einem
  66. * Algorithmus seiner Wahl (z.B. Quicksort oder einfache Maximumssuche)
  67. * sortieren lassen
  68. */
  69. /* Just for debugging */
  70. System.out.println("Sorted Result Array " + Arrays.toString(resultArray));
  71. return resultArray;
  72. }
  73. }