【課題2−3】

 

(プログラム例1)

#include <stdio.h>

#define N 10

int main(void) {

         int i, score;

         int n_ok=0, n_ng=0;    /* 合格者数、不合格者数を0で初期化 */

         int max=0, min=100;    /* 最大値、最小値をそれぞれ十分小さな値、十分大きな値で初期化 */

         int sum=0;              /* 合計を0で初期化 */

 

         for (i = 0; i < N; i++) {

                 printf("Input score for student #%d: ", i+1);

                 scanf("%d", &score);   /* 得点入力 */

                 if (score >= 60) {     /* 60点以上なら */

                          n_ok++;         /* 合格者数を+1 */

                 } else {                 /* 60点未満なら */

                          n_ng++;         /* 不合格者数を+1 */

                 }

                 if (score > max) {     /* 現時点での最大値より大なら */

                          max = score;   /* それを新たな最大値に */

                 }

                 if (score < min) {     /* 現時点での最小値より小なら */

                          min = score;   /* それを新たな最小値に */

                 }

                 sum += score;           /* 合計に加える */

         }

         printf("number of OKs: %d\n", n_ok);

         printf("number of NGs: %d\n", n_ng);

         printf("max score: %d\n", max);

         printf("min score: %d\n", min);

         printf("average: %4.1f\n", (float)sum/N);

         return 0;

}

 

 

(実行例)

Input score for student #1: 56<enter>

Input score for student #2: 76<enter>

Input score for student #3: 91<enter>

Input score for student #4: 60<enter>

Input score for student #5: 73<enter>

Input score for student #6: 81<enter>

Input score for student #7: 44<enter>

Input score for student #8: 89<enter>

Input score for student #9: 68<enter>

Input score for student #10: 72<enter>

number of OKs: 8

number of NGs: 2

max score: 91

min score: 44

average: 71.0

 

 

(プログラム例2)

#include <stdio.h>

int main(void) {

         int cnt=0, score;

         int na=0, nb=0, nc=0, nd=0;

         int max=0, min=100;

         int sum=0;

 

         while (1) {     /* 無限ループ */

                 printf("Input score for student #%d: ", cnt+1);

                 scanf("%d", &score);   /* 得点入力 */

                 if (score == 999) {    /* 999なら終了 */

                          break;

                 }

                 if ((score < 0) || (score > 100)) {

                 /* 100より大きかったり、マイナスだったら、以下をスキップしループ先頭へ */

                          printf("Score should be 0-100\n");

                          continue;

                 }

                 if (score >= 80) {     /* 80点以上ならA */

                          na++;

                 } else if (score >= 70) { /* 70点以上80点未満ならB */

                          nb++;

                 } else if (score >= 60) { /* 60点以上70点未満ならC */

                          nc++;

                 } else {                 /* 60点未満ならD */

                          nd++;

                 }

                 if (score > max) {

                          max = score;

                 }

                 if (score < min) {

                          min = score;

                 }

                 sum += score;

                 cnt++;          /* 入力人数をインクリメント */

         }

        

         printf("number of students: %d\n", cnt);

         printf(" number of As: %d\n", na);

         printf(" number of Bs: %d\n", nb);

         printf(" number of Cs: %d\n", nc);

         printf(" number of Ds: %d\n", nd);

         printf("max score: %d\n", max);

         printf("min score: %d\n", min);

         printf("average: %4.1f\n", (float)sum/cnt);

 

         return 0;

}

 

(実行例)

Input score for student #1: 76<enter>

Input score for student #2: 95<enter>

Input score for student #3: 55<enter>

Input score for student #4: 68<enter>

Input score for student #5: 40<enter>

Input score for student #6: 81<enter>

Input score for student #7: 123<enter>

Score should be 0-100

Input score for student #7: 62<enter>

Input score for student #8: 79<enter>

Input score for student #9: 88<enter>

Input score for student #10: 67<enter>

Input score for student #11: -20<enter>

Score should be 0-100

Input score for student #11: 59<enter>

Input score for student #12: 72<enter>

Input score for student #13: 999<enter>

number of students: 12

 number of As: 3

 number of Bs: 3

 number of Cs: 3

 number of Ds: 3

max score: 95

min score: 40

average: 70.2