My student number: s186099 sin(1.5) = 0.997497 cos(1.5) = 0.070753 tan(1.5) = 14.098336 -------------------------------- |
なお,結果の出力では必ず最初の行に自分の学生番号を,最後にハイフンによるラインをつけること.無い場合には減点するので注意.
階乗の再帰プログラムを有効に使ってください.
解答用紙を使用する際には,学生番号と名前の記入も忘れないでください.さらに,解答用紙自体がC言語のプログラムとなっていますので, cc コマンドを実行して,コンパイルエラーの無いことを確認してから提出してください. 指定の解答用紙を使用していない,コンパイルエラーが出る,実行時に警告が出る,学生番号と名前が無い,というような答案は提出されても採点しません.注意してください. |
// ************************************************** // // プログラミング入門II レポート課題 // 2019.5.20 // // ************************************************** // // 学生番号: // // 氏名: // // ************************************************** // // #include <stdio.h> #include <stdlib.h> #include <time.h> double si(double x); double co(double x); int fact(int n); int main(void) { srand((unsigned) time(NULL)); double x; x = (double) (rand() % 3) / 2 + 0.5; printf("My student number: s186099\n\n"); printf("sin(%.1f) = %f\n", x, si(x)); printf("cos(%.1f) = %f\n", x, co(x)); printf("tan(%.1f) = %f\n", x, si(x) / co(x)); printf("\n--------------------------------\n\n"); return 0; } double si(double x) { double s0 = x, sx = x; int i; for(i=3; i<=9; i+=2) { sx = sx * x * x * -1; s0 += (sx / fact(i)); } return s0; } double co(double x) { double c0 = 1, cx = 1; int i; for(i=2; i<=8; i+=2) { cx = cx * x * x * -1; c0 += (cx / fact(i)); } return c0; } int fact(int n) { if(n==1) { return 1; } else { return n * fact(n-1); } } |
別解
// ************************************************** // // プログラミング入門II レポート課題 // 2019.5.20 // // ************************************************** // // 学生番号: // // 氏名: // // ************************************************** // // #include <stdio.h> #include <stdlib.h> #include <time.h> double trigonomical(int n, double t, double x); int fact(int n); int main(void) { srand((unsigned) time(NULL)); double x; x = (double) (rand() % 3) / 2 + 0.5; printf("My student number: s186099\n\n"); printf("sin(%.1f) = %f\n", x, trigonomical(3, x, x)); printf("cos(%.1f) = %f\n", x, trigonomical(2, 1, x)); printf("tan(%.1f) = %f\n", x, trigonomical(3, x, x) / trigonomical(2, 1, x)); printf("\n--------------------------------\n\n"); return 0; } double trigonomical(int n, double t, double x) { double tx = t; int i; for(i=n; i<=9; i+=2) { tx = tx * x * x * -1; t += (tx / fact(i)); } return t; } int fact(int n) { if(n==1) { return 1; } else { return n * fact(n-1); } } |