본문 바로가기
PRACTICE/Basic

[C] 함수를 통해 로또 번호 생성, 출력하기 (난수 사용)

by 1005 2020. 11. 10.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void input_nums(int* lotto_nums);
void print_nums(int* lotto_nums);
 
int main(void) {
 
    int lotto_nums[6];
    srand(time(NULL));
 
    input_nums(lotto_nums);
    print_nums(lotto_nums);
 
    return 0;
}
void input_nums(int* lotto_nums) {
    int i = -1;
    int num;
    while (i < 5) {
        printf("번호 입력: ");
        //scanf("%d", &num);
        num = rand() % 45 + 1;
        printf("%d\n", num);
        int flag = 0;
        for (int j = 0; j <= i; j++) {
            if (num == *(lotto_nums + j)) {
                printf("같은 번호가 있습니다!\n");
                flag = 1;
                break;
            }
        }
        if (flag == 0) {
            ++i;
            *(lotto_nums + i) = num;
        }
    }
}
void print_nums(int* lotto_nums) {
 
    printf("로또 번호: ");
    for (int i = 0; i < 6; i++) {
        printf("%2d  "*(lotto_nums + i));
    }
}
 

 

 

댓글