テストの平均値を求める
テストの平均値を求める
#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
#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:テストの平均値を求める
>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さん質問答えず質問書いてしまってすいません
>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:テストの平均値を求める
原文では、
#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)
であると書いてあったのでそのまま埋めてしまいました。
#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:テストの平均値を求める
/*
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;
}
*/
この部分も自分でですか?
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:テストの平均値を求める
そうですか…calcAve(students, ????????); の部分結局1つしか枠がないんですね
4つのデータをもらう準備しているのでやはり考えられるのは
1)
???????の部分で3つ書く
2)
studentsの中に含まれている
3)
そもそも4つではない
というところでしょうか?
これを3つ目の????だとすると
1つめの???はほぼサブルーチンで間違いはないと思います。
2つ目の????は何らかの宣言文ぽいですね
この場合students.english, students.mathを宣言するんですかね
やり方がわからないのが残念です。
まとめるとこうなるので間違ってないでしょうか?
4つのデータをもらう準備しているのでやはり考えられるのは
1)
???????の部分で3つ書く
2)
studentsの中に含まれている
3)
そもそも4つではない
というところでしょうか?
これを3つ目の????だとすると
1つめの???はほぼサブルーチンで間違いはないと思います。
2つ目の????は何らかの宣言文ぽいですね
この場合students.english, students.mathを宣言するんですかね
やり方がわからないのが残念です。
まとめるとこうなるので間違ってないでしょうか?
Re:テストの平均値を求める
そのままの文ですと、
#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;
}
になります。
#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:テストの平均値を求める
typedef struct {
int english,math;
}DATA;
DATA students[maxStudents]
こんな構造体を作って配列にして、ループさせて入力を行い、
calcAve関数に構造体配列、mathAve, engAve, totalAveのアドレスを渡して、
calcAve側で計算させて、mathAve, engAve, totalAveにそれぞれ値を代入し、
main関数に戻ってきて、そいつを表示するだけ。
int english,math;
}DATA;
DATA students[maxStudents]
こんな構造体を作って配列にして、ループさせて入力を行い、
calcAve関数に構造体配列、mathAve, engAve, totalAveのアドレスを渡して、
calcAve側で計算させて、mathAve, engAve, totalAveにそれぞれ値を代入し、
main関数に戻ってきて、そいつを表示するだけ。
Re:テストの平均値を求める
文章による説明もあったと思うので、それも書いてほしかったのですが、
???????? の部分は好きに書いていいものとして回答します。
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つめの????????はもう回答がありますので省略します。
???????? の部分は好きに書いていいものとして回答します。
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:テストの平均値を求める
DATA students[maxStudents]って書いてるんですが…
もしかして「students]のsが多いってこと言ってますか?
↓のような文もあったので、studentsかstudentなのかはっきりしてません。
students.english = students.math = -1;
もしかして「students]のsが多いってこと言ってますか?
↓のような文もあったので、studentsかstudentなのかはっきりしてません。
students.english = students.math = -1;
Re:テストの平均値を求める
関数から値を取得するには普通は関数の戻り値を使いますね
ただこの方法ですと値を1個しか受け渡しできません
複数の値を受け取りたい場合はいくつかの方法がありますが(構造体や配列で渡すとか)
一つの方法として関数の引数を使う方法があります
関数の呼び出し側で値受け取り用の変数を用意してその変数のアドレス(ポインタ)を引数にして関数を呼び出します
関数側では引数のポインタに値を入れるだけで呼び出し側でその値が使えるようになります
ただこの方法ですと値を1個しか受け渡しできません
複数の値を受け取りたい場合はいくつかの方法がありますが(構造体や配列で渡すとか)
一つの方法として関数の引数を使う方法があります
関数の呼び出し側で値受け取り用の変数を用意してその変数のアドレス(ポインタ)を引数にして関数を呼び出します
関数側では引数のポインタに値を入れるだけで呼び出し側でその値が使えるようになります
calcAveのプロトタイプが int calcAve(student input[/url], double *englishAverage, double *mathAverage, double *totalAverage) なら calcAve(students, &engAve, &mathAve, &totalAve); と呼び出せばよいでしょう
Re:テストの平均値を求める
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種類の形になるように作るのが良く分からないです。
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:テストの平均値を求める
toyoさんがおっしゃってるように
calcAveのプロトタイプが
int calcAve(student input[/url], double *englishAverage, double *mathAverage, double *totalAverage)
なら
calcAve(students, &engAve, &mathAve, &totalAve);
と呼び出せばよいでしょう。
calcAve(students, ????????);
のように2つの引数だけでこの関数を呼ぶように指示されてるのですか?
calcAveのプロトタイプが
int calcAve(student input[/url], double *englishAverage, double *mathAverage, double *totalAverage)
なら
calcAve(students, &engAve, &mathAve, &totalAve);
と呼び出せばよいでしょう。
calcAve(students, ????????);
のように2つの引数だけでこの関数を呼ぶように指示されてるのですか?
Re:テストの平均値を求める
>calcAve(students, ????????);
>と()内が2種類の形になるように作るのが良く分からないです。
???...は、ここを文字列で埋めてソースを完成させろ、という意味で、
引数が2つとは限らないのでは?
実際、calcAveの引数は2つじゃないし。
Re:テストの平均値を求める
>あと、どうしてもcalcAve内の書き方が分からないです…
処理内容はわかるんですよね。
Cで書き方が分からない部分は日本語で書いて見てください。
処理内容はわかるんですよね。
Cで書き方が分からない部分は日本語で書いて見てください。