[코테]/[GroomEdu]
[COS PRO 2급] 4차 10번_XX시험 합격자 수 구하기 (C/C++)
Sky_
2021. 5. 22. 16:00
주어진 코드
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int scores[], int scores_len, int cutline) {
int answer = 0;
//빈칸
return answer;
}
int main() {
int scores[5] = { 80, 90, 55, 60, 59 };
int scores_len = 5;
int cutline = 60;
int ret = solution(scores, scores_len, cutline);
printf("solution 함수의 반환 값은 %d 입니다.\n", ret);
}
완성 코드
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int scores[], int scores_len, int cutline) {
int answer = 0;
for (int i = 0; i < scores_len; i++) {
if (scores[i] >= cutline) answer++;
}
return answer;
}
int main() {
int scores[5] = { 80, 90, 55, 60, 59 };
int scores_len = 5;
int cutline = 60;
int ret = solution(scores, scores_len, cutline);
printf("solution 함수의 반환 값은 %d 입니다.\n", ret);
}
예시
scores | scores_len | cutline | return |
[80, 90, 55, 60, 59] | 5 | 60 | 3 |
부가설명
각 배열에 담긴 점수가 지정된 커트라인 이상일 때, 합격이므로
for문과 >=연산자를 사용하여
커트라인보다 같거나 클 경우
합격자 수 answer++를 실행시켰다.
goorm
구름은 클라우드 기술을 이용하여 누구나 코딩을 배우고, 실력을 평가하고, 소프트웨어를 개발할 수 있는 클라우드 소프트웨어 생태계입니다.
www.goorm.io