回答編集履歴

3

コード修正

2024/03/24 03:18

投稿

Refrain
Refrain

スコア537

test CHANGED
@@ -1,45 +1,68 @@
1
1
  一度`int`型変数に代入して`score[i][j]`に代入してはどうでしょうか?
2
2
  ```c
3
3
  #include <stdio.h>
4
+
5
+ // 生徒数やランク分けが変わった際、ここを修正すると対応できます
6
+ #define STUDENTS_COUNT 20 // 生徒数
7
+ #define MAX_POINT 100
8
+ #define RANK_STEP 20
9
+ #define RANK_COUNT (MAX_POINT / RANK_STEP + 1) // ランク数
10
+
4
- // 科目を示す列挙型
11
+ // 科目を示す列挙型(最後のSUBJECTS_COUNTは科目数)
5
12
  // これを作っておくと、後々にデータの参照が楽になります
6
13
  // 今回は英語(EN)、数学(MA)だけにしていますが、
7
14
  // 国語(JA)、理科(SC)、社会(SS)なども定義しておくと、
8
15
  // 更に便利になると思います
9
- enum SUBJECTS {EN, MA};
16
+ enum SUBJECTS {EN, MA, SUBJECTS_COUNT};
17
+
18
+ int scores[STUDENTS_COUNT][SUBJECTS_COUNT]; // 生徒数 x 科目数の配列
19
+ int distributions[RANK_COUNT][SUBJECTS_COUNT]; // 科目数 x ランク数の配列
20
+
21
+ void print_data () {
22
+ // 科目情報出力
23
+ printf("\t\tEng\tMath\n");
24
+ // 英語と数学の度数分布を取得
25
+ int score_lower, score_upper;
26
+ int score_english, score_math;
27
+ for (int i = 0; i < RANK_COUNT; i ++) {
28
+ score_lower = i * RANK_STEP; // ランク帯の下限値
29
+ score_upper = (i + 1) * RANK_STEP; // ランク帯の上限値
30
+ score_english = distributions[i][EN]; // 英語の度数
31
+ score_math = distributions[i][MA]; // 数学の度数
32
+ if (i < RANK_COUNT - 1) {
33
+ printf("%d以上\t%d未満\t%d\t%d\n", score_lower, score_upper, score_english, score_math);
34
+ } else {
35
+ printf("%d点\t\t%d\t%d\n", score_lower, score_english, score_math);
36
+ }
37
+ }
38
+ }
10
39
 
11
40
  int main (void) {
12
- // 生徒数や科目数が変わった際、ここを修正すると対応できます
13
- // scores[生徒の番号][科目番号]で任意の生徒・科目の得点を取得できます
14
- int students_count = 20, students_index; // 生徒数カウンタ
41
+ int students_index; // 生徒数カウンタ
15
- int subjects_count = 2, subjects_index; // 科目数カウンタ
42
+ int subjects_index; // 科目数カウンタ
16
- int scores[students_count][subjects_count]; // 生徒数 x 科目数の配列
17
-
18
- // ランク付けのステップが変わった際、ここを修正すると対応できます
19
- // distribution[科目番号][ランク]で任意の科目・ランクの度数が取得できます
20
- int rank_step = 20; // ランク付けステップ
43
+ int rank_index; // ランクカウンタ
21
- int rank_count = 100 / rank_step, rank_index; // ランク数とカウンタ
22
- int distribution[subjects_count][rank_count]; // 科目数 x ランク数のステップ
23
-
24
44
  int score = 0; // 標準入力からの代入先
25
45
  // 生徒数だけループ
26
- for (students_index = 0; students_index < students_count; students_index ++) {
46
+ for (students_index = 0; students_index < STUDENTS_COUNT; students_index ++) {
27
- printf("%d人目", students_index + 1); // 何人目か表示する
47
+ printf("%d人目\n", students_index + 1); // 何人目か表示する
28
48
  // 科目数だけループ
29
- for (subjects_index = 0; subjects_index < subjects_count; subjects_index ++) {
49
+ for (subjects_index = 0; subjects_index < SUBJECTS_COUNT; subjects_index ++) {
30
50
  scanf("%d", &score); // 標準入力から得点を取得する
31
51
  scores[students_index][subjects_index] = score; // 任意の生徒・科目の得点を変更
32
- rank_index = scores[students_index][subjects_index] / rank_step; // ランクを取得
52
+ rank_index = scores[students_index][subjects_index] / RANK_STEP; // ランクを取得
33
- distribution[subjects_index][rank_index] ++; // 任意の科目・ランクの度数を加算
53
+ distributions[rank_index][subjects_index] ++; // 任意の科目・ランクの度数を加算
34
54
  }
35
55
  }
56
+ // 出題内容の出力
57
+ print_data();
58
+ // 以下、他の出力事例
36
- // 数学の得点を出力
59
+ // 数学の得点を出力
37
- for (students_index = 0; students_index < students_count; students_index ++) {
60
+ for (students_index = 0; students_index < STUDENTS_COUNT; students_index ++) {
38
61
  printf("%d人目の点数は%d点です", students_index, scores[students_index][MA]);
39
62
  }
40
- // 英語の度数分布を取得
63
+ // 英語の度数分布を取得
41
- for (rank_index = 0; rank_index < rank_count; rank_index ++) {
64
+ for (rank_index = 0; rank_index < RANK_COUNT; rank_index ++) {
42
- printf("%d点以上%d点未満の人数は%d人です", rank_index * rank_step, (rank_index + 1) * rank_step, distribution[EN][rank_index]);
65
+ printf("%d点以上%d点未満の人数は%d人です", rank_index * rank_step, (rank_index + 1) * rank_step, distribution[rank_index][EN]);
43
66
  }
44
67
  }
45
68
  ```

