Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program uses C specifically. Only C code works, not C++. In this variation

ID: 3671457 • Letter: T

Question

This program uses C specifically. Only C code works, not C++.

In this variation of Bulls and Cows, here are the rules:

The user will decide how many digits are in the code (between 2 and 6).

No digit is used twice in the code.

You will guess the code.

The computer will give you feedback by saying how many bulls there were and how many cows there were. The number of bulls is the number of correct digits in the wrong position. The number of cows is the number of correct digits in the correct location.

So, for example, if the code was 1234, and the guess was 1324, the feedback would be “2 bulls, 2 cows.” 1 and 4 were in the right spots—those are the cows. 3 and 2 were in the wrong spots—those are the bulls.

2

12

3

123

4

1234

5

12345

6

123456

For this program, you can hard code in the secret code. The table shows the codes you should use for the number of digits.

Your program should do the following:

Prompt the player for the number of digits that should be in the secret code. (2 – 6) Check that they entered a number between 2 and 6. Use the table from above to determine the secret code. For example, if the player chooses 5 the secret code will be 12345.

Prompt the player for a guess. Make sure they entered the correct number of digits for their guess.

For this assignment you can assume the following:

               When the user enters a guess they enter numbers between 1 and 9. Example: 1029 not valid

               When the user enters a guess they do not duplicate any of the digits. Example: 1111 not valid


Check to see if the player guessed the code correct:
        Look at their guess digit by digit. (Hint: use % and / with repetition to look at it starting with the ones digit?)
        For each digit:
                 If it matches the digit at that spot in the code (its place number matches the digit), add one to the cows count.
                 If it matches any other digit in the code, add one to the bulls count.
If they got it right (all cows!), tell them they won and continue the game.
If they didn’t get it right, give the player feedback for that guess, and let them guess again, continuing the guess/feedback process until they get it right.

Example Input & Output

How many digits do you want the code to be? 4

Guess the code: 4321

        Nope! 0 cows, 4 bulls.

Guess the code: 3421

        Nope! 0 cows, 4 bulls.

Guess the code: 6789

        Nope! 0 cows, 0 bulls.

Guess the code: 1324

        Nope! 2 cows, 2 bulls.

Guess the code: 4231

        Nope! 2 cows, 2 bulls.

Guess the code: 1234

You won!

How many digits do you want the code to be? 3

Guess the code: 987

        Nope! 0 cows, 0 bulls.

Guess the code: 543

        Nope! 1 cows, 0 bulls.

Guess the code: 231

        Nope! 0 cows, 3 bulls.

Guess the code: 512

        Nope! 0 cows, 2 bulls.

Guess the code: 123

You won!

2

12

3

123

4

1234

5

12345

6

123456

Explanation / Answer


