Computer Programming I (Advanced) 7 th week Kazumasa Yamamoto Dept. Comp. Sci. & Eng. Computer Programming I (Adv.) 7th week 1
Exercise of last week 1. Sorting by bubble sort Compare the bubble sort with qsort() in the standard library by using clock(). How slow is the bubble sort? 2. Character string search by Boyer-Moore algorithm Compare the BM algorithm with a brute-force method with strncmp() by using clock(). How fast is the BM algorithm? Computer Programming I (Adv.) 7th week 2
前回の課題 1. バブルソートによるソーティング 標準ライブラリ内の qsort() と比べて どれくらい遅いか clock() を使って見てみる 2. Boyer-Moore 法による文字列検索 strncmp() で力任せに検索するのと比べて どれくらい速くなるか clock() を使って見てみる Computer Programming I (Adv.) 7th week 3
Today s contents Control of auxiliary programs pipe gnuplot How to use Exercise: Game of Life (with gnuplot) Computer Programming I (Adv.) 7th week 4
今日の内容 外部プログラムを制御する パイプ gnuplot 簡単な使い方 課題 : ライフゲーム ( 表示は gnuplot で ) Computer Programming I (Adv.) 7th week 5
Control of auxiliary programs Controlling an auxiliary program from a C language program Run an auxiliary program Then, pass data to the running program Pass the data by using pipe pipe in shell command line $ ls less Do the same thing by yourself! Computer Programming I (Adv.) 7th week 6
外部プログラムの制御 C 言語のプログラムから 既存の外部プログラムを制御する 外部プログラムを動かす 動いている外部プログラムにデータを渡す パイプを使ってデータを渡すことを考える shellのコマンドラインで使うパイプ $ ls less 自前でやってみよう Computer Programming I (Adv.) 7th week 7
Program control by pipe Open / close of a pipe popen() / pclose() popen() Open a pipe 1 st arg. : command line character string 2 nd arg. : file mode (the same as in fopen() ) Return value : a pointer to the pipe (FILE structure) pclose() Close the pipe Argument : the pointer to the pipe (FILE structure) Computer Programming I (Adv.) 7th week 8
パイプによるプログラム制御 パイプを開く 閉じる popen() / pclose() popen() パイプを開く 第 1 引数 : コマンドライン文字列 第 2 引数 : 開くモード (fopen() と同じ ) 返り値 : ファイル構造体へのポインタ pclose() パイプを閉じる 引数 : ファイル構造体へのポインタ Computer Programming I (Adv.) 7th week 9
Simple example #include <stdio.h> int main() { FILE *pipe; char str[256]; // <stdio.h> has definitions of pipe related. // a FILE structure variable. The same as for a file. pipe = popen( ls -1, r ); // read the result of ls -1 while (fgets(str, 256, pipe)!= NULL) { // Input / Output of pipe is printf( %s, str); // the same as a file, because } // it s via the FILE structure } pclose(pipe); // Close the pipe Computer Programming I (Adv.) 7th week 10
単純な例 #include <stdio.h> int main() { FILE *pipe; char str[256]; // パイプ関係のものは <stdio.h> で宣言されている // パイプ用のファイル構造体 普通のファイルと同じ pipe = popen( ls -1, r ); // ls -1 の結果を読み込む while (fgets(str, 256, pipe)!= NULL) { // パイプに対する入出力は printf( %s, str); // ファイル構造体経由なので } // 普通のファイルと同じ } pclose(pipe); // パイプを閉じる Computer Programming I (Adv.) 7th week 11
How to send data The former example code reads output of an auxiliary program. How to send data? Run an auxiliary program which can read data from the standard input. with an option which lets the program be able to do it, etc. Write data by fprintf(), etc. in your code. Computer Programming I (Adv.) 7th week 12
データを送るには 先ほどの例は 外部プログラムの出力を読み込んでいた データを送るには? 外部プログラムを標準入力からデータを読み込めるようにして起動 それ用のオプションを付ける等 fprintf() 等でデータを書き込む Computer Programming I (Adv.) 7th week 13
gnuplot A software to draw charts An isolated appricaton, not library By giving coordinate (x, y (, z)) data sets as text data, gnuplot draws a chart. Computer Programming I (Adv.) 7th week 14
gnuplot グラフを描くための独立したソフトウェア ライブラリではない テキストデータとして x 座標 y 座標 ( z 座標 ) の組み合わせのデータを与えることで グラフを描く Computer Programming I (Adv.) 7th week 15
Try running gnuplot (1) $ xterm (on xterm) $ gnuplot (snip) gnuplot> set terminal xterm gnuplot> plot sample1.txt gnuplot> plot sample1.txt with line gnuplot> plot sample1.txt with points gnuplot> plot sample1.txt with points pointtype 2 gnuplot> plot sample1.txt with points pointtype 3 gnuplot> plot sample1.txt with points pointtype 4 Computer Programming I (Adv.) 7th week 16
gnuplot を動かしてみる (1) $ xterm (xterm 上で ) $ gnuplot ( 省略 ) gnuplot> set terminal xterm gnuplot> plot sample1.txt gnuplot> plot sample1.txt with line gnuplot> plot sample1.txt with points gnuplot> plot sample1.txt with points pointtype 2 gnuplot> plot sample1.txt with points pointtype 3 gnuplot> plot sample1.txt with points pointtype 4 Computer Programming I (Adv.) 7th week 17
Try running gnuplot (2) $ gnuplot (snip) gnuplot> plot - input data( e ends) > 10 5 input data( e ends) > 20 10 input data( e ends) > 30 15 input data( e ends) > 40 20 input data( e ends) > 50 25 input data( e ends) > 60 30 input data( e ends) > e gnuplot> Computer Programming I (Adv.) 7th week 18
gnuplot を動かしてみる (2) $ gnuplot ( 省略 ) gnuplot> plot - input data( e ends) > 10 5 input data( e ends) > 20 10 input data( e ends) > 30 15 input data( e ends) > 40 20 input data( e ends) > 50 25 input data( e ends) > 60 30 input data( e ends) > e gnuplot> Computer Programming I (Adv.) 7th week 19
Requirements for exercise of this week Starting option -persist Not erase the graph window if the program finished. Without this option, the graph window will disappear when closing the pipe. This is maybe default behavior... Drawing charts set xrange [0:100], set yrange [0:100] Specifying chart ranges (for x- and y-coordinates) Do once at beginning - given as a plot command option instead of a file name Reads data from the standard input e should be sent when finishing the data transmission. By combining with pipe, you can send data from your code. Do input by your code instead of the keyboard input in the former example. Computer Programming I (Adv.) 7th week 20
今日の課題で必要なこと 起動時オプション -persist プログラムが終了してもグラフウィンドウを消さないようにする これを付けていないと パイプを閉じた段階でグラフウィンドウが消える デフォルトの動作になっているかも? グラフ描画時 set xrange [0:100] set yrange [0:100] グラフの範囲の指定 最初に 1 回だけやれば良い plot コマンドでファイル名のところを - とする 標準入力からデータを読むようになる データの最後を示すためには e のみを送る パイプと組み合わせることで プログラムからデータを流せる 先ほどの例でキーボードで入力した部分をプログラムにやらせる Computer Programming I (Adv.) 7th week 21
Overview of this week s exercise Game of Life Use gnuplot for displaysing graphically Your code must show the alternation of generations by sending data to gnuplot via pipe. Up and down, left and right of the cell area should be connected circulatedly. Computer Programming I (Adv.) 7th week 22
今日の課題概要 ライフゲーム グラフィカルに表示するために gnuplot を利用 pipe で gnuplot にデータを送って 世代交代の様子を見れるようにする セルのエリアは上下 左右が繋がって循環しているものとして作成すること Computer Programming I (Adv.) 7th week 23
Game of Life Here, we will treat the standard 23/3 Game of Life. Rule (from Wikipedia) The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. The initial pattern constitutes the seed of the system. The first generation is created by applying the below rules simultaneously to every cell in the seed births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations. Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: Birth : Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. Live : Any live cell with two or three live neighbors lives on to the next generation. Under-population : Birth : Any live cell with fewer than two live neighbors dies, as if caused by under-population. Over-population : Any live cell with more than three live neighbors dies, as if by overpopulation. Computer Programming I (Adv.) 7th week 24
ライフゲーム ここでは標準的な 23/3 ライフゲームを考える ルール (Wikipedia より ) 碁盤の格子状のものを考える その格子 1 つ 1 つをセルと呼ぶ セルには 生 死 の状態がある セルの生死は まず初期状態として与えられ その後は 以下のルールに沿って 1 世代ごとに変化していく あるセルに注目すると そのセルには 上下左右斜め方向に 8 個のセルがあり これらを 隣接するセル とする 誕生 : 死んでいるセルに隣接する生きたセルがちょうど 3 つあれば 次の世代でそのセルは生となる 生存 : 生きているセルに隣接する生きたセルが 2 つか 3 つならば そのセルは次の世代でも生きる 過疎 : 生きているセルに隣接する生きたセルが 1 つ以下ならば そのセルは過疎により次の世代で死ぬ 過密 : 生きているセルに隣接する生きたセルが 4 つ以上ならば そのセルは過密により次の世代で死ぬ Computer Programming I (Adv.) 7th week 25
Data format sample1.dat, sample2.dat, sample3.dat binary formatted files All data are int type. First and second data : Size of cell area maximum of x- and y-coordinates The origin is (0, 0). Third data and then : Initial live cell coordinates x-coordinate, y-coordinate Repeated the number of initial live cells. Read them by fread() sample1.txt has the same content of sample1.dat in text mode. Use this to check your code. Computer Programming I (Adv.) 7th week 26
データ形式 sample1.dat,sample2.dat,sample3.dat バイナリファイル 全て int 型 1 つ目と 2 つ目のデータ : セルのエリアの大きさ x 座標の最大値と y 座標の最大値 原点は (0,0) それ以降 : 初期配置の生きたセルの座標 x 座標値,y 座標値 初期配置の生きたセルの数だけ繰り返し fread() を使って読む sample1.txt の内容を上記に沿ってバイナリ化したものが sample1.dat 動作チェックに用いよ Computer Programming I (Adv.) 7th week 27
How to get a graph window of gnuplot The exercise program should be run on xterm as the same as the former example of gnuplot. Computer Programming I (Adv.) 7th week 28
gnuplot のグラフウィンドウが 出ないことへの対処 先ほどの gnuplot の例と同様 課題プログラムも xterm 上で実行しましょう Computer Programming I (Adv.) 7th week 29
Examination Date : June 16 (Thu) 2 nd time slot (10:30 12:00) Room:B1-201 We have the same examination for BASIC class and ADVANCED class simultaneously in one room. Written (paper) examination (not in Moodle) The examination topics is suited for BASIC class. The topics is limited from contents in ADVANCED class. Many basic topics will be questions of the exam. We don t have this class on June 3. From June 10, you will have the class of Computer Programming II a in this time slot. Computer Programming I (Adv.) 7th week 30
テスト 日時 :6 月 16 日 ( 木 ) 2 限目 10:30 12:00 場所 :B1-201 ベーシッククラスとアドバンスクラスのテストを 同じ部屋で同時に行います 筆記試験です (Moodle では実施しません ) 出題範囲はベーシックの方の内容に合わせます アドバンスでやった内容より狭く 基本的な内容が多くなります 6 月 3 日 ( 金 ) のこの講義はお休みです 6 月 10 日 ( 金 ) からはソフトウェア演習 Ⅱa です Computer Programming I (Adv.) 7th week 31