군대 가기 전 마지막 코딩...
~cpp
#include <stdio.h>
#include <stdlib.h>
#define NUMBER_OF_SCORES 5
struct ArratData{
int sum;
float avg;
int max;
int min;
};
struct ScoreData{
int score;
int rank;
};
void InputScores(struct ArratData* aArrayData, struct ScoreData* aArray);
void PrintArray(struct ArratData* aArrayData, struct ScoreData* aArray);
void RankScores(struct ScoreData* aArray);
void PrintRanks(struct ScoreData* aArray);
int main(){
struct ScoreData scoreArray[NUMBER_OF_SCORES];
struct ArratData arrayData;
InputScores(&arrayData, scoreArray);
PrintArray(&arrayData, scoreArray);
RankScores(scoreArray);
PrintRanks(scoreArray);
system("pause");
return 0;
}
void InputScores(struct ArratData* aArrayData, struct ScoreData* aArray){
/*Enter a number for score[0] : 80
Enter a number for score[1] : 50
Enter a number for score[2] : 99
Enter a number for score[3] : 74
Enter a number for score[4] : 88
*/
int count = 0;
while(count < NUMBER_OF_SCORES){
printf("\nEnter a number for score[%d] : ", count);
scanf("%d", &(aArray[count].score));
aArray[count].rank = 1;
if(count == 0){
aArrayData->min = aArray[count].score;
aArrayData->max = aArray[count].score;
aArrayData->sum = aArray[count].score;
}
else{
if(aArray[count].score < aArrayData->min){
aArrayData->min = aArray[count].score;
}
if(aArray[count].score > aArrayData->max){
aArrayData->max = aArray[count].score;
}
aArrayData->sum += aArray[count].score;
}
count++;
}
aArrayData->avg = (float)aArrayData->sum/NUMBER_OF_SCORES;
}
void PrintArray(struct ArratData* aArrayData, struct ScoreData* aArray){
/*[[ The contents of score array ]]
80 50 99 74 88
sum = 391, avg = 78.2
max = 99, min = 50
*/
int count;
printf("\n[[ The contents of score array ]]\n\n");
for(count = 0; count < NUMBER_OF_SCORES; count++){
printf("%d\t", aArray[count].score);
}
printf("\n\nsum = %d, avg = %.1f", aArrayData->sum, aArrayData->avg);
printf("\n\nmax = %d, min = %d\n", aArrayData->max, aArrayData->min);
}
void RankScores(struct ScoreData* aArray){
int count;
int cmpCount;
for(count = 0; count < NUMBER_OF_SCORES; count++){
for(cmpCount = 0; cmpCount < NUMBER_OF_SCORES; cmpCount++){
if(aArray[count].score < aArray[cmpCount].score){
/*배열 내에 나보다 큰 숫자가 있을 때마다 나의 랭크는 내려간다.*/
aArray[count].rank++;
}
}
}
}
void PrintRanks(struct ScoreData* aArray){
/*score[0] : 80, rank = 3
score[1] : 50, rank = 5
score[2] : 99, rank = 1
score[3] : 74, rank = 4
score[4] : 88, rank = 2*/
int count;
for(count = 0; count < NUMBER_OF_SCORES; count++){
printf("\nscore[%d] : %d, rank = %d\n", count, aArray[count].score, aArray[count].rank);
}
}