Presentation is loading. Please wait.

Presentation is loading. Please wait.

解答 1 複素数を構造体として定義し、二つの複素数の積(結果は複素数)を返す 関数 を定義せよ。

Similar presentations


Presentation on theme: "解答 1 複素数を構造体として定義し、二つの複素数の積(結果は複素数)を返す 関数 を定義せよ。"— Presentation transcript:

1 解答 1 複素数を構造体として定義し、二つの複素数の積(結果は複素数)を返す 関数 を定義せよ。
#include <stdio.h> struct complex { double real, imag; }; typedef struct complex Complex; Complex cmult(Complex, Complex); main() { Complex x = {1.0, 1.0}; Complex y = {1.414, 1.414}; Complex z; z = cmult(x,y); printf(“z = %f + %fi\n”, z.real, z.imag); } Complex cmult(Complex a, Complex b) { Complex c; c.real = a.real*b.real - a.imag * b.imag; c.imag = a.real*b.imag + a.imag * b.real; return c;

2 解答 2 3次元での座標(x、y、z)ベクトルを構造体で定義し、二つの点の 間の距離を計算する関数distanceを定義せよ。
この関数を使って、(1、1、1)と(0、1、0)の距離を求める プログラムを作成せよ。 #include <stdio.h> #include <math.h> struct vector { double x, y, z; }; typedef struct vector Vector; double distance(Vector, Vector); main() { Vector x = {1.0, 1.0, 1.0}; Vector y = {0.0, 1.0, 0.0}; double d; d = distance(x,y); printf(“distance = %f”, d); } double distance(Vector a, Vector b) { double d; d = (a.x-b.x)*(a.x -b.x)+(a.y-b.y)*(a.y-b.y) + (a.z - b.z)*(a.z - b.z); d = sqrt(d); return d;

3 解答 1 #include <stdio.h> #include <math.h> main()
{ char bun[100]; int n,j; char c; printf(“文章を入力”);scanf(“%s”, bun); n = strlen(bun); for(j = 0; j < n; j++) { c = bun[j]; if(c >= ‘a’ && c <= ‘z’) { c = c + 3; if(c > ‘z’) c = c - ‘z’ + ‘a’; } if(c >= ‘A’ && c <= ‘Z’) { if(c > ‘Z’) c = c - ‘Z’ + ‘A’; printf(“暗号文は%s\n”, bun);

4 解答 2 正の整数nに対して、nの約数の和(1を含みnを含まない 約数の和)を返す関数を作れ。これをもとに、1以上
10000以下の友愛数を求めるプログラムを作成せよ。 #include <stdio.h> int yakusuu(int n) { int i,m=0; for(i = 1; i < n; i++) if(n % i == 0) m = m + i; return m; }

5 解答 2(続き) 正の整数nに対して、nの約数の和(1を含みnを含まない 約数の和)を返す関数を作れ。これをもとに、1以上
10000以下の友愛数を求めるプログラムを作成せよ。 #include <stdio.h> int yakusu(int); main() { int i,m,n; for(i = 1; i < 10000; i++){ m = yakusu(i); n = yakusu(m); if(n == i) printf(“%d and %d は友愛数¥n”, i, m); }

6 解答 3 #include <stdio.h> double func(double,double[],int); main()
解答 3  #include <stdio.h> double func(double,double[],int); main() { double f[] = {-3,1,3,0,0,1}; double a = 2.0; int n; double s; n = sizeof(f)/sizeof(double); s = func(a, f, n); printf(“関数の値は%f\n”, s); } double func(double a, double f[], int n) { int I; double s=0; for(I = 0; I < n; I++) { s = s*a + f[n-I-1]; return s;

7 解答 4 平面での座標ベクトル(x、y)を構造体として定義し、 2点間の距離を求める関数をプログラムせよ。
また、この関数を用いて与えられた3点を頂点とする 三角形の面積を求めるプログラムを作成せよ。 #include <stdio.h> #include <math.h> struct point { double x,y; }; typedef struct point Point; double distance(Point, Point); Double helon(double, double, double); main() { Point A = {1,0}; Point B = {0, 1};  Point C = {1,1}; double a, b, c, s; a = distance(A,B); b = distance(B,C); c = distance(C,A); d = helon(a,b,c); printf(“面積は%d\n”, d); }

8 解答 4(続き) double distance(Point a, Point b) { double d;
d = (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); return sqrt(d); } double helon(double a, double b, double, c) { double s; s = (a+b+c)/2; S = s*(s-a)*(s-b)*(s-c); return sqrt(s);

9 解答 5 正の実数を入力し平方根を計算するプログラム。ただし、負の実数を入力した場合は、その旨表示するよう気配りすること。
#include <stdio.h> #include <math.h> main() { double d, s; printf(“正の実数を入力せよ”); scanf(“%lf”, &d); if(d > 0) { s = sqrt(d); printf(“%fの平方根は%fです\n”, d, s); } else { printf(“正の実数じゃないとだめ\n”); }

10 解答 6 キーボードから 10 人の成績(100 点満点の整数値)を配列に読み込み、最高点、最低点及び 平均点を表示するプログラム
#include <stdio.h> main() { int d[10]; int j; double av, amax, amin; for(j = 0; j < 10; j++) { printf(“学籍番号 No. %d の成績”); scanf(“%d”, &d[j]); } amax = amin = d[0]; if(amax < d[j]) amax = d[j]; if(amin > d[j]) amin = d[j]; av = av+ d[j]; av = av/10; printf(“最高点は%d点です\n”, amax); printf(“最低点は%d点です\n”, amin); printf(“平均点は%d点です\n”, av); % ./a.out 学籍 No. 0 の成績:80 学籍 No. 1 の成績:70 学籍 No. 2 の成績:95 ... 学籍 No. 9 の成績:40 最高点は 100 点です。 最低点は 10 点です。 平均点は 55.3 点です。 %

11 解答 7 下のような模様を出力するプログラムをつくれ。 %./a.out 文字は何にする?# 最大の個数は?5 ##### #### ###
#include <stdio.h> main() { char c; int n; int I,j; printf(“文字は何にする”); scanf(“%c”, &c); printf(“最大の個数は”); scanf(“%d”, &n); for(I = 0; I < n; I++) { for(j = 0; j < n-I;j++) putchar(‘ ‘); for(j = 0; j < n-I, j++) putchar(c ); }

12 解答 8 ユークリッドの互除法を用いて、入力した 2 つの自然数の最大公約数を求めるプログラムを作れ。
#include <stdio.h> main() { int n,m; int a, b,r; printf(“自然数を二つ入力せよ:”); scanf(“%d %d”, &a, &b); if(a <= 0 || b <= 0) { printf(“入力エラー\n”); exit(1); } n = a; m = b; while(m != 0) { r = n % m; n = m; m = r; printf(“%d と %d の最大公約数は%dです。\n”, a, b, m);

13 解答 9 の解を求める方法にニュートン法がある。 適当な初期値 から始めて、 を順に求めていくと、 は解に近づいていく。
                   の解を求める方法にニュートン法がある。 適当な初期値  から始めて、 を順に求めていくと、     は解に近づいていく。 #include <stdio.h> #include <math.h> main() { double x,y,c; c = 2.0; y=1.0; do { x = y; y = (2*x+c/(x*x))/3.0; } while(fabs(x-y) > ); printf(“sqrt(%lf) = %lf¥n”, c, y); }


Download ppt "解答 1 複素数を構造体として定義し、二つの複素数の積(結果は複素数)を返す 関数 を定義せよ。"

Similar presentations


Ads by Google