課題6-1
まずは、講義資料の4-5ページの4.1参考にしてprint関数を利用しよう。
C言語の課題(第2回)でもやったように、3行分のメッセージを出力するプログラムを、今度はPythonで作成せよ。
プログラムをTACTに提出せよ。
課題6-2
制御文for を利用しよう。
C言語の課題(第3回)でもやったように、以下のA,B,C,Dを求めるプログラムを、今度はPythonで作成し、実行せよ。結果とプログラムをTACTに提出せよ。
課題6-3
下に表示しているbutterfly.cはバタフライ曲線を生成し、butterfly.datというファイルにx座標とy座標の数値を書き出すCプログラムである。
a. butterfly.cのプログラムを実行してbutterfly.datを作成せよ。butterfly.datのファイルの行数を調べTACTに提出せよ。(C言語のプログラム実行は先週の講義資料や演習、もしくは今週の講義資料の7ページの5.1を参照せよ。行数を調べるのはwcコマンドを用いると良い)
b. butterfly.dat のデータを配列として読み込み、x軸とy軸の2次元プロットを作成せよ。TACTにはプロットを作成したスクリプトと図を提出せよ。(グラフ描画は講義資料の7ページの5.2を参照せよ)
/********* butterfly.c **************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.1415926535897932
#endif
int main(){
FILE * fp;
char filename[] = "butterfly.dat"; int i, n;
double th, x, y;
/* 書き出しファイルを開く*/
if(NULL== (fp=fopen(filename,"w"))){
fprintf(stderr, "Cannot open file: %s\n", filename);
exit(1); }
/* バタフライ曲線 */
n = 100;
for (i=0; i < n; i++){
th = 2.0 * M_PI * i / n;
x = sin(th) * (exp(cos(th)) - 2.0*cos(4.0*th) - pow(sin(th/12.0),5));
y = cos(th) * (exp(cos(th)) - 2.0*cos(4.0*th) - pow(sin(th/12.0),5));
fprintf(fp,"%lf %lf\n",x,y);
}
fclose(fp); /* ファイルは必ず閉じる */
return 0;
}