본문 바로가기
PRACTICE/Basic

[C] 시험 별 성적 최소값, 최대값 구하기

by 1005 2020. 11. 10.

 

한 학급은 최대 10명 까지의 학생들로 구성되어 있다.
각 학생들은 3번의 시험을 치른다. 학생들의 성적은 난수를 생성하여 얻는다.
각 시험에 대하여 최대점수와, 최저점수를 계산하여 출력한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
 
void get_minmax(int score[][3])
{
    int i, s, min, max;
    for (s = 0; s < 3; s++) {
        min = max = score[0][s];
        for (i = 0; i < 10; i++) {
            if (score[i][s] < min) min = score[i][s];
            if (score[i][s] > max) max = score[i][s];
        }
        printf("#%d번째 시험의 최대점수=%d\n", s+1, max);
        printf("#%d번째 시험의 최저점수=%d\n\n", s+1, min);
    }
}
 
int main(void)
{
    int i, s;
    int score[10][3]; //학생 10명과 3번의 시험
    
    puts("   시험1     시험2     시험3 ");
    puts("--------------------------------");
    for (i = 0; i < 10; i++) {
        for (s = 0; s < 3; s++) {
            score[i][s] = rand() % 100 + 1;
            printf("%8d ", score[i][s]);
        }
        printf("\n");
    }
    printf("\n");
    get_minmax(score);
    return 0;
}

 

 

 

댓글