generated from kenryuS/report-temp
finished cls10
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 2.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 3.1 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 9.5 KiB |
Binary file not shown.
Binary file not shown.
+110
-4
@@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
\usepackage{tex/preamble}
|
\usepackage{tex/preamble}
|
||||||
\usepackage{tex/simple-title}
|
\usepackage{tex/simple-title}
|
||||||
|
\usepackage{subcaption}
|
||||||
|
\usepackage{here}
|
||||||
|
|
||||||
\reportauthor{柴田健琉}
|
\reportauthor{柴田健琉}
|
||||||
\reporttitle{情報処理2 - 前期第10回課題}
|
\reporttitle{情報処理2 - 前期第10回課題}
|
||||||
\reportdate{2026年}{06月}{23日}
|
\reportdate{2026年}{06月}{23日}
|
||||||
\turnindate{年}{月}{日}
|
\turnindate{2026年}{06月}{29日}
|
||||||
|
|
||||||
\begin{document}
|
\begin{document}
|
||||||
\simpletitle
|
\simpletitle
|
||||||
@@ -16,8 +18,112 @@
|
|||||||
この課題のプログラムは以下の環境での動作が確認されている:
|
この課題のプログラムは以下の環境での動作が確認されている:
|
||||||
|
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
\item{OS: }
|
\item{OS: Arch Linux, Linux 6.18.36-1-lts x86\_64}
|
||||||
\item{}
|
\item{CC: GCC 15.2.0}
|
||||||
\item{}
|
\item{CFLAGS: \texttt{-g -O1 -Wall -Wpedantic}}
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
|
|
||||||
|
\section{課題 1}
|
||||||
|
|
||||||
|
教科書の演習10-4にて整数型配列の内容を配列の添字と同じにする関数\texttt{set\_idx}関数を作成する.
|
||||||
|
|
||||||
|
\lstinputlisting[language=C,title={課題1のプログラム}]{./src/cls10/a1.c}
|
||||||
|
|
||||||
|
\subsection{実行結果}
|
||||||
|
|
||||||
|
\begin{figure}[H]
|
||||||
|
\centering
|
||||||
|
\begin{minipage}[h]{0.45\textwidth}
|
||||||
|
\centering
|
||||||
|
\includegraphics[width=6cm]{./assets/cls10-a1-1.png}
|
||||||
|
\subcaption{\texttt{A\_LEN} = 5}
|
||||||
|
\end{minipage}
|
||||||
|
\begin{minipage}[h]{0.45\textwidth}
|
||||||
|
\centering
|
||||||
|
\includegraphics[width=6cm]{./assets/cls10-a1-2.png}
|
||||||
|
\subcaption{\texttt{A\_LEN} = 10}
|
||||||
|
\end{minipage}
|
||||||
|
\caption{課題1の実行結果}
|
||||||
|
\end{figure}
|
||||||
|
|
||||||
|
\subsection{処理の流れ}
|
||||||
|
|
||||||
|
\subsubsection{\texttt{main}関数}
|
||||||
|
|
||||||
|
\begin{enumerate}
|
||||||
|
\item{要素数10の配列\texttt{a}を初期化}
|
||||||
|
\item{\texttt{set\_idx}関数を引数に配列\texttt{a}と要素数を設定して呼び出す}
|
||||||
|
\item{配列\texttt{a}の各要素を標準出力に表示する}
|
||||||
|
\end{enumerate}
|
||||||
|
|
||||||
|
\subsubsection{\texttt{set\_idx}関数}
|
||||||
|
|
||||||
|
\begin{enumerate}
|
||||||
|
\item{ループカウンタ\texttt{i}を0に初期化}
|
||||||
|
\item{ポインタ\texttt{v}から\texttt{i} * 4バイトを加算したメモリアドレスにループカウンタ\texttt{i}を書き込む}
|
||||||
|
\item{ループカウンタ\texttt{i}をインクリメントする}
|
||||||
|
\item{ループカウンタ\texttt{i}が\texttt{sz}になるまで2を実行}
|
||||||
|
\end{enumerate}
|
||||||
|
|
||||||
|
\subsubsection{\texttt{main}関数}
|
||||||
|
|
||||||
|
\begin{enumerate}
|
||||||
|
\item{配列\texttt{a}を\texttt{A\_LEN}の長さで宣言}
|
||||||
|
\item{\texttt{set\_idx}関数を引数に\texttt{a}と\texttt{A\_LEN}を持たせて呼び出す}
|
||||||
|
\item{配列の内容を標準出力に表示する}
|
||||||
|
\end{enumerate}
|
||||||
|
|
||||||
|
\subsection{書く際の難点}
|
||||||
|
|
||||||
|
とくに問題なく素直に書けた.
|
||||||
|
|
||||||
|
\subsection{想定されるバグ}
|
||||||
|
|
||||||
|
ヌルポインタの検出がないので引数\texttt{v}にヌルポインタを渡すとヌルポインタ参照でセグメンテーションフォルトを引き起し, プログラムがクラッシュする.
|
||||||
|
|
||||||
|
\section{課題 2}
|
||||||
|
|
||||||
|
配列を昇順に並び換えるプログラムList 8-5の\texttt{bsort}関数をポインタで書き換える.
|
||||||
|
|
||||||
|
\lstinputlisting[language=C,title={課題2のプログラム}]{./src/cls10/a2.c}
|
||||||
|
|
||||||
|
\subsection{実行結果}
|
||||||
|
|
||||||
|
\begin{figure}[H]
|
||||||
|
\centering
|
||||||
|
\includegraphics[width=11cm]{./assets/cls10-a2.png}
|
||||||
|
\caption{課題2の実行結果}
|
||||||
|
\end{figure}
|
||||||
|
|
||||||
|
\subsection{処理の流れ}
|
||||||
|
|
||||||
|
\subsubsection{\texttt{bsort}関数}
|
||||||
|
|
||||||
|
\begin{enumerate}
|
||||||
|
\item{値の一時保持用の変数\texttt{i}を0で宣言・初期化する}
|
||||||
|
\item{ループカウンタ\texttt{i}の\texttt{for}ループ内で\texttt{n} - 1回繰り返す:}
|
||||||
|
\begin{enumerate}
|
||||||
|
\item{ループカウンタ\texttt{j}の\texttt{for}ループ内で\texttt{n} - 1から\texttt{i}まで繰り返す:}
|
||||||
|
\begin{enumerate}
|
||||||
|
\item{もし, \texttt{(v + j - 1)}が指す値が\texttt{(v + j)}が指す値より大きければ, 交換する}
|
||||||
|
\end{enumerate}
|
||||||
|
\end{enumerate}
|
||||||
|
\end{enumerate}
|
||||||
|
|
||||||
|
\subsubsection{\texttt{main}関数}
|
||||||
|
|
||||||
|
\begin{enumerate}
|
||||||
|
\item{長さ\texttt{ARR\_LEN}の配列\texttt{a}を0で宣言・初期化する}
|
||||||
|
\item{標準入力から配列\texttt{a}に各要素の値を取り込む}
|
||||||
|
\item{\texttt{bsort}関数を引数に\texttt{a}と\texttt{ARR\_LEN}を持たせて呼び出す}
|
||||||
|
\item{ソートの結果を標準出力に表示する}
|
||||||
|
\end{enumerate}
|
||||||
|
|
||||||
|
\subsection{書く際の難点}
|
||||||
|
|
||||||
|
既存の関数を配列表記からポインタ表記にするだけなのであっさりと書けた.
|
||||||
|
|
||||||
|
\subsection{想定されるバグ}
|
||||||
|
|
||||||
|
ヌルポインタの検出がないので引数\texttt{v}にヌルポインタを渡すとヌルポインタ参照でセグメンテーションフォルトを引き起し, プログラムがクラッシュする.
|
||||||
\end{document}
|
\end{document}
|
||||||
|
|||||||
+5
-3
@@ -1,5 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define A_LEN 5
|
||||||
|
|
||||||
void set_idx(int *v, int sz) {
|
void set_idx(int *v, int sz) {
|
||||||
for (int i = 0; i < sz; i++) {
|
for (int i = 0; i < sz; i++) {
|
||||||
*(v + i) = i;
|
*(v + i) = i;
|
||||||
@@ -7,9 +9,9 @@ void set_idx(int *v, int sz) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int a[10];
|
int a[A_LEN];
|
||||||
set_idx(a, 10);
|
set_idx(a, A_LEN);
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < A_LEN; i++) {
|
||||||
printf("a[%d] = %d\n", i, a[i]);
|
printf("a[%d] = %d\n", i, a[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
#define ARR_LEN 10
|
#define ARR_LEN 10
|
||||||
|
|
||||||
void bsort(int *v, int n) {
|
void bsort(int *v, int n) {
|
||||||
register int t = 0;
|
int t = 0;
|
||||||
for (int i = 0; i < n - 1; i++) {
|
for (int i = 0; i < n - 1; i++) {
|
||||||
for (int j = n - 1; j > i; j--) {
|
for (int j = n - 1; j > i; j--) {
|
||||||
if (*(v + j - 1) > *(v + j)) {
|
if (*(v + j - 1) > *(v + j)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user