C++ SS22: Klausur bis Aufgabe 3
This commit is contained in:
+27
-29
@@ -174,58 +174,56 @@ std::cout << a++ << "\n";
|
||||
\begin{parts}
|
||||
\part
|
||||
\begin{cppcode}
|
||||
std::vector<double> a{1., 3., 2., 4.};
|
||||
a.at(4) = 5;
|
||||
int *a;
|
||||
a = new int[6];
|
||||
a[8] = 13;
|
||||
\end{cppcode}
|
||||
\begin{solution}
|
||||
Vector hat nur 4 Elemente und es würd auf Element 5 zugegriffen. (2 Pt.)
|
||||
Array hat nur 6 Elemente und es würde auf Element 9 zugegriffen. (2 Pt.)
|
||||
\end{solution}
|
||||
|
||||
\part
|
||||
% TODO not an actuall error but only warning
|
||||
\begin{cppcode}
|
||||
class SomeClass {
|
||||
int a;
|
||||
int& getA() const { return a;}
|
||||
int b;
|
||||
public:
|
||||
SomeClass::getB(){return b}
|
||||
};
|
||||
\end{cppcode}
|
||||
\begin{solution}
|
||||
getA kann nicht const sein und int\& zurückgeben (2 Pt.)
|
||||
Klassenname in der Deklaration, Semikolon fehlt, Rückgabetyp fehlt (2 Pt.)
|
||||
\end{solution}
|
||||
|
||||
\part
|
||||
\begin{cppcode}
|
||||
void scale(std::vector<double> vec, double factor) {
|
||||
for(auto& num : vec)
|
||||
num = num * factor;
|
||||
}
|
||||
int *a;
|
||||
for (int i=0;i<10;i++)
|
||||
a[i] = i*i;
|
||||
\end{cppcode}
|
||||
\begin{solution}
|
||||
Der Vektor wird als Kopie übergeben und die gehen nach Ende der Funktion verloren. (2 Pt.)
|
||||
\end{solution}
|
||||
|
||||
\part
|
||||
% TODO funktioniert theoretisch auch..
|
||||
\begin{cppcode}
|
||||
double& max(double a, double b) {
|
||||
if ( a > b)
|
||||
return a;
|
||||
else
|
||||
return b;
|
||||
}
|
||||
\end{cppcode}
|
||||
\begin{solution}
|
||||
Dangeling Ref (2 Pt.)
|
||||
New vergessen
|
||||
\end{solution}
|
||||
|
||||
\part
|
||||
\begin{cppcode}
|
||||
int len;
|
||||
std::cin >> len;
|
||||
std::array<int, len> a;
|
||||
int a = 5;
|
||||
for (int i=0; i<5; i++)
|
||||
a += i;
|
||||
std::cout <<i <<" " <<a <<std::endl;
|
||||
\end{cppcode}
|
||||
\begin{solution}
|
||||
Die Variable len ist erst zur Laufzeit bekannt. Der Array braucht aber len zur Kompilezeit (2 Pt.)
|
||||
i ist in einem anderen Scope (2 Pt.)
|
||||
\end{solution}
|
||||
|
||||
\part
|
||||
\begin{cppcode}
|
||||
int a = 5;
|
||||
for (int i=0; i<5; i++)
|
||||
i -= a;
|
||||
\end{cppcode}
|
||||
\begin{solution}
|
||||
Schleife erreicht nie das Ende (2 Pt.)
|
||||
\end{solution}
|
||||
\end{parts}
|
||||
\end{aufgabe}
|
||||
|
||||
Reference in New Issue
Block a user