/* 例3. 総和の計算の関数 */ #include #define N 100 /* 関数プロトタイプ宣言 */ double souwa(double a[], int n); main() { int i, n; double x[N], s; /* データの代入 */ x[0] = 1.0; x[1] = 4.0; x[2] = -3.0; x[3] = 9.0; x[4] = 8.0; n = 5; /* データの表示 */ /* これも関数として別に記述すると便利であろう */ i = 0; while (i < n) { printf("x[%d] = %g\n", i, x[i]); i = i + 1; } /* 総和の計算の本体 */ s = souwa(x, n); /* 結果の表示 */ printf("総和は %g です。\n", s); } double souwa(double a[], int n) { int i; double s; s = 0; i = 0; while (i < n) { s = s + a[i]; i = i + 1; } return s; }