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

Use C++ Classes and Data Structures knowledge to write a Guess Numbers Game Prog

ID: 3838861 • Letter: U

Question

Use C++ Classes and Data Structures knowledge to write a Guess Numbers Game Program!

Requirement: You should let system randomly create a 4 digits(no repeat) number(ex: 1234), and then ask user to guess a 4 digit number. If user's number has one digit that match system's digit but not match the position, you output "B" to the screen, and if user's number has one digit that match system's digit and position as well then you output "A" to the screen. And you are going to ask user to continue to guess until geting the right answer or they choose to quit!

For example:

system's randomly number---1234, user's first guess input---2567

*user's number has a digit "2" match system's 2 but does not match the position, so output "B" to the screen.

user's second guess input---2537

*user's number still has a digit "2" match system's 2 but does not match the position, and has a digit "3" match both number and position, so output "A B" to the screen this time.

user's third guess input---5236

*user's number has a digit "2" and a digit "3" match both number and position, so ouput "A A" to the screen.

Recommd knowledge:

Structs and class, Class Templates, Pointers and Dynamic Array, Stack and Queus, Link list, Hash Table, Trees, Heaps and so on.

If you have any question that you do not understand the problem please left me a comment and I will reply you as soon as possible!

Thank you!

Explanation / Answer

#include <cstdlib>
#include <time.h>
#include <iostream>

using namespace std;

int main() {
srand(time(0));
int number;
int randomChoice[3];
int guessArray[3];
int temp,i,j;
int k=0,l=0;
string dontMatch="B";
string match="A";
  

number = rand() % 1000 + 1;
int guess;
cout<<number<<" ";


while (number > 0)
{
int digit = number%10;
number /= 10;
randomChoice[k]=digit;
k++;
}
for(i=0,j=3-1;i<3/2;i++,j--)
   {
       temp=randomChoice[i];
       randomChoice[i]=randomChoice[j];
       randomChoice[j]=temp;
   }
  
do {
cout << " Enter your estimate: ";
cin >> guess;
while (guess > 0)
{
int digit = guess%10;
guess /= 10;
guessArray[l]=digit;
l++;
}
for(i=0,j=3-1;i<3/2;i++,j--)
   {
       temp=guessArray[i];
       guessArray[i]=guessArray[j];
       guessArray[j]=temp;
   }
  


for(int n=0;n<3;n++)
{
for(int m=0;m<3;m++)
{
if(randomChoice[n]==guessArray[m]&&n==m)

{

cout<<match;
  
}
if(randomChoice[n]==guessArray[m]&&n!=m)
{
cout<<dontMatch;
  
}
}

}
  
} while (guess != number);
return 0;
}

Output:

330

Enter your estimate: 035

BA

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