#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main(void){
  
    //Use time so the random integers will be different every time
    srand(time(0));
  
    //Define all the integers that will be used
    int codeDigits = 0;
    int array[6];
    int a = 0;
    int b = 0;
    int c = 0;
    int guessOne, guessTwo, guessThree, guessFour, guessFive, guessSix;
    int bulls = 0;
    int cows = 0;
    int code = 0;
  
  
    //Assign random integers between 0 and 9 to each spot in the array
    for (a = 0; a < 6; a++){
        array[a] = rand() % 10;
    }
  
    //While any integer equals any other integer within the array....
    while(array[0] == array[1] || array[0] == array[2] || array[0] == array[3] || array[0] == array[4] || array[0] == array[5] ||
          array[1] == array[2] || array[1] == array[3] || array[1] == array[4] || array[1] == array[5] ||
          array[2] == array[3] || array[2] == array[4] || array[2] == array[5] ||
          array[3] == array[4] || array[3] == array[5] ||
          array[4] == array[5]){
      
        //Assign new random integers to every spot
        for (b = 0; b < 6; b++){
            array[b] = rand() % 10;
        }
        b = 0;
    }
  
    //Ask the user to enter how long the code will be
    printf("How many digits(between two and six) are in the code? ");
    scanf("%d",&codeDigits);
  
    //If the code is not between two and six digits, inform the user and end the program
    if (codeDigits < 2||codeDigits > 6){
        printf("You did not enter an integer between two and six digits. Please try again. ");
    }
  
    //If the code is two digits long, use this loop
    if (codeDigits == 2){
        while(cows < 2){
          
            cows = 0;
            bulls = 0;
          
            //Have the user guess the code
            printf("Guess the code:");
            scanf("%d", &code);
          
            //Break down the user's entered code into individual digits and assign these to the proper variable
            guessTwo = code % 10;
            guessOne = ((code - guessTwo)/10) % 10;
          
            //Assign cows and bulls
            if(guessOne == array[0]){
                cows = cows + 1;
            }
            if(guessTwo == array[1]){
                cows = cows + 1;
            }
            if(guessTwo == array[0]){
                bulls = bulls + 1;
            }
            if(guessOne == array[1]){
                bulls = bulls + 1;
            }
          
            //If the user did not enter the correct code, tell them how many cows and bulls they entered
            if(cows < 2){
                printf("    Nope! %d Cows, %d Bulls ", cows, bulls);
            }
          
            //If the user enters zero, print the code for them
            if(guessOne == 0 || guessTwo == 0){
                printf("Because you entered zero... ");
                while(c<6){
                    printf("%d", array[c]);
                    c++;
                }
                printf(" ");
            }
        }
        //Once they have all cows, inform the user that they have won
        printf("You Won! ");
    }
  
    //If the code is three digits long, use this loop
    if (codeDigits == 3){
        while(cows < 3){
          
            cows = 0;
            bulls = 0;
          
            //Have the user guess the code
            printf("Guess the code:");
            scanf("%d", &code);
          
            //Break down the user's entered code into individual digits and assign these to the proper variable
            guessThree = code % 10;
            guessTwo = ((code - guessThree)/10) % 10;
            code = ((code - guessThree)/10);
            guessOne = ((code - guessTwo)/10) % 10;
          
            //Assign cows and bulls
            if(guessOne == array[0]){
                cows = cows + 1;
            }
            if(guessTwo == array[1]){
                cows = cows + 1;
            }
            if(guessThree == array[2]){
                cows = cows + 1;
            }
            if(guessTwo == array[0] || guessThree == array[0]){
                bulls = bulls + 1;
            }
            if(guessOne == array[1] || guessThree == array[1]){
                bulls = bulls + 1;
            }
            if(guessOne == array[2] || guessTwo == array[2]){
                bulls = bulls +1;
            }
          
            //If the user did not enter the correct code, tell them how many cows and bulls they entered
            if(cows < 3){
                printf("    Nope! %d Cows, %d Bulls ", cows, bulls);
            }
          
            //If the user enters zero, print the code for them
            if(guessOne == 0 || guessTwo == 0 || guessThree == 0){
                printf("Because you entered zero... ");
                while(c<6){
                    printf("%d", array[c]);
                    c++;
                }
                printf(" ");

            }
        }
      
        //Once they have all cows, inform the user that they have won
        printf("You Won! ");
    }
  
    //If the code is four digits long, use this loop
    if (codeDigits == 4){
        while(cows < 4){
          
            cows = 0;
            bulls = 0;
          
            //Have the user guess the code
            printf("Guess the code:");
            scanf("%d", &code);
          
            //Break down the user's entered code into individual digits and assign these to the proper variable
            guessFour = code % 10;
            guessThree = ((code - guessFour)/10) % 10;
            code = ((code - guessFour)/10);
            guessTwo = ((code - guessThree)/10) % 10;
            code = ((code - guessThree)/10);
            guessOne = ((code - guessTwo)/10) % 10;

          
            //Assign cows and bulls
            if(guessOne == array[0]){
                cows = cows + 1;
            }
            if(guessTwo == array[1]){
                cows = cows + 1;
            }
            if(guessThree == array[2]){
                cows = cows + 1;
            }
            if(guessFour == array[3]) {
                cows = cows + 1;
            }
            if(guessTwo == array[0] || guessThree == array[0] || guessFour == array[0]){
                bulls = bulls + 1;
            }
            if(guessOne == array[1] || guessThree == array[1] || guessFour == array[1]){
                bulls = bulls + 1;
            }
            if(guessOne == array[2] || guessTwo == array[2] || guessFour == array[2]){
                bulls = bulls + 1;
            }
            if(guessOne == array[3] || guessTwo == array[3] || guessThree == array[3]){
                bulls = bulls + 1;
            }
          
            //If the user did not enter the correct code, tell them how many cows and bulls they entered
            if(cows < 4){
                printf("    Nope! %d Cows, %d Bulls ", cows, bulls);
            }
          
            //If the user enters zero, print the code for them
            if(guessOne == 0 || guessTwo == 0 || guessThree == 0 || guessFour == 0){
                printf("Because you entered zero... ");
                while(c<6){
                    printf("%d", array[c]);
                    c++;
                }
                printf(" ");
            }
        }
      
        //Once they have all cows, inform the user that they have won
        printf("You Won! ");
    }
  
    //If the code is five digits long, use this loop
    if (codeDigits == 5){
        while(cows < 5){
          
            cows = 0;
            bulls = 0;
          
            //Have the user guess the code
            printf("Guess the code:");
            scanf("%d", &code);
          
            //Break down the user's entered code into individual digits and assign these to the proper variable
            guessFive = code % 10;
            guessFour = ((code - guessFive)/10) % 10;
            code = ((code - guessFive)/10);
            guessThree = ((code - guessFour)/10) % 10;
            code = ((code - guessFour)/10);
            guessTwo = ((code - guessThree)/10) % 10;
            code = ((code - guessThree)/10);
            guessOne = ((code - guessTwo)/10) % 10;
          
            //Assign cows and bulls
            if(guessOne == array[0]){
                cows = cows + 1;
            }
            if(guessTwo == array[1]){
                cows = cows + 1;
            }
            if(guessThree == array[2]){
                cows = cows + 1;
            }
            if(guessFour == array[3]){
                cows = cows + 1;
            }
            if(guessFive == array[4]){
                cows = cows + 1;
            }
            if(guessTwo == array[0] || guessThree == array[0] || guessFour == array[0] || guessFive == array[0]){
                bulls = bulls + 1;
            }
            if(guessOne == array[1] || guessThree == array[1] || guessFour == array[1] || guessFive == array[1]){
                bulls = bulls + 1;
            }
            if(guessOne == array[2] || guessTwo == array[2] || guessFour == array[2] || guessFive == array[2]){
                bulls = bulls + 1;
            }
            if(guessOne == array[3] || guessTwo == array[3] || guessThree == array[3] || guessFive == array[3]){
                bulls = bulls + 1;
            }
            if(guessOne == array[4] || guessTwo == array[4] || guessThree == array[4] || guessFour == array[4]){
                bulls = bulls + 1;
            }
          
            //If the user did not enter the correct code, tell them how many cows and bulls they entered
            if(cows < 5){
                printf("    Nope! %d Cows, %d Bulls ", cows, bulls);
            }
          
            //If the user enters zero, print the code for them
            if(guessOne == 0 || guessTwo == 0 || guessThree == 0 || guessFour == 0 || guessFive == 0){
                printf("Because you entered zero... ");
                while(c<6){
                    printf("%d", array[c]);
                    c++;
                }
                printf(" ");
            }
        }
      
        //Once they have all cows, inform the user that they have won
        printf("You Won! ");
    }
  
    //If the code is six digits long, use this loop
    if (codeDigits==6){
        while(cows<6){
          
            cows = 0;
            bulls = 0;
          
            //Have the user guess the code
            printf("Guess the code:");
            scanf("%d", &code);
          
            //Break down the user's entered code into individual digits and assign these to the proper variable
            guessSix = code % 10;
            guessFive = ((code - guessSix)/10) % 10;
            code = ((code - guessSix)/10);
            guessFour = ((code - guessFive)/10) % 10;
            code = ((code - guessFive)/10);
            guessThree = ((code - guessFour)/10) % 10;
            code = ((code - guessFour)/10);
            guessTwo = ((code - guessThree)/10) % 10;
            code = ((code - guessThree)/10);
            guessOne = ((code - guessTwo)/10) % 10;
          
          
            //Assign cows and bulls
            if(guessOne == array[0]){
                cows = cows + 1;
            }
            if(guessTwo == array[1]){
                cows = cows + 1;
            }
            if(guessThree == array[2]){
                cows = cows + 1;
            }
            if(guessFour == array[3]){
                cows = cows + 1;
            }
            if(guessFive == array[4]){
                cows = cows + 1;
            }
            if(guessSix == array[5]){
                cows = cows + 1;
            }
            if(guessTwo == array[0] || guessThree == array[0] || guessFour == array[0] || guessFive == array[0] || guessSix == array[0]){
                bulls = bulls + 1;
            }
            if(guessOne == array[1] || guessThree == array[1] || guessFour == array[1] || guessFive == array[1] || guessSix == array[1]){
                bulls = bulls + 1;
            }
            if(guessOne == array[2] || guessTwo == array[2] || guessFour == array[2] || guessFive == array[2] || guessSix == array[2]){
                bulls = bulls +1;
            }
            if(guessOne == array[3] || guessTwo == array[3] || guessThree == array[3] || guessFive == array[3] || guessSix == array[3]){
                bulls = bulls + 1;
            }
            if(guessOne == array[4] || guessTwo == array[4] || guessThree == array[4] || guessFour == array[4] || guessSix ==array[4]){
                bulls = bulls + 1;
            }
            if(guessOne == array[5] || guessTwo == array[5] || guessThree == array[5] || guessFour == array[5] || guessFive == array[5]){
                bulls = bulls +1;
            }
          
            //If the user did not enter the correct code, tell them how many cows and bulls they entered
            if(cows < 6){
                printf("    Nope! %d Cows, %d Bulls ", cows, bulls);
            }
          
            //If the user enters zero, print the code for them
            if(guessOne == 0 || guessTwo == 0 || guessThree == 0 || guessFour == 0 || guessFive == 0 || guessSix == 0){
                printf("Because you entered zero... ");
                while(c<6){
                    printf("%d", array[c]);
                    c++;
                }
                printf(" ");
            }
        }
      
        //Once they have all cows, inform the user that they have won
        printf("You Won! ");
    }
}

output

How many digits(between two and six) are in the code?                                                                                                       
3                                                                                                                                                           
Guess the code:987                                                                                                                                          
    Nope! 0 Cows, 1 Bulls                                                                                                                                   
Guess the code:543                                                                                                                                          
    Nope! 0 Cows, 0 Bulls                                                                                                                                   
Guess the code:123                                                                                                                                          
    Nope! 0 Cows, 1 Bulls                                                                                                                                   
Guess the code:                                                                                                                                             
231                                                                                                                                                         
    Nope! 0 Cows, 1 Bulls                                                                                                                                   
Guess the code:                                                                                                                                             

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote