ページ 1 / 1
テストの平均値を求める
Posted: 2010年6月20日(日) 23:24
by Apple
#include <stdio.h>
#define maxStudents 10
int calcAve(student input[/url], double *englishAverage,
double *mathAverage, double *totalAverage)
{??????
int main(void)
{
students english,math;
double mathAve, engAve, totalAve;
int i=0;
while (i<maxStudents) {
if (scanf("%d %d", &students.english, &students.math) != 2)
break;
i++;
}
students.english = students.math = -1;
calcAve(students, ????????);
printf ("English average is %6.2f\n", engAve);
printf ("Math average is %6.2f\n", mathAve);
printf ("Total average is %6.2f\n", totalAve);
return 0;
}
タイトルの通りで入力された値の各平均値を求めます。
↑のはサンプルで出された骨組みに自分で手を加えたものです。
calcAveの中身をいじるのですが、配列の最後を{-1,-1}のダミーにしなさい、というヒントが
いまいち理解できません。
入力例
45 69
68 80
57 75
35 50
60 71
80 74
78 80
32 46
48 56
100 100
出力例
English average is 60.30
Math average is 70.10
Total average is 65.20
Re:テストの平均値を求める
Posted: 2010年6月20日(日) 23:47
by パコネコ
>int calcAve(student input[/url], double *englishAverage,
>double *mathAverage, double *totalAverage)
>{??????
これってサブルーチンですよね?
(1つ目)student input[/url],(2つ目)double *englishAverage,(3つ目)double *mathAverage,(4つ目) double *totalAverage
でメインから4つもらってくるということでしょうか?
でも
>calcAve(students, ????????);
2つっぽいですよね?
>students english,math;
このstudentsってどういう意味なんですか?
読んでみると
>&students.english, &students.math
となっているのでstudentsの配列を.englishバージョンと.mathバージョンがあるということになるんですね
気になってしまったのでどなたか教えていただいてもいいですか?
Appleさん質問答えず質問書いてしまってすいません
Re:テストの平均値を求める
Posted: 2010年6月20日(日) 23:56
by Apple
原文では、
#include <stdio.h>
#define maxStudents 10
????????
int main(void)
{
????????;
double mathAve, engAve, totalAve;
となっていて、ヒントでcalcAveのプロトタイプが
int calcAve(student input[/url], double *englishAverage, double *mathAverage, double *totalAverage)
であると書いてあったのでそのまま埋めてしまいました。
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 00:11
by パコネコ
/*
int i=0;
while (i<maxStudents) {
if (scanf("%d %d", &students.english, &students.math) != 2)
break;
i++;
}
students.english = students.math = -1;
calcAve(students, ????????);
printf ("English average is %6.2f\n", engAve);
printf ("Math average is %6.2f\n", mathAve);
printf ("Total average is %6.2f\n", totalAve);
return 0;
}
*/
この部分も自分でですか?
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 00:13
by Apple
そこも元々記述されてあった部分なので、変更しなくて良いです。
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 00:22
by パコネコ
そうですか…calcAve(students, ????????); の部分結局1つしか枠がないんですね
4つのデータをもらう準備しているのでやはり考えられるのは
1)
???????の部分で3つ書く
2)
studentsの中に含まれている
3)
そもそも4つではない
というところでしょうか?
これを3つ目の????だとすると
1つめの???はほぼサブルーチンで間違いはないと思います。
2つ目の????は何らかの宣言文ぽいですね
この場合students.english, students.mathを宣言するんですかね
やり方がわからないのが残念です。
まとめるとこうなるので間違ってないでしょうか?
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 00:37
by fatens
おそらく、構造体の配列, typedef, ポインタあたりを使うのだとは思いますが、
一度原文をそのまま提示してみてください。
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 06:59
by Apple
そのままの文ですと、
#include <stdio.h>
#define maxStudents 10
????????
int main(void)
{
????????;
double mathAve, engAve, totalAve;
int i=0;
while (i<maxStudents) {
if (scanf("%d %d", &students.english, &students.math) != 2)
break;
i++;
}
students.english = students.math = -1;
calcAve(students, ????????);
printf ("English average is %6.2f\n", engAve);
printf ("Math average is %6.2f\n", mathAve);
printf ("Total average is %6.2f\n", totalAve);
return 0;
}
になります。
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 08:03
by シエル
typedef struct {
int english,math;
}DATA;
DATA students[maxStudents]
こんな構造体を作って配列にして、ループさせて入力を行い、
calcAve関数に構造体配列、mathAve, engAve, totalAveのアドレスを渡して、
calcAve側で計算させて、mathAve, engAve, totalAveにそれぞれ値を代入し、
main関数に戻ってきて、そいつを表示するだけ。
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 08:50
by fatens
文章による説明もあったと思うので、それも書いてほしかったのですが、
???????? の部分は好きに書いていいものとして回答します。
1つめの????????
english, mathをメンバに持つ構造体を作り、typedefで名前をstudentとします。
シエルさんはDATAとしていますが、これだと以下のプロトタイプ宣言と合いません。
int calcAve(student input[/url], double *englishAverage, double *mathAverage, double *totalAverage)
そして、calcAve 関数の処理を書きます。
2つめの????????
student型で要素数がmaxStudents+1の配列を作ります。
そうしないと
while (i<maxStudents) {
if (scanf("%d %d", &students.english, &students.math) != 2)
break;
i++;
}
students.english = students.math = -1;
この部分でmaxStudents人のデータを入力したとき、
最後で配列の範囲外の要素にアクセスしてしまいます。
3つめの????????はもう回答がありますので省略します。
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 09:07
by シエル
DATA students[maxStudents]って書いてるんですが…
もしかして「students]のsが多いってこと言ってますか?
↓のような文もあったので、studentsかstudentなのかはっきりしてません。
students.english = students.math = -1;
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 09:08
by toyo
関数から値を取得するには普通は関数の戻り値を使いますね
ただこの方法ですと値を1個しか受け渡しできません
複数の値を受け取りたい場合はいくつかの方法がありますが(構造体や配列で渡すとか)
一つの方法として関数の引数を使う方法があります
関数の呼び出し側で値受け取り用の変数を用意してその変数のアドレス(ポインタ)を引数にして関数を呼び出します
関数側では引数のポインタに値を入れるだけで呼び出し側でその値が使えるようになります
calcAveのプロトタイプが
int calcAve(student input[/url], double *englishAverage, double *mathAverage, double *totalAverage)
なら
calcAve(students, &engAve, &mathAve, &totalAve);
と呼び出せばよいでしょう
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 09:12
by シエル
あっfatensさんの意味がわかった…
プロトタイプ宣言思いっきり無視してました。。。
確かにDATAじゃだめですね。すいません。。。
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 20:10
by Apple
1つめの???で、
typedef struct {
int english;
int math;
}student;
int calcAve(student input[/url], double *englishAverage,
double *mathAverage, double *totalAverage)
{
まで書きましたが、calcAveの()内に4種類あって3つめの???のように
calcAve(students, ????????);
と()内が2種類の形になるように作るのが良く分からないです。
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 20:59
by シエル
toyoさんがおっしゃってるように
calcAveのプロトタイプが
int calcAve(student input[/url], double *englishAverage, double *mathAverage, double *totalAverage)
なら
calcAve(students, &engAve, &mathAve, &totalAve);
と呼び出せばよいでしょう。
calcAve(students, ????????);
のように2つの引数だけでこの関数を呼ぶように指示されてるのですか?
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 21:03
by フリオ
>calcAve(students, ????????);
>と()内が2種類の形になるように作るのが良く分からないです。
???...は、ここを文字列で埋めてソースを完成させろ、という意味で、
引数が2つとは限らないのでは?
実際、calcAveの引数は2つじゃないし。
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 21:35
by Apple
確かにプロトタイプに4つある以上、引数は4つですね。
あと、どうしてもcalcAve内の書き方が分からないです…
Re:テストの平均値を求める
Posted: 2010年6月21日(月) 22:29
by 組木紙織
>あと、どうしてもcalcAve内の書き方が分からないです…
処理内容はわかるんですよね。
Cで書き方が分からない部分は日本語で書いて見てください。