how to get different array in for loop every time?

Viewed 19
#include<stdio.h>
#include<time.h>


int main(void)
{
srand(time(NULL));
int answer;
int treatment = rand() % 4;
printf("###발모제 찾기###\n\n");

int cntShowBottle = 0;
int prevCntShowBottle = 0;

int ptr[4] = { 0,0,0,0 };
int bottle[4] = { 0, 0, 0, 0 };
int isincluded = 0;

for (int i = 1; i <= 3; i++)
{
    do {
        cntShowBottle = rand() % 2 + 2;
    } while (cntShowBottle == prevCntShowBottle);
    prevCntShowBottle = cntShowBottle;

    printf(" %d 번째 시도 : ", i);

    for (int j = 0; j < cntShowBottle; j++)
    {
        int randBottle = rand() % 4;

        if (bottle[randBottle] == 0)
        {
            bottle[randBottle] = 1;
            if (randBottle == treatment)
            {
                isincluded = 1;
            }
        }
        else
        {
            j--;
        }
    }
}

    if (bottle[0] == ptr[0] && bottle[1] == ptr[1] && bottle[2] == ptr[2] && bottle[3] == ptr[3])
    {
        int bottle[4] = { 0,0,0,0 };
        for (int j = 0; j < cntShowBottle; j++)
        {
            int randBottle = rand() % 4;

            if (bottle[randBottle] == 0)
            {
                bottle[randBottle] = 1;
                if (randBottle == treatment)
                {
                    isincluded = 1;
                }
            }
            else
            {
                j--;
            }
        }
    }
    else
    {
        return 0;
    }



    for (int i = 0; i < 4; i++)
    {
        ptr[i] = bottle[i];
    }



    for (int k = 0; k < 4; k++)
    {
        if (bottle[k] == 1)
            printf("%d ", k + 1);
    }
        printf("번 물약을 머리에 바릅니다.\n\n");
        
        if (isincluded == 1)
        {
            printf("성공!\n");
        }
        else
        {
            printf("실패...\n");
        }
        
        printf("\n ...계속 하려면 아무키나 누르세요...");
        getchar(0);



printf("\n\n발모제는 몇 번? : ");
scanf_s("%d", &answer);

if (answer == treatment+1)
{
    printf("\n 정답! \n");
}
else
{
    printf("\n 실패! \n 정답은 %d 였습니다.\n", treatment+1);
}


return 0;
}

in this loop, for (int j = 0; j < cntShowBottle; j++), 'bottle' array will be [0,1,1,1] or [1,1,0,0] etc. In this loop, how to get different array without overlapping(like [0,1,1,0] and again [0,1,1,0])?? I tried comparing each elements, if it takes overlapping array, makes 'bottle' array again. but it didn't run properly. please help..

0 Answers
Related