Answer to Question #193853 in C++ for Shubo

Question #193853

Let us consider a guessing game in which the computer picks a random two- digit number in the range from 0 to 99 and asks the user to guess the number. After each guess, the computer lets the player know how many digits of guessed numbers match the correct number. If the guessed number matches the correct number, the computer lets the player know the number of guesses the player made.

Questiona1: If the player enters an incorrect guess, the computer lets the player know how many digits from the guess are also in the correct number. The order of digits doesn’t affect the number of digits that match. For example, if the correct number is 21 and the player guessed 1, the computer reports one digit because the player’s guess contains 1. If the guess contains two digits that match, the computer reports two digits. The user still needs to come up with the correct order of those digits to arrive at the correct number.


1
Expert's answer
2021-05-17T03:35:19-0400
#include <iostream>
#include<time.h>
int main()
{
    srand(time(NULL));
    int num= rand() % 100;
    int u_num;
    int attempts{ 0 };
    int digits{ 0 };
    do
    {
        digits = 0;
        std::cout << "Enter your number: ";
        std::cin >> u_num;
        if (u_num / 10 == num / 10 || u_num / 10 == num % 10) {
            digits += 1;
        }
        if (u_num / 10 != u_num % 10 && u_num % 10 == num / 10 || u_num / 10 != u_num % 10 && u_num % 10 == num % 10) {
            digits += 1;
        }
        std::cout << "Guess the numbers: " << digits << std::endl;
        attempts += 1;
    } while (u_num!=num);
    std::cout << "The number of attempts to guess the number was: " << attempts;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog