네이버 지식인 문제 풀이
//=============================
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <stdbool.h>
# define ERROR 1
# define LEN 10
//=============================
typedef struct Num {
int element[LEN];
int min;
int max;
}Num, *ptrNum;
// FUNCTION PROTOTYPE ================================
void init(Num** param); // ======================= [1]
void randValueInput(Num** param); // ============= [2]
void minMax(Num** param); // ===================== [3]
// ===================================================
int main(void) {
ptrNum node = NULL;
node = (ptrNum)malloc(sizeof(Num));
if (node == NULL) {
printf("malloc fail ...!!!\n");
exit(ERROR);
}
init(&node);
randValueInput(&node);
minMax(&node);
free(node);
return 0;
} // end of main function
// ===================================================
void init(Num** param) { // ====================== [1]
int i; // index
for (i = 0; i < LEN; i++) {
(** param).element[i] = 0x0;
}
(** param).min = 0x0; // 최솟값
(** param).max = 0x0; // 최댓값
} // end of init function
// ===================================================
void randValueInput(Num** param) { // ============ [2]
srand((unsigned)time(NULL));
int i; // index
for (i = 0; i < LEN; i++) {
(** param).element[i] = rand() % 20 + 1;
printf("%02d", (** param).element[i]);
if (i != LEN - 1) {
printf(" - ");
}
}
printf("\n\n");
} // end of randValueInput function
// ===================================================
void minMax(Num** param) { // ==================== [3]
int i, j; // index
bool flag;
int temp = 0x0;
for (i = 0; i < LEN - 1; i++) {
flag = false;
for (j = 0; j < LEN - 1; j++) {
if ((** param).element[j] > (** param).element[j + 1]) {
temp = (** param).element[j];
(** param).element[j] = (** param).element[j + 1];
(** param).element[j + 1] = temp;
flag = true;
}
}
if (flag == false) {
break;
}
}
// 최솟값
(** param).min = (** param).element[0];
// 최댓값
(** param).max = (** param).element[LEN - 1];
printf("최솟값 : %02d \n최댓값 : %02d \n", (** param).min, (** param).max);
} // end of minMax function