Question

In C language The project should create a code maker. The computer version of this game...

In C language

The project should create a code maker. The computer version of this game uses digits 0-9 for the code. The feedback may be an array of characters using ‘b’ for black and ‘w’ for white.

1. The program should generate a code

i. First, generate random number from 0 to 9. Actually, generate 4 random numbers

ii. If any random number is repeated, replace the repeated number iii. Loop step ii until there is no repetition

2. The program should accept and interpret user input

i. Scan in user input as a number

ii. Separate the digits of the input by position, i.e. units, tens, etc

iii. Stick each digit into an array

3. The program should compare the input with the generated code

i. Check each member of the input array with code array

ii. If input array element matches EXACT member of output array, set a black (‘b’) in feedback array

iii. If input array element matches ANY member of output array, set a white (‘w’) in feedback array

4. The program should give feedback

i. Output the feedback array element by element in the same line

5. Exit conditions i. If all feedback array elements are ‘b’, exit success

ii. If max tries exceeded, exit failure

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following code satisfies all the requirements: Thumbs Up please if this helps :)
#include <stdio.h>
#include <stdlib.h>
#include<time.h>

static char feedback[4]={'_','_','_','_'};
int randomNum()
{
return (rand()%9);

}
int main()
{
int i=0,j,rem=0,temp;
int flag=20;
int input[4]={0,0,0,0},output[4],n;
srand(time(0));
while(flag>0)
{
input[0] = randomNum();
input[1] = randomNum();
while(input[0] == input[1])
{
input[1] = randomNum();
}
input[2] = randomNum();
while(input[0] == input[2] || input[1] == input[2])
{
input[2] = randomNum();
}
input[3] = randomNum();
while(input[0] == input[3] || input[1] == input[3] || input[2] == input[3])
{
input[3] = randomNum();
}
printf("Enter a 4 digit Number : ");
scanf("%d",&n);
temp = n;
  
for(i=0;i<4;i++)
{
rem= temp%10;
output[i] = rem;
temp/=10;
}
  
//checking
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(input[i]==output[j])
{
if(i != j && feedback[i] != 'b')
{
feedback[i] = 'w';   
}
else
{
feedback[i] = 'b';
}
}
  
}
}
//giving feedback
for(i=0;i<4;i++)
{
printf(" %c ",feedback[i]);
}
  
printf("\n");
if(feedback[0] == 'b' &&
feedback[1] == 'b'&&
feedback[2] == 'b'&&
feedback[3] == 'b')
{
exit(0);
}
flag--;
}
printf("FAILURE");
}

OUTPUT:

test case 1 :

test Case 2 :

Add a comment
Know the answer?
Add Answer to:
In C language The project should create a code maker. The computer version of this game...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program...

    Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program should have an array of 5 integers named lottery and should generate a random number in the range of 1 through 99 for each element of the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive...

    Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive integer numbers from 1 to 999 (lottery numbers) and takes a single input from the user and checks the input against the 15 lottery numbers. The user wins if her/his input matches one of the lottery numbers. Your implementation must have a level of modularity (using functions) Functions you need to implement: Define function initialize() that takes array lotterNumbers[] as a parameter and assigns...

  • Any programming language may be used. Please indicate what language you have chosen. You are an...

    Any programming language may be used. Please indicate what language you have chosen. You are an analytics developer, and you need to write the searching algorithm to find the element. Your program should perform the following: Implement the Binary Search function. Write a random number generator that creates 1,000 elements, and store them in the array. Write a random number generator that generates a single element called searched value. Pass the searched value and array into the Binary Search function....

  • Follow instructions to create a visual C++ project, add the CH code, compile, fun and fix...

    Follow instructions to create a visual C++ project, add the CH code, compile, fun and fix errors, Write your Name, course, and date as comments on first line. Write Program's Name epp (Give it your last name with the assignment number as a name... Program0 LAST NAME). Explain what the program is to do along with any directions or instructions needed for program users (ie the purpose of each library included next to #include directive, names and uses of identifiers...etc)....

  • X86 Assembly language lab: TITLE Lab 3: assembly language fundamentals               ;;;;; Q1: Don't...

    X86 Assembly language lab: TITLE Lab 3: assembly language fundamentals               ;;;;; Q1: Don't forget to document your program            ; Name:Yuyan Wang ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;; Answer each question below by writing code at the APPROPRIATE places in the file. ;;;;; Hint: the appropriate place is not always right below the question. ;;;;; Q2: Write the directive to bring in the IO library           ;;;;; Q3: Create a constant called MAX and initialize it to 150...

  • Has to be written in C++. Visual studio. Write a C++ program to realize the game...

    Has to be written in C++. Visual studio. Write a C++ program to realize the game of guessing the number. Generally, the game will generate a random number and the player has to find out the number. In each guess, the program will give you a feedback as your guess is correct, too large, or too small. Then the player play repetitively until find out the number. Specifically, the game plays as the following. a). When the game is started,...

  • 1. Write a C code that do the following: Generate array of random integer numbers of...

    1. Write a C code that do the following: Generate array of random integer numbers of size 25 Fill the array with random integers ranging from [1-100] Find the maximum number of the array and its location Find the minimum number of the array and its location Search for a number in the array Print the array Flip the array Sort this array Quit the program when the user choose to exit

  • use c++ language, keep it simple i am using code block Exercise#2: Arrays with Random Numbers...

    use c++ language, keep it simple i am using code block Exercise#2: Arrays with Random Numbers Write a program that generates n random numbers as follows: Asks the user to input a positive integer n Asks the user to input the maximum value (say m) for the integers to be generated. Write a program that generates the n numbers in the range 0 to m. The program stores the generated numbers in an array A[ Asks the user to enter...

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT