Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 521927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:18:05+00:00 2026-05-13T08:18:05+00:00

White is the checking of correct number at wrong position. But I don’t know

  • 0

“White” is the checking of correct number at wrong position.
But I don’t know how to count it correctly.

#include "stdafx.h"
#include "stdlib.h" 
#include "time.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int answer[4];
    int guess[4];
    int count = 0;

    srand(time(NULL)); 
    /*answer[0] = (rand() % 6)+1;
    answer[1] = (rand() % 6)+1;
    answer[2] = (rand() % 6)+1;
    answer[3] = (rand() % 6)+1;*/

    answer[0] = 3;
    answer[1] = 3;
    answer[2] = 5;
    answer[3] = 2;

    printf("%d %d %d %d\n", answer[0],  answer[1],  answer[2],  answer[3]);
    printf("          B W\n"); 

    do
    {
        int black = 0;
        int white = 0;
        count++;
        printf("Enter 4 numbers, this is your %d guess: ", count);
        scanf("%d %d %d %d", &guess[0], &guess[1], &guess[2], &guess[3]);
        printf("\n%d %d %d %d\n", guess[0], guess[1], guess[2], guess[3]);

        int g2[2][6];

        for (int a = 0;a < 4;a++)
            g2[0][a]=answer[a];

        for (int i = 0;i < 4;i++)
            g2[1][i]=guess[i];

        if (answer[0]==guess[0])
            black++;
        if (answer[1]==guess[1])
            black++;
        if (answer[2]==guess[2])
            black++;
        if (answer[3]==guess[3])
            black++;

        if (answer[1]==guess[0] || answer[2]==guess[0] || answer[3]==guess[0])
            white++;
        if (answer[0]==guess[1] || answer[2]==guess[1] || answer[3]==guess[1])
            white++;
        if (answer[0]==guess[2] || answer[1]==guess[2] || answer[3]==guess[2])
            white++;
        if (answer[0]==guess[3] || answer[1]==guess[3] || answer[2]==guess[3])
            white++;

        if (black==4)
            white=0;

        g2[1][4]=black;
        g2[1][5]=white;

        for (int n = 0;n < 6;n++)
            printf(" %d",g2[1][n]); 

        printf("\n");
    }
    while (answer[0]!=guess[0] || answer[1]!=guess[1] ||
           answer[2]!=guess[2] || answer[3]!=guess[3]);

    printf("BINGO!!!\n");

    return 0;
}

Update 2:

for (int slot=0;slot<4;slot++)
{
    if (guess[slot] == answer[slot]) 
        black++;
    else 
        for (int s=0;s<4;s++)
            if (s != slot) 
            {
                if (guess[slot] == answer[s]) 
                    white++;
                break;
            }
}

Update 3:

for (int x=0;x<4;x++)
flag[x]=0;

for (int slot = 0;slot < 4;slot++) 
{ 
    if (guess[slot] == answer[slot])  
        if (flag[slot]==1)
            black++; 
    else  
        for (int s=0;s < 4;s++) 
            if (s != slot)  
            { 
                if (guess[slot] == answer[s])
                    if (flag[s]==1)
                    {
                        white++; 
                        break; 
                    }
            } 
} 

Update 4

for (int x=0;x<4;x++)
flag[x]=0;

for (int slot = 0;slot < 4;slot++) 
{ 
    if (guess[slot] == answer[slot])  {
        black++; 
        flag[slot]=1;
    }
else  
    for (int s=0;s < 4;s++)
        if (s != slot)
        { 
            if (guess[slot] == answer[s])  
            {
                white++; 
                flag[s]=1;
                break; 
            }
        } 
} 

Update 5:

#include "stdafx.h"
#include "stdlib.h" 
#include "time.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int answer[4];
    int guess[4];
    int flag[4];
    int count = 0;

    srand(time(NULL)); 
    /*answer[0] = (rand() % 6)+1;
    answer[1] = (rand() % 6)+1;
    answer[2] = (rand() % 6)+1;
    answer[3] = (rand() % 6)+1;*/

    answer[0] = 1;
    answer[1] = 2;
    answer[2] = 3;
    answer[3] = 4;

    printf("%d %d %d %d\n", answer[0],  answer[1],  answer[2],  answer[3]); 

    do
    {
        int black = 0;
        int white = 0;
        count++;
        printf("Enter 4 numbers, this is your %d guess: ", count);
        scanf("%d %d %d %d", &guess[0], &guess[1], &guess[2], &guess[3]);

        int g2[2][6];

        for (int a = 0;a < 4;a++)
            g2[0][a]=answer[a];

        for (int i = 0;i < 4;i++)
            g2[1][i]=guess[i];

        for (int x=0;x<4;x++)
            flag[x]=0;

        for (int slot = 0;slot < 4;slot++) 
        { 
            if (guess[slot] == answer[slot])  
                black++; 
            else   
                for (int s=0;s < 4;s++)  
                    if (s != slot && guess[slot] == answer[s] && !flag[s]) 
                    {  
                        white++;  
                        flag[s]=1; 
                        break;  
                    }
        } 

        g2[1][4]=black;
        g2[1][5]=white;

        printf("Guess %d: ", count);

        for (int n = 0;n < 4;n++){
            printf(" %d",g2[1][n]);
        }

        printf(" Black: %d White: %d\n", g2[1][4], g2[1][5]);
        printf("\n");
    }
    while (answer[0]!=guess[0] || answer[1]!=guess[1] ||
           answer[2]!=guess[2] || answer[3]!=guess[3]);

    printf("BINGO!!!\n");

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:18:05+00:00Added an answer on May 13, 2026 at 8:18 am

    Suggested alternative (pseudocode):

    white = 0;
    black = 0;
    for slot=0 to 3
      if guess[slot] == answer[slot]
        black++
      else
        for s=0 to 3
          if s != slot
            if guess[slot] == answer[s]
              white++
              break
    

    Update (detail)

     else  
        for (int s=0;s < 4;s++) 
          if (s != slot && guess[slot] == answer[s] && !flag[s])
          { 
              white++; 
              flag[s]=1;
               break; 
          }
    

    I think this should do it: We’re checking for another slot that’s not the current one, one that’s not a perfect (black) match, and that hasn’t been used before to flag a black or white.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an object on a plain white background, does anybody know if OpenCV provides
Please let me know why there is lot of white space below the page
While cruising through my white book the other day, I noticed in the list
When is white space not important in Python? It seems to be ignored inside
Suppose you have the following string: white sand, tall waves, warm sun It's easy
I am doing some research on Unicode for a white-paper I am writing. Does
What are the pros/cons of using black-box reuse over white-box reuse?
How does one go about converting an image to black and white in PHP?
I need to display a string which has a white space on a asp.net
Is there an easy way of using the RegularExpressionValidator control while ignoring white space?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.