My student number: s124099 Student: 1 2 3 4 5 6 7 8 9 10 Average English: 70 98 51 61 55 67.0 Math : 100 56 91 53 86 73 76.5 Physics: 93 60 56 71 87 85 82 68 73 75.0 ------------------------------------------------------------- |
表示させる際の科目名は予習を兼ねて,文字列配列を使用してみましょう.以下の配列を用意して下さい.
char subject[3][8] = {"English", "Math", "Physics"};
#include <stdio.h> #include <stdlib.h> #include <time.h> main() { srand((unsigned) time(NULL)); int i, j, choice, sum, count, score[3][10]; char subject[3][8] = {"English", "Math", "Physics"}; printf("My student number: s124099\n\n"); printf("Student: "); for(i=0; i<=9; i++){ choice = rand() % 3; for(j=0; j<=2; j++) if(j!=choice) score[j][i] = rand() % 50 + 51; else score[j][i] = 0; } for(i=1; i<=10; i++) printf("%4d ", i); printf("Average\n"); for(i=0; i<=2; i++){ printf("%-7s: ", subject[i]); sum = 0; count = 0; for(j=0; j<=9; j++) if(score[i][j] != 0){ count++; sum += score[i][j]; printf("%4d ", score[i][j]); } else printf(" "); printf("%7.1f\n", sum * 1.0 / count); } printf("\n-------------------------------------------------------------\n\n"); return(0); } |