인터넷에서 본것처럼 했으나 결과가 나오지 않아서 부탁드립니다.
이것은 저가 만든 오름차순으로 결과값을 갖는 프로그램입니다.
#include <stdio.h>
int main(){
int student, subject;
int stu, sub;
char name[100][21];
int score[100][10];
int sum;
int temp, k, num;
char copy[100][21];
printf("학생수 과목수\n");
scanf("%d %d", &student, &subject);
for(stu=0; stu<student; stu++){
printf("학생의 이름\n");
scanf("%s", name[stu]);
for(sum=0, sub=0; sub<subject; sub++){
printf("학생의 성적\n");
scanf("%d", &score[stu][sub]);
sum += score[stu][sub];
}
score[stu][sub] = sum;
}
k = student;
for(num=0; num<k; num++){
for(stu=0; stu<student; stu++){
sub=subject;
if(score[stu][sub] < score[stu+1][sub]){
strcpy(copy[stu], name[stu]);
strcpy(name[stu], name[stu+1]);
strcpy(name[stu+1], copy[stu]);
for(sub=0; sub<=subject; sub++){
temp=score[stu+1][sub];
score[stu+1][sub]=score[stu][sub];
score[stu][sub]=temp;
}
}
}
student--;
}
for(stu=k-1; stu>=0; stu--){
printf("%s ", name[stu]);
for(sub=0; sub<=subject; sub++){
printf("%d ", score[stu][sub]);
}
printf("\n");
}
return 0;
}
저는 이것을 내림차순으로 나오게 하고 싶어서
if(score[stu][sub] < score[stu+1][sub]){
이부분의 부호를 인터넷에 널린것처럼 해보았으나 되지 않았습니다.
되지 않았던 이유와 내림차순의 방법부탁드립니다.
편하게 하려면 구조체랑 memcpy 적용하셈
앞에서부터 비교하려면 비교구간을 맨 뒤에서부터 하나씩 줄이고, 뒤에서부터 바꾸려면 비교구간을 앞에서부터 하나씩 줄이고