13 sqrt 14 sqrt sqrtpractice1 c 1 2

  • Slides: 60
Download presentation

13 sqrt 関数 •

13 sqrt 関数 •

14 平方根の計算 • sqrt() 関数は引数が負の場合計算出来ない。 sqrt_practice_1. c 1 2 3 4 5 6 7

14 平方根の計算 • sqrt() 関数は引数が負の場合計算出来ない。 sqrt_practice_1. c 1 2 3 4 5 6 7 8 9 10 11 #include <stdio. h> #include <math. h> void main() { double x; fprintf(stderr, "x = ? b"); scanf("%lf", &x); printf("x: %fn", x); printf("sqrt(x): %fn", sqrt(x)); } mintty+bash+gcc $ gcc sqrt_practice_1. c &&. /a x = 2 x: 2. 000000 sqrt(x): 1. 414214 $. /a x = -2 x: -2. 000000 sqrt(x): nan

15 演習: 平方根の計算(if文) • mintty+bash+gcc $ gcc sqrt_practice_if. c &&. /a x = -2

15 演習: 平方根の計算(if文) • mintty+bash+gcc $ gcc sqrt_practice_if. c &&. /a x = -2 x: -2. 000000 sqrt(x): 1. 414214 i

16 演習: 平方根の計算(if文) ヒント • sqrt_practice_1. c 10 printf("sqrt(x): %fn", sqrt(x)); sqrt_practice_if. c 10

16 演習: 平方根の計算(if文) ヒント • sqrt_practice_1. c 10 printf("sqrt(x): %fn", sqrt(x)); sqrt_practice_if. c 10 11 12 13 14 真 偽 条件式

[1] pp. 63 -66, 256 -257. 17 条件演算子 三項演算子(? : ) • 書式: 真

[1] pp. 63 -66, 256 -257. 17 条件演算子 三項演算子(? : ) • 書式: 真 式 1 条件式 偽 式 2 • 条件式 ? 式 1 : 式 2 1 2 3 4 5 6 7 8 9 condexprtest. c mintty+bash+gcc #include <stdio. h> $ gcc condexprtest. c &&. /a i = 1 not zero $. /a i = 0 zero void main() { int i; fprintf(stderr, "i = ? b"); scanf("%d", &i); printf("%sn", i ? "not zero" : "zero"); }

30 演習: 4 で割り切れるか表示する • leap_year_practice_1. c の /*WYCH*/ を書き 換えて、year が 4 で割り切れる場合

30 演習: 4 で割り切れるか表示する • leap_year_practice_1. c の /*WYCH*/ を書き 換えて、year が 4 で割り切れる場合 "can be divided by 4. "、割り切れない場合 "can not be divided by 4. " と表示するプログラムを完成せ 2015 -05 -11修正 よ。 誤:dividec 正: divided mintty+bash+gcc $ gcc leap_year_practice_1. c &&. /a year = 2015 can not be divided by 4. $. /a year = 2016 can be divided by 4.

39 演習: 月の名前の表示 • monthname_practice_1. c の /*WYCH*/ の部 分を変更し、入力した月 month に対応する 英語の月名(January, February,

39 演習: 月の名前の表示 • monthname_practice_1. c の /*WYCH*/ の部 分を変更し、入力した月 month に対応する 英語の月名(January, February, March, April, June, July, August, September, October, November, December )を表示せよ。 mintty+bash+gcc $ gcc monthname_practice_1. c &&. /a month = 1 January

47 教科書 pp. 119 -122. 後判定ループ (do while 文) • continue, break 後の処理(iの値)に注目 looptest_dowhile.

47 教科書 pp. 119 -122. 後判定ループ (do while 文) • continue, break 後の処理(iの値)に注目 looptest_dowhile. c mintty + bash int i = 0, j = 0, n; $. /looptest_dowhile n = 10 0, 1: 1 st 2 nd 3 rd 1, 2: 1 st continue 1, 3: 1 st 2 nd 3 rd 2, 4: 1 st 2 nd break fprintf(stderr, "n = "); scanf("%d", &n); do { j++; printf("%d, %d: 1 st", i, j); if (j == 2) {printf(" continuen"); continue; } printf(" 2 nd"); if (j == 4) {printf(" breakn"); break; } printf(" 3 rdn"); i++; } while (i < n); mintty + bash $. /looptest_dowhile n = 0 0, 1: 1 st 2 nd 3 rd do while 文は、 ループ内の処理を 最低1回は実行する。

48 教科書 p. 123. 前判定ループ (while 文) • continue, break 後の処理(iの値)に注目 looptest_while. c mintty

48 教科書 p. 123. 前判定ループ (while 文) • continue, break 後の処理(iの値)に注目 looptest_while. c mintty + bash int i = 0, j = 0, n; $. /looptest_while n = 10 0, 1: 1 st 2 nd 3 rd 1, 2: 1 st continue 1, 3: 1 st 2 nd 3 rd 2, 4: 1 st 2 nd break fprintf(stderr, "n = "); scanf("%d", &n); while (i < n) { j++; printf("%d, %d: 1 st", i, j); if (j == 2) {printf(" continuen"); continue; } printf(" 2 nd"); if (j == 4) {printf(" breakn"); break; } printf(" 3 rdn"); i++; } mintty + bash $. /looptest_while n = 0

49 教科書 pp. 124 -129. 初期化・更新処理付きループ (for 文) • continue, break 後の処理(iの値)に注目 looptest_for. c

49 教科書 pp. 124 -129. 初期化・更新処理付きループ (for 文) • continue, break 後の処理(iの値)に注目 looptest_for. c mintty + bash int i = 0, j = 0, n; $. /looptest_for n = 10 0, 1: 1 st 2 nd 3 rd 1, 2: 1 st continue 2, 3: 1 st 2 nd 3 rd 3, 4: 1 st 2 nd break fprintf(stderr, "n = "); scanf("%d", &n); for (i = 0; i < n; i++) j++; printf("%d, %d: 1 st", if (j == 2) {printf(" 2 nd"); if (j == 4) {printf(" 3 rdn"); } { i, j); continuen"); continue; } breakn"); break; } mintty + bash $. /looptest_for n = 0

51 [1] p. 281. goto文 • 指定した名札付き文へ移動(ジャンプ)する • 名札(label)は以下のように設定出来る ラベル名: 文 goto 文は 余程理由がない限り使わないこと

51 [1] p. 281. goto文 • 指定した名札付き文へ移動(ジャンプ)する • 名札(label)は以下のように設定出来る ラベル名: 文 goto 文は 余程理由がない限り使わないこと do while 文相当 for 文相当 loop: ; { // something to do contin: ; } if (expr) goto loop; brk: ; loop: ; if (expr) { // something to do contin: ; goto loop; } brk: ; expr 1; loop: ; if (expr 2) { // something to do contin: ; expr 3; goto loop; } brk: ;

58 演習: 素数判定 • 標準入力から入力された正整数が素数かど うか判定するプログラムを作成せよ。 • isprime_practice_1. c の /*WYCH*/ の箇所を修正すれば良い。 mintty +

58 演習: 素数判定 • 標準入力から入力された正整数が素数かど うか判定するプログラムを作成せよ。 • isprime_practice_1. c の /*WYCH*/ の箇所を修正すれば良い。 mintty + bash $ $ i 1 $ i 2 $ i 3 $ i 4 gcc is_prime_practice_1. c. /a = 1 is not prime number. /a = 2 is prime number. /a = 3 is prime number. /a = 4 is not prime number