
Ich brauche Ihre Hilfe beim Schreiben eines Algorithmus mit TeXstudio und For-Schleifen. Für die Methode der Gaußschen Elimination habe ich den Code:
for (int i = 0; i < N-1; i++) {
for (int j = i; j < N; j++) {
double ratio = A[j][i]/A[i][i];
for (int k = i; k < N; k++) {
A[j][k] -= (ratio*A[i][k]);
b[j] -= (ratio*b[i]);
}
}
}
Antwort1
Eine Möglichkeit, Ihren Code zu schreiben, wäre die Verwendung des listings
Pakets. Ihr Gaußscher Eliminationsalgorithmus würde dann wie folgt geschrieben werden:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{ %
backgroundcolor = \color{white}, % Background color
keywordstyle = \color{blue}, % Keyword style (just color here)
numbers = left, % Add line number to the left of the code
captionpos = b % Caption at the bottom of the listing
}
\begin{document}
\begin{lstlisting}[language = C, caption={Gaussian elimination algorithm}]
for (int i = 0; i < N-1; i++) {
for (int j = i; j < N; j++) {
double ratio = A[j][i]/A[i][i];
for (int k = i; k < N; k++) {
A[j][k] -= (ratio*A[i][k]);
b[j] -= (ratio*b[i]);
}
}
}
\end{lstlisting}
\end{document}
Wenn Sie keine Überschrift für Ihren Code wünschen, entfernen Sie einfach die caption
Option am Anfang der lstlisting
Umgebung. Wenn die Sprache nicht C war, können Sie die language
Option jederzeit in die von Ihnen verwendete Sprache ändern.
Antwort2
Der entsprechende Pseudocode; hier werden die Pakete algorithm
und algpseudocode
verwendet.
\documentclass{article}
\usepackage{algorithm} % Required for pseudo code
\usepackage[noend]{algpseudocode} % Required for pseudo code
\renewcommand{\algorithmicrequire}{\textbf{Input:}} % Changes 'Require' to 'Input'
\begin{document}
\begin{algorithm}
\caption{Caption of pseudo code}
\begin{algorithmic}
\Require $A$
\For{$i \gets 0$ to $N - 2 $}
\For{$j \gets i$ to $N - 1 $}
\State $ratio \gets A[j][i]/A[i][i]$
\For{$k \gets i$ to $N - 1 $}
\State $A[j][k] \gets A[j][k] - (ratio*A[i][k])$
\State $b[j] \gets b[j] - (ratio*b[i])$
\EndFor
\EndFor
\EndFor
\end{algorithmic}
\label{alg:code_label}
\end{algorithm}
\end{document}