1 / 21

応用プログラミング 第 2 回

応用プログラミング 第 2 回. 今日の内容. 前回の課題 の解説 三値の最大値 関数の働き 課題 連絡 事項. 前回の課題1. 初期化処理 加算を繰り返す for 文の使い方 if 文の使い方. 前回の課題1. int k; double s = 0; for (k = 1; k <= 10000; k++) { if (k % 2 == 0) s -= 1.0/(2*k-1); else s+= 1.0/(2*k-1); }. 前回の課題2. 配列の使い方 入力関数 scanf の使い方 変数の中身の入れ替え. 9. 4.

Download Presentation

応用プログラミング 第 2 回

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. 応用プログラミング第2回

  2. 今日の内容 • 前回の課題の解説 • 三値の最大値 • 関数の働き • 課題 • 連絡事項

  3. 前回の課題1 • 初期化処理 • 加算を繰り返す • for文の使い方 • if文の使い方

  4. 前回の課題1 int k; double s = 0; for (k = 1; k <= 10000; k++) { if (k % 2 == 0) s -= 1.0/(2*k-1); else s+= 1.0/(2*k-1); }

  5. 前回の課題2 • 配列の使い方 • 入力関数scanfの使い方 • 変数の中身の入れ替え 9 4 6 7 1 [0] [1] [2] [3] [4] 隣の箱(配列)の大小を比較して, 大きな値が右側に来るように入れ替える

  6. ・5つの中の最大値が入る ・この箱はもう調べなくてよい(確定) 前回の課題2 9 4 6 7 1 [0] [1] [2] [3] [4] 4 6 7 1 9 [0] [1] [2] [3] [4] 4つの中の最大値が入る 4 6 1 7 [0] [1] [2] [3]

  7. 前回の課題2 for (j = 1; j < 20; j++) { for (i = 0; i < 20 - j; i++) { if (data[i] > data[i+1]) { tmp = data[i]; data[i] = data[i+1]; data[i+1] = tmp; } } } 隣どうしの値の大小を比較 隣どうしの値を入れ替え tmp 4 4 9 data[i] data[i+1]

  8. 三値の最大値(P.14~) • 3つの値の最大値を求めるプログラムを考える. 4 9 6 a b c 9 max 最大値

  9. 三値の最大値(P.14~) • どうやって,最大値を求めればよいか?  ⇒どんなルールに従えばうまくいくのか? • maxにaの値を代入 • bの値がmaxより大きいならば,maxにbを代入 • cの値がmaxより大きいならば,maxにcを代入 4 9 6 a b c max

  10. 三値の最大値(P.14~) • maxにaの値を代入 • bの値がmaxより大きいならば,maxにbを代入 • cの値がmaxより大きいならば,maxにcを代入 4 4 9 9 6 a b c max

  11. フローチャート START • ルール(プログラムの流れ=フロー)を視覚的に表したい(p.21) a → max TRUE b > max b → max 上から下へ処理が進む FALSE TRUE c > max c→ max FALSE END

  12. 関数の働き #include <stdio.h> int main() { int a,b,c; int max; printf(“a = “); scanf(“%d”,&a); printf(“b = “); scanf(“%d”,&b); printf(“c = “); scanf(“%d”,&c); max = a; if (b > max) max = b; if (c > max) max = c; printf(“max = %d\n”,max); return 0; } int max3(int x, int y, int z) { int max; max = a; if (b > max) max = b; if (c > max) max = c; return max; } max = max3(a,b,c)

  13. max3に最大値を計算してほしい! 材料(int型)が,3つ必要です.持ってきてください. mainの家 6 c 9 4 b a max3の家

  14. 最大値計算終了! 結果をmainの家まで届けて 材料は,決められた容器に入れておいてください. max3の家 x 9 6 max 9 y 4 z

  15. max

  16. 関数の働き #include <stdio.h> int main() { int a,b,c; int max; printf(“a = “); scanf(“%d”,&a); printf(“b = “); scanf(“%d”,&b); printf(“c = “); scanf(“%d”,&c); max = a; if (b > max) max = b; if (c > max) max = c; printf(“max = %d\n”,max); return 0; } int max3(int x, int y, int z) { int max; max = a; if (b > max) max = b; if (c > max) max = c; return max; } max = max3(a,b,c)

  17. 関数の働き a → x b → y c → z max3関数 int max3(int x, int y, int z) { int max; max = x; if (y > max) max = y; if (z > max) max = z; return max; } main関数 main() { max = max3(a,b,c); } max(main側) ← max(max3側)

  18. 関数の働き • 実引数(actual argument):関数呼び出し側がサブルーチンに渡す値 • 仮引数(formal argument):実引数の値をサブルーチン側で受け取るための変数 • 戻り値(return value):サブルーチンが呼び出し側に返す値 max3関数 main関数 int max3(int x, int y, int z) { int max; max = x; if (y > max) max = y; if (z > max) max = z; return max; } main() { max = max3(a,b,c); }

  19. アルゴリズムとは • 問題を解くためのものであって,明確に定義され,順序づけられた有限個の規則からなる集合(JIS X0001)

  20. クイズ(4月22日深夜まで) 1.三値の最小値を求める以下の関数およびフローチャートを作れ int min3(int x, int y, int z) 2.三値の中央値を求める以下の関数およびフローチャートを作れ int med3(int x, int y, int z)

  21. 注意事項 • メールでレポートを送信するときは、レポートをメールに添付して送ること • メールの件名は、 「学籍番号+半角スペース+氏名」にしてください。 (例)08F1099 松木裕二 • メールアドレス変更のお知らせ: 2009apz@gmail.com • 講義資料 www.fit.ac.jp/~matsuki/AP.html

More Related