작은숲:위키노트/C 언어 예제/최대·최소값 찾기: 두 판 사이의 차이
보이기
잔글 문자열 찾아 바꾸기 - "분류:공유" 문자열을 "분류:위키노트/공유" 문자열로 |
잔글 문자열 찾아 바꾸기 - "<source" 문자열을 "<syntaxhighlight" 문자열로 |
||
| 1번째 줄: | 1번째 줄: | ||
< | <syntaxhighlight lang="c"> | ||
/** | /** | ||
* Copyright (c) 2001,2002 Yoon, Hyunho <hhyoon@kldp.org> | * Copyright (c) 2001,2002 Yoon, Hyunho <hhyoon@kldp.org> | ||
2021년 3월 28일 (일) 12:51 판
<syntaxhighlight lang="c"> /**
* Copyright (c) 2001,2002 Yoon, Hyunho <hhyoon@kldp.org> * http://mooo.org * ---------------------------------------------------------------------- * * LICENSE * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/**
* 사용자에게 n명의 학생의 점수를 입력받아 이 값 중 최대/최소값을 구하고, * 총합과 평균을 구하는 프로그램 .. */
- include <stdio.h>
int main() { int i; // 인덱스 변수
int n; // 몇명의 점수를 입력받을 것인가?
int score; // 점수
int max; // 입력받은 점수 중 최대값
int min; // 입력받은 점수 중 최소값
int sum; // 입력받은 점수 총합
i = 0; // 인덱스 변수는 0으로 초기화
n = 0;
score = 0;
max = 0; // 최대값을 저장할 변수는 항상 가능한 점수 중 최저값으로 초기화한다!
min = 100; // 최소값을 저장할 변수는 항상 가능한 점수 중 최대값으로 초기화한다!
sum = 0; // 자기 자신과 더해지는, 즉 합을 구하는 변수는 항상 0으로 초기화!
/*
* 다섯명 이상 입력할 수 있도록!
* 즉, 입력한 값이 5보다 작을 경우는 다시 입력하게 한다.
*/
do {
printf("Enter the number of scores: ");
scanf("%d", &n);
} while (n < 5);
printf("\n");
/*
* 입력받은 n명 만큼의 성적을 입력받는다.
* 점수를 입력 받으면서 점수가 0-100 사이인지 검사하고,
* 총합과 최대/최소를 구한다.
*/
do {
printf("Enter the %dth score (1-100): ", i + 1);
scanf("%d", &score);
/*
* 점수가 0부터 100 사이인지 검사 ..
* 아니라면 다시 입력할 수 있도록!
*/
if (score < 0 || score > 100)
continue;
sum += score; // 입력받은 점수를 총합에 더한다.
if (score > max) // 입력받은 값이 현재 최대값보다 크면 그 값이 새로운 최대값이 된다.
max = score;
if (score < min) // 입력받은 값이 현재 최소값보다 작으면 그 값이 새로운 최소값이 된다.
min = score;
i++; // 인덱스 변수 증가
} while (i < n);
printf("\n\nMax score is %d.\n", max);
printf("Min score is %d.\n", min);
printf("Sum is %d.\n", sum);
/*
* 평균을 구할 때는 항상 두 정수형 값 중 하나는 ..
* double 혹은 float 형으로 형변환 해줘야 한다!
*/
printf("Average is %3.2f.\n\n", (double) sum / n);
return 0;
}</source>