2

コード修正

2024/03/23 16:23

投稿

Refrain
Refrain

スコア537

test CHANGED
@@ -1,7 +1,14 @@
1
1
  一度`int`型変数に代入して`score[i][j]`に代入してはどうでしょうか?
2
2
  ```c
3
3
  #include <stdio.h>
4
+ // 科目を示す列挙型
5
+ // これを作っておくと、後々にデータの参照が楽になります
6
+ // 今回は英語(EN)、数学(MA)だけにしていますが、
7
+ // 国語(JA)、理科(SC)、社会(SS)なども定義しておくと、
8
+ // 更に便利になると思います
9
+ enum SUBJECTS {EN, MA};
10
+
4
- int main () {
11
+ int main (void) {
5
12
  // 生徒数や科目数が変わった際、ここを修正すると対応できます
6
13
  // scores[生徒の番号][科目番号]で任意の生徒・科目の得点を取得できます
7
14
  int students_count = 20, students_index; // 生徒数とカウンタ
@@ -22,9 +29,17 @@
22
29
  for (subjects_index = 0; subjects_index < subjects_count; subjects_index ++) {
23
30
  scanf("%d", &score); // 標準入力から得点を取得する
24
31
  scores[students_index][subjects_index] = score; // 任意の生徒・科目の得点を変更
25
- rank_index = score[students_index][subjects_index] / rank_step; // ランクを取得
32
+ rank_index = scores[students_index][subjects_index] / rank_step; // ランクを取得
26
33
  distribution[subjects_index][rank_index] ++; // 任意の科目・ランクの度数を加算
27
34
  }
28
35
  }
36
+ // 数学の得点を出力
37
+ for (students_index = 0; students_index < students_count; students_index ++) {
38
+ printf("%d人目の点数は%d点です", students_index, scores[students_index][MA]);
39
+ }
40
+ // 英語の度数分布を取得
41
+ for (rank_index = 0; rank_index < rank_count; rank_index ++) {
42
+ printf("%d点以上%d点未満の人数は%d人です", rank_index * rank_step, (rank_index + 1) * rank_step, distribution[EN][rank_index]);
43
+ }
29
44
  }
30
45
  ```

1

コード修正

2024/03/23 16:08

投稿

Refrain
Refrain

スコア537

test CHANGED
@@ -1,25 +1,30 @@
1
1
  一度`int`型変数に代入して`score[i][j]`に代入してはどうでしょうか?
2
2
  ```c
3
3
  #include <stdio.h>
4
- int main (void) {
4
+ int main () {
5
+ // 生徒数や科目数が変わった際、ここを修正すると対応できます
6
+ // scores[生徒の番号][科目番号]で任意の生徒・科目の得点を取得できます
5
- int ni_max = 20; //
7
+ int students_count = 20, students_index; // 生徒とカウンタ
6
- int ka_max = 2; // 科目数
8
+ int subjects_count = 2, subjects_index; // 科目数とカウンタ
7
- int score[ni_max][ka_max];
9
+ int scores[students_count][subjects_count]; // 生徒数 x 科目数の配列
10
+
11
+ // ランク付けのステップが変わった際、ここを修正すると対応できます
12
+ // distribution[科目番号][ランク]で任意の科目・ランクの度数が取得できます
8
- int sc_step = 20; // ランク付けのステップ
13
+ int rank_step = 20; // ランク付けのステップ
9
- int EN[6];
10
- int i;
11
- for (i = 0; i < 6; i ++) EN[i] = 0;
14
+ int rank_count = 100 / rank_step, rank_index; // ランク数とカウンタ
15
+ int distribution[subjects_count][rank_count]; // 科目数 x ランク数のステップ
16
+
12
- int ni, ka, sc = 0; // ni人目、ka科目、入力データの代入先
17
+ int score = 0; // 標準入力からの代入先
13
- printf("%d", EN[0]);
18
+ // 生徒数だけループ
14
- for (ni = 0; ni < ni_max; ni ++) {
19
+ for (students_index = 0; students_index < students_count; students_index ++) {
15
- for (ka = 0; ka < ka_max; ka ++) {
16
- printf("%d人目", ni + 1);
20
+ printf("%d人目", students_index + 1); // 何人目か表示する
21
+ // 科目数だけループ
22
+ for (subjects_index = 0; subjects_index < subjects_count; subjects_index ++) {
17
- scanf("%d", &sc);
23
+ scanf("%d", &score); // 標準入力から得点を取得する
18
- score[ni][ka] = sc;
24
+ scores[students_index][subjects_index] = score; // 任意の生徒・科目の得点を変更
19
- i = score[ni][ka] / sc_step; // 20点刻みでイデッス指定
25
+ rank_index = score[students_index][subjects_index] / rank_step; // ンクを取得
20
- EN[i] ++;
26
+ distribution[subjects_index][rank_index] ++; // 任意の科目・ランクの度数を加算
21
27
  }
22
28
  }
23
- for (i = 0; i < 6; i ++) printf("%d", EN[i]);
24
29
  }
25
30
  ```