C, C++, C#/C++ 프로그래밍 입문

Swap, Sort, 로또 번호 생성 method

SB_J00N 2023. 8. 1. 16:53
#include <iostream>
using namespace std;

void Swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;

}
void Sort(int numbers[], int count)
{
    for (int i = 0; i < count-1; i++)
    {
        for (int j = i; j < count - 1; j++) 
        {
            if (numbers[j] >= numbers[j + 1]) 
            {
                Swap(numbers[j], numbers[j + 1]);
            }
        }
    }
}

void ChooseLotto(int numbers[])
{
    srand((unsigned)time(0));
    int count = 0;
    
    
    while (count != 6)
    {
        int randValue = 1 + (rand() & 45);
        bool found = false;

        for (int i = 0; i < count; i++)
        {
            if (numbers[i] == randValue)
            {
                found = true;
                break;
            }
        }
        if (!found)
        {
            numbers[count] = randValue;
            count++;
        }

    }
    
    Sort(numbers, 6);
}

int main()
{
    int numbers[6] = { };
    ChooseLotto(numbers);
    for (int i =0; i < 6; i++)
        cout << numbers[i] << endl;

    return 0;
}