1 / 6

問題 1

問題 1. 正の実数を受け取り、小数部分のみを取り出す関数を定義して、以下の動作をするプログラムを作れ。. ヒント: int 型の変数に double 型の値を代入すると小数点以下が 切り捨てられることを用いよ。. % ./a.out 正の実数を入力: 5.1234 小数部分は 0.1234 です。 %. main 文はすでに完成している。関数 my_function を完成せよ。. double my_function(double); main() { double input, output;

Download Presentation

問題 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 問題 1 正の実数を受け取り、小数部分のみを取り出す関数を定義して、以下の動作をするプログラムを作れ。 ヒント:int 型の変数に double 型の値を代入すると小数点以下が 切り捨てられることを用いよ。 % ./a.out 正の実数を入力: 5.1234 小数部分は 0.1234 です。 % main 文はすでに完成している。関数 my_function を完成せよ。 double my_function(double); main() { double input, output; printf(“正の実数を入力:); scanf(“%lf”, &input); output = my_function(input); printf(“小数部分は %f です\n”, output); }

  2. 問題 2 成績(100点満点の整数値)を受け取り、秀、優、良、可、不可、を出力する 関数を定義して、以下の動作をするプログラムを作れ。 main 文の骨格はすでに完成している。 関数 hantei を定義せよ。 100~90 : 秀 89 ~ 80 : 優 79 ~ 70 : 良 69 ~ 60 : 可 59 ~ 0 : 不可 void hantei(int); main() { int score; printf(“成績を入力:”); scanf(“%d”, &score); hantei(score); } % ./a.out 成績を入力:90 貴方の成績は秀です。 % 関数 hantei は成績を引数として受け取り、上記の判定にしたがって、秀・優・良・可・不可を表示する。 (返却値無し)

  3. 問題 3 正の整数を受け取り、それが素数であれば 1 (int) を、素数でなければ 0 (int) を返却値として返す関数 prime を完成させよ。正の整数でなければ−1を返す様にする。 % ./a.out 正の整数を入力:13 13 は素数です % main 文の骨格部分はすでにでき上がっている。 int prime(int); main() { int i,r scanf(“%d”, &i); r = prime(i); if( r == 1 ) printf(“%d は素数です\n”, i); else if(r == 0) printf(“%d は素数ではない\n”, i); else printf(“%d は正の整数でありません。\n”, i); } 関数 prime は整数値の引数を受け取る。返却値は int 0 もしくは 1,-1 である。

  4. 問題 4 正の整数nを受け取り、1以上n未満のnの約数の和を求める関数 int yakusuu(int n) を定義せよ。これを使って、友愛数を求めるプログラムを作成せよ。 2 つの自然数について、 片方の約数(自分自身は 除く)の和が、他方の約数 (同じく自分自身は除く)の 和に等しくなるとき、これら 2 つの自然数は友愛数の 関係にあるという。 % ./a.out 220 の友愛数は 284 です。 1184 の友愛数は1210 です。 2620 の友愛数は2924 です。 5020 の友愛数は 5564 です。 ... % int yakusuu(int); main() { int i, j; for(i = 1; i < 100000; i++) { j = yakusuu(i); if (j > i && i == yakusuu(j)) printf(“%dの友愛数は%dです\n”, i、j); } }

  5. 問題 5 gnuplot 等の視覚化ツールを用いて次の関数をグラフに描け。 z = f(x, y) = cos(x2 + y2)/(1+x2+y2) –4 ≤ x ≤ 4, –4 ≤ y ≤ 4 2 次元平面上の点 (x, y) の高さがz = f(x, y) で与えられる 3 次元空間内の曲面。 2 つの引き数をもつ関数を自分で定義して次の形式で出力する。 % ./a.out -4.000000 -4.000000 0.025279 -4.000000 -3.900000 0.030390 -4.000000 -3.800000 0.017824 .... % ./a.out > data % gnuplot ... gnuplot > splot "data" gnuplot > quit % x, y, zの形式で出力(スペースで区切って出力) リダイレクションで計算結果をファイルへ書き込む。 gnuplot からデータファイルを読み込み視覚化する。

  6. gnuplotによるグラフ描画 gnuplotはグラフを描画するソフトで、二次元、三次元の グラフを描く事が出来る。 % gnuplot gnuplot > plot "data” gnuplot >splot “ data2” gnuplot>set isosamples 100 gnuplot>splot [-4:4][-4:4]cos(x*x+y*y)/(1+x*x+y*y) gnuplot > quit   % 二次元グラフの描画 三次元グラフの描画 標本点の数を100に設定

More Related