teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

fix array index

2020/07/23 12:58

投稿

YufanLou
YufanLou

スコア466

answer CHANGED
@@ -29,7 +29,7 @@
29
29
  for (int i = 0; i < 200; i++) {
30
30
  int score = 0;
31
31
  fscanf(fp, "%d\n", &score);
32
- if (score >= 0) { score_arr[i] = score; count++; }
32
+ if (score >= 0) { score_arr[count] = score; count++; }
33
33
  }
34
34
 
35
35
  for (int i = 0; i < count; i++) {

1

fix access out of bound by tracking count

2020/07/23 12:58

投稿

YufanLou
YufanLou

スコア466

answer CHANGED
@@ -25,13 +25,14 @@
25
25
  if (fp == NULL) { printf("Open error\n"); return 1; }
26
26
 
27
27
  int score_arr[200];
28
+ int count = 0;
28
29
  for (int i = 0; i < 200; i++) {
29
30
  int score = 0;
30
31
  fscanf(fp, "%d\n", &score);
31
- if (score >= 0) { score_arr[i] = score; }
32
+ if (score >= 0) { score_arr[i] = score; count++; }
32
33
  }
33
34
 
34
- for (int i = 0; i < 200; i++) {
35
+ for (int i = 0; i < count; i++) {
35
36
  // データ処理
36
37
  printf("%d\n", score_arr[i]);
37
38
  }