#include <stdio.h>
#define NINZU 5 //NINZU=5に置換
#define KYOKA 3 //KYOKA=3に置換
int main(void)
{
int score[NINZU][KYOKA]={{87,78,91} //{}の数=NINZU=5
,{86,67,89},{78,82,90},{65,55,71},{69,82,88}
}; //{}内の要素数=KYOKA=3
int i,j,sum; //変数 i,j,sumの宣言
//(1)学生ごとの平均点を求める
for(i=0;i<NINZU;i++){
sum=0;
for(j=0;j<KYOKA;j++)
sum+=score[j];
printf("%d番目の学生の平均点は%2.f\n",i+1,(double)sum/KYOKA);
}
//(2)教科ごとの平均点を求める
for(j=0;j<KYOKA;j++){
sum=0;
for(i=0;i<NINZU;i++)
sum+=score[j];
printf("%d番目の教科の平均点は%.2f\n",j+1,(double)sum/KYOKA);
}
}
-実行結果-
1番目の学生の平均点は85
2番目の学生の平均点は81
3番目の学生の平均点は83
4番目の学生の平均点は64
5番目の学生の平均点は80
1番目の教科の平均点は128.33
2番目の教科の平均点は121.33
3番目の教科の平均点は143.00
%2.f が何を表しているのか教えてください。
Re: %2.f が何を表しているのか教えてください。
※コードを貼り付ける場合はcodeタグで囲ってください。
それはフォーマット指定子と呼ばれるもので
%2.f はfloatを2桁まで表示する、という意味だと思います。
%.2f はfloatを小数点以下2桁まで表示する、みたいです。。
参考:フォーマット指定子一覧
それはフォーマット指定子と呼ばれるもので
%2.f はfloatを2桁まで表示する、という意味だと思います。
%.2f はfloatを小数点以下2桁まで表示する、みたいです。。
参考:フォーマット指定子一覧
Re: %2.f が何を表しているのか教えてください。
% : 変換指定子の始まり
2 : フィールド幅 (少なくとも2文字を使って出力する)
. : 精度の指定 (整数が省略されているので0とみなされ、0なので小数点と小数点以下の桁は出力しない)
f : double型のデータを十進数で出力する
という意味です。
N1570 7.21.6.1 The fprintf functionより引用
2 : フィールド幅 (少なくとも2文字を使って出力する)
. : 精度の指定 (整数が省略されているので0とみなされ、0なので小数点と小数点以下の桁は出力しない)
f : double型のデータを十進数で出力する
という意味です。
N1570 7.21.6.1 The fprintf functionより引用
N1570 7.21.6.3 The printf functionより引用4 Each conversion specification is introduced by the character %. After the %, the following
appear in sequence:
— Zero or more flags (in any order) that modify the meaning of the conversion
specification.
— An optional minimum field width. If the converted value has fewer characters than the
field width, it is padded with spaces (by default) on the left (or right, if the left
adjustment flag, described later, has been given) to the field width. The field width
takes the form of an asterisk * (described later) or a nonnegative decimal integer. 275)
— An optional precision that gives the minimum number of digits to appear for the d, i,
o, u, x, and X conversions, the number of digits to appear after the decimal-point
character for a, A, e, E, f, and F conversions, the maximum number of significant
digits for the g and G conversions, or the maximum number of bytes to be written for
s conversions. The precision takes the form of a period (.) followed either by an
asterisk * (described later) or by an optional decimal integer; if only the period is
specified, the precision is taken as zero. If a precision appears with any other
conversion specifier, the behavior is undefined.
— An optional length modifier that specifies the size of the argument.
— A conversion specifier character that specifies the type of conversion to be applied.
(中略)
8 The conversion specifiers and their meanings are:
(中略)
f,F A double argument representing a floating-point number is converted to
decimal notation in the style [−]ddd.ddd, where the number of digits after
the decimal-point character is equal to the precision specification. If the
precision is missing, it is taken as 6; if the precision is zero and the # flag is
not specified, no decimal-point character appears. If a decimal-point
character appears, at least one digit appears before it. The value is rounded to
the appropriate number of digits.
2 The printf function is equivalent to fprintf with the argument stdout interposed
before the arguments to printf.
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)
Re: %2.f が何を表しているのか教えてください。
これらのような、ソースコードを見ればだれでもわかるようなコメントは不要だと思います。hanya841 さんが書きました: #define NINZU 5 //NINZU=5に置換
#define KYOKA 3 //KYOKA=3に置換
int i,j,sum; //変数 i,j,sumの宣言
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。
プログラムは思ったとおりには動かない。書いたとおりに動く。