2차원 배열 관련 예제 질문
조회수 1122회
학생 5명의 점수를 입력받아 점수, 합계, 평균, 등수를 출력해야합니다. 우선 코드를 작성해봤는데 계속
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define STUDENT 5
int sum(int size, int arr[][6]) // 총점 구하는 함수
{
int i, j;
for (i = 0; i <= size; i++)
{
for (j = 0; j <= 2; j++)
arr[i][4] += arr[i][j];
}
return 0;
}
float avg(int size, int arr[][6]) // 평균 구하는 함수
{
int i;
for (i = 0; i <= size; i++)
{
arr[i][5] = arr[i][4] / 3;
}
return 0;
}
int main(void)
{
int arr[6][6] = { 0 };
int scnum[6][6] = { 0 };
int i, j, k = 0;
int size = 4;
for (i = 0; i <= size; i++)
{
printf("학번 : \n");
scanf("%d", &scnum[i]);
printf("국 영 수 점수 입력 : \n");
for (j = 0; j <= 2; j++)
{
scanf("%d", &arr[i][j]);
}
}
sum(size, arr);
avg(size, arr);
for (k = i + 1; k <= STUDENT; k++) {
if (arr[i][3] > arr[k][3]) {
arr[k][6] += 1;
}
else if (arr[i][3] < arr[k][3]) {
arr[i][6] += 1;
}
else {
arr[i][6] += 0;
arr[k][6] += 0;
}
}
printf("학번\t국어\t영어\t수학\t총점\t평균\t석차\n");
for (i = 0; i <= size; i++)
{
printf("%4d\t", scnum[i]);
for (j = 0; j <= 4; j++) {
printf("%d\t", arr[i][j]);
}
printf("%.3f\t", arr[i][5]);
printf("%d", arr[i][6]);
printf("\n");
}
return 0;
}
학번은 정수형 4자리만 나오고,
학번 국어 영어 수학 총점 평균 석차
**** *** *** *** *** . 5
**** *** *** *** *** . 4
**** *** *** *** *** . 1
**** *** *** *** *** . 3
**** *** *** *** *** . 2
이런식으로 결과가 출력되게 하고싶은데 계속 결과가 이상하게 나오네요(한줄 띄기는 없어도 됩니다.) c를 배운지 얼마 안되어서 이 코드에 이상한 부분이 뭔지 알고 싶습니다ㅠㅠ
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
도움이 될지 모르겠네요.
아래 코드 참고하세요.
- 코드
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #define STUDENT_NUM 5 struct student_data { int id; float kor_score; float eng_score; float math_score; float sum_score; float ave_score; int ranking; }; struct student_data* sort_by_sum(const struct student_data data[], int count); void calc_ranking(struct student_data data[], int count); int main(void) { struct student_data my_class[STUDENT_NUM]; for (int i = 0; i < STUDENT_NUM; i++) { printf("학번: "); scanf("%d", &(my_class[i].id)); printf("국어 점수 입력: "); scanf("%f", &(my_class[i].kor_score)); printf("영어 점수 입력: "); scanf("%f", &(my_class[i].eng_score)); printf("수학 점수 입력: "); scanf("%f", &(my_class[i].math_score)); } for (int i = 0; i < STUDENT_NUM; i++) { my_class[i].sum_score = my_class[i].kor_score + my_class[i].eng_score + my_class[i].math_score; my_class[i].ave_score = my_class[i].sum_score / 3; } calc_ranking(my_class, STUDENT_NUM); printf("학번\t국어\t영어\t수학\t총점\t평균\t석차\n"); for (int i = 0; i < STUDENT_NUM; i++) { printf("%04d\t", my_class[i].id); printf("%.2f\t", my_class[i].kor_score); printf("%.2f\t", my_class[i].eng_score); printf("%.2f\t", my_class[i].math_score); printf("%.2f\t", my_class[i].sum_score); printf("%.2f\t", my_class[i].ave_score); printf("%2d\n", my_class[i].ranking); } return 0; } struct student_data* sort_by_sum(const struct student_data data[], int count) { struct student_data* p_data_copied = (struct student_data*)malloc(sizeof(struct student_data) * count); for (int i = 0; i < count; i++) p_data_copied[i] = data[i]; struct student_data temp = { 0, }; float max = 0; int max_index = 0; for (int i = 0; i < count; i++) { max = p_data_copied[i].sum_score; max_index = i; for (int j = i + 1; j < count; j++) { if (p_data_copied[j].sum_score > max) { max = p_data_copied[j].sum_score; max_index = j; } } temp = p_data_copied[max_index]; p_data_copied[max_index] = p_data_copied[i]; p_data_copied[i] = temp; } return p_data_copied; } void calc_ranking(struct student_data data[], int count) { struct student_data* p_data_sorted = sort_by_sum(data, count); int ranking = 1; int same_count = 0; for (int i = 0; i < count; i++) { if (i == 0) { p_data_sorted[i].ranking = ranking; ranking++; } else { if (p_data_sorted[i].sum_score == p_data_sorted[i - 1].sum_score) { same_count++; p_data_sorted[i].ranking = ranking - same_count; ranking++; } else { same_count = 0; p_data_sorted[i].ranking = ranking; ranking++; } } } for (int i = 0; i < count; i++) { for (int j = 0; j < count; j++) { if (p_data_sorted[i].id == data[j].id) data[j].ranking = p_data_sorted[i].ranking; } } free(p_data_sorted); return; }
- 결과
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력