개요
1차원 배열과 포인터를 이용하여 학생들의 점수를 입력받고 합계와 평균을 구한다. |
Code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
int student,sum = 0;
int* score = NULL;
printf("성적 처리할 학생 수를 입력하세요: ");
scanf_s("%d", &student);
//int 크기 * 입력받을 학생 수 만큼의 memory 크기 선언
score = (int*)malloc((unsigned)sizeof(int) * student);
printf("학생의 성적을 입력하세요.\n");
for (int i = 0; i < student; i++) {
do {
printf("%2d번 : ", i + 1);
scanf_s("%d", &score[i]);
//Error 값이 입력 된 경우 재입력
if (score[i] < 0 || score[i] > 100) {
printf("점수가 잘못입력 되었습니다.\n");
}
} while (score[i] < 0 || score[i] > 100);
sum += score[i]; //누적합
}
puts("=======================");
//입력받은 점수를 다시 출력
for (int i = 0; i < student; i++) {
printf("%2d번 : %2d\n", i+1, score[i]);
}
puts("=======================");
//총점과 평균을 출력
printf("총점 : %d\n평균 : %0.2f", sum, (float)sum / student);
free(score); //malloc 반납
return 0;
}
Output
'C\C++' 카테고리의 다른 글
소수 구하기 (0) | 2022.02.17 |
---|---|
Pointer & Array Ex01 (0) | 2022.02.17 |
2D Array Sum (0) | 2022.02.14 |
string ex01 (0) | 2022.02.14 |
do_while 연습 (0) | 2022.02.12 |