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

!!!!!! IT HAS TO BE WRITTEN IN C++ PLEASE HELP !!!! Use at least one user define

ID: 3732889 • Letter: #

Question

!!!!!! IT HAS TO BE WRITTEN IN C++ PLEASE HELP !!!!

Use at least one user defined class in your program. Write a program to have the computer genera try to guess the number. Every time a guess is made, the computer responds with the number of red, yellow, and green digits in the guess. A guess digit is green if the guess digit and the corresponding digit are the same. A guess digit is red if it does not correspond to any of the digits. A yellow guess is if it is a correct digit but not in the correct place Suppose the computer's number is 123. Your output may look as follows Please enter a guess? 422 You have: 2 red 1 green 0 yellow Enter the next guess? 459 You have: 3 red 0 green Oyellow Enter the next guess? 122 You have: 1 red 2 green 0 yellow Enter the next guess? 123 VERIFIED BY: DATE:

Explanation / Answer


#include<bits/stdc++.h>

using namespace std;

int check(int guess, int ran)
{
   int g1, g2, g3, r1, r2, r3;

   g1 = guess%10;
   g2 = (guess/10)%10;
   g3 = (guess/100)%10;

   r1 = ran%10;
   r2 = (ran/10)%10;
   r3 = (ran/100)%10;

   int green = 0, red = 0, yellow = 0;

   if(g1==r1)
       green++;

   if(g2==r2)
       green++;

   if(g3==r3)
       green++;

   if(g1!=r1 && g1!=r2 && g1!=r3)
       red++;

   if(g2!=r1 && g2!=r2 && g2!=r3)
       red++;

   if(g3!=r1 && g3!=r2 && g3!=r3)
       red++;

   yellow = 3 - red - green;

   cout << "You have: ";
   cout << red << " red ";
   cout << green << " green ";
   cout << yellow << " yellow ";

   return 0;
}

int main()
{
   srand (time(NULL));
   int random = rand()%900 + 100;
   int guess;

   int num = 0;

   while(true)
   {
       if(num == 0)
       {
          cout << "Please enter a guess? ";
          num = 1;
        }
        else
        {
          cout << "Enter the next guess? ";
        }
       cin >> guess;

       if(guess == random)
       {
           cout << "CONGRATULATIONS, YOU ARE CORRECT!!!!!!! ";
           break;
       }
       else
       {
           check(guess, random);
       }
   }

   return 0;
}