/**************************************/
/* 2次元空間上の距離を計算するマクロ */
/* coded by Y.Suganuma */
/**************************************/
#include <stdio.h>
#include <math.h>
#define range(v1,v2,w1,w2) (sqrt((v1-w1)*(v1-w1)+(v2-w2)*(v2-w2)))
int main()
{
double x[2], y[2];
printf("2次元空間の点の座標を入力して下さい ");
scanf("%lf %lf", &x[0], &x[1]);
printf("もう1つの点の座標を入力して下さい ");
scanf("%lf %lf", &y[0], &y[1]);
printf(" 2つの点の距離は %f\n", range(x[0],x[1],y[0],y[1]));
return 0;
}
/****************************/
/* 正規乱数の発生 */
/* coded by Y.Suganuma */
/****************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define DOS yes
double norm(double, double);
#if defined DOS
double drand48(void)
{
return rand() / (double)RAND_MAX;
}
#endif
int main()
{
double m,s;
int i1;
#if defined DOS
srand(123);
#else
srand48(12345);
#endif
printf("正規分布\n");
printf(" 平均値は? ");
scanf("%lf", &m);
printf(" 標準偏差は? ");
scanf("%lf", &s);
for (i1 = 0; i1 < 10; i1++)
printf(" %f\n", norm(m, s));
return 0;
}
/******************************/
/* 正規分布 */
/* m : 平均値 */
/* s : 標準偏差 */
/* return : 正規分布変量 */
/******************************/
double norm(double m, double s)
{
double x;
int i1;
x = 0.0;
for (i1 = 0; i1 < 12; i1++)
x += drand48();
x = s * (x - 6.0) + m;
return x;
}
| 菅沼ホーム | 演習解答例目次 | 本文目次 | 付録 | 索引 |