ページ 1 / 1
関数についてしつもんです。
Posted: 2007年6月10日(日) 01:09
by yossiy
C言語を始めて関数でつまづいています。
平均点、標準偏差、偏差値を求めるプログラムです。
関数を使わないものは出来たのですが、関数を使ったものに直すことができません。
引数の書き方もよくわかりません。
どなたか教えてください。
関数を使わないもの
#include <stdio.h>
#include <math.h>
#define NUM 10
int main(void)
{
int soten[NUM]={84,95,60,100,83,72,93,85,65,98};
int goukei;
double heikin;
double hensati[NUM];
double hyoujunhensa;
int i,n;
/*****計算をする部分*****/
/*****平均点を求める*****/
goukei = 0;
for (i=0; i<NUM; i++) {
goukei += soten;
}
heikin = (double)goukei / NUM;
/*****標準偏差を求める*****/
goukei = 0;
for (i=0; i<NUM; i++) {
goukei = goukei + (soten - heikin) * (soten - heikin);
}
hyoujunhensa = sqrt(goukei/NUM);
/*****偏差値を求めて配列に入れる*****/
for (i=0; i <NUM; i++){
hensati[n] = (soten - heikin) / hyoujunhensa *10 + 50;
}
/*****表示をする部分*****/
/*****平均点と標準偏差の表示*****/
printf("平均点 %5.2f\n",heikin);
printf("標準偏差 %6.3f\n",hyoujunhensa);
/*****個人の点数、偏差値の表示*****/
for(i=0; i<NUM; i++){
for(n=0; n<NUM; n++);
printf("点数 %d 偏差値 %6.3f\n", soten,hensati[n]);
}
return 0;
}
関数を使ったもの
#include <stdio.h>
#include <math.h>
#define NUM 10
double cal_heikin(int soten[NUM]);
double cal_hyoujunhensa(int soten[NUM],double heikin);
double cal_hensati(int soten,double heikin,double hyoujunhensa);
int main(void)
{
int soten[NUM]={84,95,60,100,83,72,93,85,65,98};
double heikin;
double hensati[NUM];
double hyoujunhensa;
int i,n;
heikin = cal_heikin(soten,NUM);
printf("平均:%5.3f\n",heikin);"
hyoujunhensa = cal_hyoujunhensa(soten,NUM,heikin);
printf("標準偏差:%5.3f\n",hyoujunhensa);
for(i=0; i<NUM; i++){
for(n=0; n<NUM; n++);
printf("点数 %d 偏差値 %5.3f\n", soten,hensati[n]);
}
return 0;
}
/*平均点を求める*/
double cal_heikin(int soten,NUM){
int goukei,i;
double heikin;
goukei = 0;
for (i=0; i<NUM; i++) {
goukei += soten;
}
heikin = (double)goukei / NUM;
return heikin;
}
/*標準偏差を求める*/
double cal_hyoujunhensa(int soten,NUM,double heikin){
int goukei,i;
double heikin,hyoujunhensa;
goukei = 0;
for (i=0; i<NUM; i++) {
goukei = goukei + (soten - heikin) * (soten - heikin);
}
hyoujunhensa = sqrt(goukei/NUM);
return hyoujunhensa;
}
/*偏差値(一人分)を求めて配列に入れる*/
double cal_hensati(int soten,double heikin,double hyoujunhensa){
int i,n;
double hensati[NUM];
for (i=0; i <NUM; i++){
hensati[n] = (soten - heikin) / hyoujunhensa *10 + 50;
}
return hensati;
}
Re:関数についてしつもんです。
Posted: 2007年6月10日(日) 08:29
by Hermit
>関数を使わないものは出来たのですが、関数を使ったものに直すことができません。
関数を使わない物も変なので、まずはそちらを直しましょう。
電卓片手に、数値を検証しなおしたら、ちょっと違うことが解ると思いますが。
あと、int i,n; のうち、n は余計です。それを使っている所が変になっています。
関数呼び出しですが、
double cal_heikin(int soten[NUM]);
double cal_heikin(int soten,NUM){
double cal_hyoujunhensa(int soten[NUM],double heikin);
double cal_hyoujunhensa(int soten,NUM,double heikin){
宣言と、関数の受け取りが違っています。
まだたくさん間違いはありますが、とりあえずそれだけ書いておきます。
Re:関数についてしつもんです。
Posted: 2007年6月10日(日) 15:19
by BAZZ
とりあえずソースが見にくくてしょうがないので
ソースを
で囲うようにしてみてくださいな^^
そうすると字下げが生かされますんで。
これ重要ですよーー^^
多分orz
Re:関数についてしつもんです。
Posted: 2007年6月10日(日) 15:23
by BAZZ
あれ、
なんか消えてしまった。"
"で囲ってみてくださいといいたかったのです。
Re:関数についてしつもんです。
Posted: 2007年6月10日(日) 15:27
by BAZZ
すいません。なんか馬鹿やってしまってorz
いいたいことをペイントで描きました。
どうぞみてやってください・・・
関数についてしつもんです。2
Posted: 2007年6月10日(日) 18:12
by yossiy
アドバイスありがとうございます。
直せるところは直してみました。
要素数NUMの引数の書き方がわかりません。
まだまだエラーがありますので、引き続きご指導願います。
#include <stdio.h>
#include <math.h>
#define NUM 10 /*要素数*/
double cal_heikin(int soten[NUM]);/*引数:素点の配列(int配列)、要素数(int型)、返り値:平均点(double)*/
double cal_hyoujunhensa(int soten[NUM],double heikin);/*引数:素点の配列(int配列)、要素数(int)、平均点(double)、返り値:標準偏差(double)*/
double cal_hensati(int soten[NUM],double heikin,double hyoujunhensa);/*引数:素点(int)、平均点(double)、標準偏差(double)、返り値:偏差値(double)*/
int main(void)
{
int soten[NUM]={84,95,60,100,83,72,93,85,65,98};
double heikin;
double hensati[NUM];
double hyoujunhensa;
int i,n;
heikin = cal_heikin(soten);
printf("平均:%5.3f\n",heikin);"
hyoujunhensa = cal_hyoujunhensa(soten,heikin);
printf("標準偏差:%5.3f\n",hyoujunhensa);
hensati[n]=cal_hensati(soten,heikin,hyoujunhensa)
for(i=0; i<NUM; i++){
for(n=0; n<NUM; n++);
printf("点数 %d 偏差値 %5.3f\n", soten,hensati[n]);
}
return 0;
}
/*平均点を求める*/
double cal_heikin(int soten[NUM]){
int goukei,i;
double heikin;
goukei = 0;
for (i=0; i<NUM; i++) {
goukei += soten;
}
heikin = (double)goukei / NUM;
return heikin;
}
/*標準偏差を求める*/
double cal_hyoujunhensa(int soten,double heikin){
int goukei,i;
double heikin,hyoujunhensa;
goukei = 0;
for (i=0; i<NUM; i++) {
goukei = goukei + (soten - heikin) * (soten - heikin);
}
hyoujunhensa = sqrt(goukei/NUM);
return hyoujunhensa;
}
/*偏差値(一人分)を求めて配列に入れる*/
double cal_hensati(int soten[NUM],double heikin,double hyoujunhensa){
int i,n;
double hensati[NUM];
for (i=0; i <NUM; i++){
hensati[n] = (soten - heikin) / hyoujunhensa *10 + 50;
}
return hensati;
}
Re:関数についてしつもんです。2
Posted: 2007年6月10日(日) 18:36
by 組木紙織
コメントをいれてコンパイルが通るように一部変更しました。
宣言の部分の int soten[NUM]は int soten[/url]で十分ですので。
内容は一切見ていません。
#include <stdio.h>
#include <math.h>
#define NUM 10 /*要素数*/
double cal_heikin(int soten[NUM]);/*引数:素点の配列(int配列)、要素数(int型)、返り値:平均点(double)*/
double cal_hyoujunhensa(int soten[NUM],double heikin);/*引数:素点の配列(int配列)、要素数(int)、平均点(double)、返り値:標準偏差(double)*/
double cal_hensati(int soten[NUM],double heikin,double hyoujunhensa);/*引数:素点(int)、平均点(double)、標準偏差(double)、返り値:偏差値(double)*/
int main(void)
{
int soten[NUM]={84,95,60,100,83,72,93,85,65,98};
double heikin;
double hensati[NUM];
double hyoujunhensa;
int i,n;
heikin = cal_heikin(soten);
/*最後の"は必要なし*/
printf("平均:%5.3f\n",heikin);/*"*/
hyoujunhensa = cal_hyoujunhensa(soten,heikin);
printf("標準偏差:%5.3f\n",hyoujunhensa);
/*最後に;を入れてください*/
hensati[n]=cal_hensati(soten,heikin,hyoujunhensa);
for(i=0; i<NUM; i++){
for(n=0; n<NUM; n++);
printf("点数 %d 偏差値 %5.3f\n", soten,hensati[n]);
}
return 0;
}
/*平均点を求める*/
double cal_heikin(int soten[NUM]){
int goukei,i;
double heikin;
goukei = 0;
for (i=0; i<NUM; i++) {
goukei += soten;
}
heikin = (double)goukei / NUM;
return heikin;
}
/*標準偏差を求める*/
/*1番目の引数は整数型ですか?宣言時は配列でしたよ*/
double cal_hyoujunhensa(int soten[NUM],double heikin){
int goukei,i;
/*変数heikinの再宣言?*/
double /*heikin,*/hyoujunhensa;
goukei = 0;
for (i=0; i<NUM; i++) {
goukei = goukei + (soten - heikin) * (soten - heikin);
}
hyoujunhensa = sqrt(goukei/NUM);
return hyoujunhensa;
}
/*偏差値(一人分)を求めて配列に入れる*/
double cal_hensati(int soten[NUM],double heikin,double hyoujunhensa){
int i,n;
double hensati[NUM];
for (i=0; i <NUM; i++){
hensati[n] = (soten - heikin) / hyoujunhensa *10 + 50;
}
/*配列を返しますか?宣言ではdoubleを返しています
今回は何も考えずに最初の要素を返しています。
*/
return hensati[0];
}
Re:関数についてしつもんです。2
Posted: 2007年6月10日(日) 18:56
by Hermit
#include <stdio.h>
#include <math.h>
#define NUM 10 /*要素数*/
double cal_heikin(int soten[NUM]);/*引数:素点の配列(int配列)、要素数(int型)、返り値:平均点(double)*/
/*平均点を求める*/
double cal_heikin(int soten[NUM]){
int goukei,i;
double heikin;
goukei = 0;
for (i=0; i<NUM; i++) {
goukei += soten;
}
heikin = (double)goukei / NUM;
return heikin;
}
double cal_hyoujunhensa(int soten[NUM],double heikin);/*引数:素点の配列(int配列)、要素数(int)、平均点(double)、返り値:標準偏差(double)*/
/*標準偏差を求める*/
//double cal_hyoujunhensa(int soten,double heikin){
double cal_hyoujunhensa(int soten[/url],double heikin){
// int goukei,i;
// double heikin,hyoujunhensa; //引数で渡された heikin が有るから heikin は必要なし
int i;
//合計が、int だと、小数点以下は切り捨てられる。double に!
double goukei, hyoujunhensa;
// goukei = 0;
goukei = 0.0;
for (i=0; i<NUM; i++) {
goukei = goukei + (soten - heikin) * (soten - heikin);
}
hyoujunhensa = sqrt(goukei/NUM);
return hyoujunhensa;
}
//double cal_hensati(int soten[NUM],double heikin,double hyoujunhensa);/*引数:素点(int)、平均点(double)、標準偏差(double)、返り値:偏差値(double)*/
/*偏差値(一人分)を求めて配列に入れる*/
//double cal_hensati(int soten[NUM],double heikin,double hyoujunhensa){
void cal_hensati(int soten[NUM],double hensati[/url],double heikin,double hyoujunhensa){
int i,n;
// double hensati[NUM]; ここで宣言しても、関数を抜けると捨てられます、引数などで渡してもらう方がいいかな?やり方は色々ある。
for (i=0; i <NUM; i++){
// n は初期化されてない上、どこでも変化していない。なぜ使用する?
// hensati[n] = (soten - heikin) / hyoujunhensa *10 + 50;
hensati = (soten - heikin) / hyoujunhensa *10 + 50;
}
// return hensati; これを返してもどうしようもない。
}
int main(void)
{
int soten[NUM]={84,95,60,100,83,72,93,85,65,98};
double heikin;
double hensati[NUM];
double hyoujunhensa;
int i,n;
heikin = cal_heikin(soten);
printf("平均:%5.3f\n",heikin);//" <- なに?この '"' は。
hyoujunhensa = cal_hyoujunhensa(soten,heikin);
printf("標準偏差:%5.3f\n",hyoujunhensa);
// hensati[n]=cal_hensati(soten,heikin,hyoujunhensa) この様な事しても、配列に数値は入らない
// cal_hensati(soten,hensati,heikin,hyoujunhensa)
cal_hensati(soten,hensati,heikin,hyoujunhensa);
for(i=0; i<NUM; i++){
// for(n=0; n<NUM; n++); 何のためにある?
// printf("点数 %d 偏差値 %5.3f\n", soten,hensati[n]);
printf("点数 %d 偏差値 %5.3f\n", soten,hensati);
}
return 0;
}
Re:関数についてしつもんです。2
Posted: 2007年6月10日(日) 21:16
by Hermit
普通、標準偏差は、母体を推計して出すと思うので、こっちで計算する方がいいとおもいます。
元の計算方法は、StDevP の方、
#include <stdio.h>
#include <math.h>
#define COUNT(x) (sizeof(x)/sizeof(x[0]))
struct Ave_Stdev
{ //名前は、excel にあわせる。
double Ave;
double StDev;
double StDevP;
};
struct Ave_Stdev
st_asp (int *point, int count)
{
struct Ave_Stdev data;
double Ave = 0.0;
double StDev2 = 0.0;
int i;
for (i = 0; i < count; i++)
{
Ave += point;
StDev2 += point * point;
}
data.Ave = Ave / count;
{ //同じなのでとりあえず tmp にまとめる
double tmp = StDev2 - data.Ave * Ave;
data.StDev = sqrt (tmp / (count - 1));
data.StDevP = sqrt (tmp / count);
} //tmp のための{}付け終わり
return data;
}
int
main (void)
{
int soten[/url] = { 84, 95, 60, 100, 83, 72, 93, 85, 65, 98 };
struct Ave_Stdev data = st_asp (soten, COUNT(soten));
int i;
printf ("平均:%5.3f\n", data.Ave);
printf ("標準偏差:%5.3f\n", data.StDev);
for (i = 0; i < COUNT(soten); i++)
printf ("点数 %3d 偏差値 %5.3f\n", soten,
(soten - data.Ave) / data.StDev * 10 + 50);
return 0;
}
Re:関数についてしつもんです。2
Posted: 2007年6月12日(火) 00:17
by yossiy
みなさん、ありがとうございます。
大変参考になりました。
基本的な事が分からず苦労しています。
またよろしくお願いします。