2 Commits

  1. 16
      gsd-questions.tex
  2. 105
      todten/GSDSolution.java

16
gsd-questions.tex

@ -9,6 +9,7 @@
\usepackage{colortbl}
\usepackage{anysize}
\usepackage{enumerate}
\usepackage{verbatim}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
@ -50,11 +51,6 @@ $$
2.82 \\ 5.65 \\ 7.81
\end{array}\right)
$$
\ifdefined\loesung
\begin{verbatim}
Musterloesung here
\end{verbatim}
\fi
\subsection{Merging and Sorting}
Let $A$ and $B$ be two arrays of $n$ (unsorted) integer entries each. Write
@ -69,11 +65,6 @@ $$
\end{array}
$$
\ifdefined\loesung
\begin{verbatim}
Musterloesung here
\end{verbatim}
\fi
\subsection{Recursion}
@ -81,9 +72,8 @@ Consider a herd of cows. Write a \emph{recursive} function to calculate the
sum of all legs. Use addition only (no multiplication allowed).
\ifdefined\loesung
\begin{verbatim}
Musterloesung here
\end{verbatim}
\subsection{Loesung}
\verbatiminput{todten/GSDSolution.java}
\fi
\section{Algorithms and Data Structures}

105
todten/GSDSolution.java

@ -1,21 +1,104 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gsdsolution;
/**
*
* @author todten
*/
import java.math.*;
import java.util.*;
public class GSDSolution {
/**
* @param args the command line arguments
*/
/* Main Program */
public static void main(String[] args) {
System.out.println("Hello GSD \n");
int someCows = 10;
System.out.println(someCows + " cows have " + countLegs(someCows) + " legs \n");
System.out.println("Calculate Row Vector");
calcAbsRowVector(inputMatrix);
System.out.println("Merge and Sort Arrays");
mergeAndSortArrays(input1, input2);
}
/* Third Exercise */
public static int countLegs(int cows) {
if (cows == 0) {
return 0;
} else {
return 4 + countLegs(cows - 1);
}
}
/* First Exercise */
static int[][] inputMatrix = new int[][]{
{2, 2},
{4, 4},
{6, 5}};
public static double[] calcAbsRowVector(int[][] aMatrix) {
double[] result = new double[aMatrix.length];
double temp = 0.0;
for (int i = 0; i < aMatrix.length; i++) {
for (int j = 0; j < aMatrix[i].length; j++) {
temp = temp + Math.pow(aMatrix[i][j], 2);
//System.out.println(" I = " + i + " J = " + j + " Temp = " + temp);
}
result[i] = Math.sqrt(temp);
temp = 0.0;
//System.out.println(" Result at I " + i + " Result " + result[i]);
}
return result;
}
/* Second Exercise */
static int[] input1 = new int[]{2, 7, 5, 34};
static int[] input2 = new int[]{3, 48, 4, 72};
public static int[] mergeAndSortArrays(int[] firstArray, int[] secondArray) {
int[] resultArray = new int[firstArray.length * 2];
/* OK, both Arrays are the same size */
int offset = firstArray.length;
for (int i = 0; i < firstArray.length; i++) {
resultArray[i] = firstArray[i];
}
for (int i = 0; i < secondArray.length; i++) {
resultArray[i + offset] = secondArray[i];
}
/* Just for debugging */
System.out.println("Result Array " + Arrays.toString(resultArray));
/*
* Die haeufigste Antwort wird die Verwendung der build - in Funktion
* von Java zum Sortieren von Arrays sein
*/
Arrays.sort(resultArray);
/*
* Dann nachfragen und den Bewerber das Array manuell mit einem
* Algorithmus seiner Wahl (z.B. Quicksort oder einfache Maximumssuche)
* sortieren lassen
*/
/* Just for debugging */
System.out.println("Sorted Result Array " + Arrays.toString(resultArray));
return resultArray;
}
}
Loading…
Cancel
Save