'떡밥'에 해당되는 글 2

  1. 2009/05/11 로또 추첨 프로그램 (2)
  2. 2009/05/11 C언어 배열을 함수 파라미터로 넘기는 방법 (2)
 

로또 추첨 프로그램

뻘글들/낚시글들 | 2009/05/11 19:38 | Posted by DMW
크리에이티브 커먼즈 라이선스
Creative Commons License
#include <stdio.h>

void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

void random_shuffle(int *array, int size) {
    int i, temp;
    
    for(i = 2; i < size; i++)
        swap(&array[i], &array[rand() % i]);
}

int main(int argc, char **argv) {
    int i;
    int lotto[45];
    
    for(i = 0; i < 45; i++)
        lotto[i] = i + 1;

    random_shuffle(lotto, 45);

    for(i = 0; i < 5; i++)
        printf("%d\n", lotto[i]);

    return 0;
}


Output:
1
2
3
4
5
6
30
13
17
26

저작자 표시 비영리 동일 조건 변경 허락

'뻘글들 > 낚시글들' 카테고리의 다른 글

로또 추첨 프로그램  (2) 2009/05/11
C언어 배열을 함수 파라미터로 넘기는 방법  (2) 2009/05/11
TAG 떡밥, 로또

댓글을 달아 주세요

  1. Favicon of http://btd86.tistory.com/ BlogIcon 숙신 2009/07/14 22:26  댓글주소  수정/삭제  댓글쓰기

    형, 이거 코드패드에서 한거 맞나요?
    어떻게 한거에요??

크리에이티브 커먼즈 라이선스
Creative Commons License

#include <stdio.h>

void function1(int array[], int size) {
    int i;
    for(i = 0; i < size; i++)
        printf("%d ", array[i]);
    printf("\n");
}

void function2(int *array, int size) {
    int i;
    for(i = 0; i < size; i++)
        printf("%d ", array[i]);
    printf("\n");
}

void function3(int *first, int *last) {
    for( ; first != last; first++)
        printf("%d ", *first);
    printf("\n");
}

int main(int argc, char **argv) {
    int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    function1(a, 10);
    function2(a, 10);
    function3(a, a + 10);

    return 0;
}
    


Output:
1
2
3
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 


저작자 표시 비영리 동일 조건 변경 허락

'뻘글들 > 낚시글들' 카테고리의 다른 글

로또 추첨 프로그램  (2) 2009/05/11
C언어 배열을 함수 파라미터로 넘기는 방법  (2) 2009/05/11
TAG 떡밥, 배열

댓글을 달아 주세요

  1. Favicon of http://flaxia-iii.tistory.com BlogIcon Kr015se 2009/06/14 00:03  댓글주소  수정/삭제  댓글쓰기

    개인적으로는 3번을 선호합니다. STL 스타일을 좋아해서요.
    다만 first가 last보다 뒤에 있다거나 하는 경우에 대비해 예외처리가 필요하긴 하지만요.