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

45 points Grading Rubric for Programming Style 10 Meaningful identifier names 10

ID: 3753117 • Letter: 4

Question

45 points Grading Rubric for Programming Style

10 Meaningful identifier names

10 Comments: Has header. Comments on each block of code

10 Functional Decomposition

10 Appropriate data and control structures

5 Code Layout: Appropriate indentation and blank lines

#include

using namespace std;

//-------------------------------------------------------------------

int main()

{

cout << "Program: 2 MasterMind The program selects 3 distinct random digits 0..9." << endl;

cout << "On each turn you guess 3 digits. Program indicates how many are correct." << endl;

cout << "You have 10 moves to guess the number. Good luck!" << endl;

cout << endl;

cout << "Press 's' to set the three digits, or 'r' to randomize them: ";

return 0;

}

The program selects 3 distinct random digits 0..9. On each turn you guess 3 digits. The program indicates how many are correct. You have 10 moves to guess the number. Good luck! Press 's' to set the three digits, or 'r' to randomize them: r Input of 000 displays the hidden digits. Input of 999 exits the program, In place Out of place 1. Your guess: 012 2. Your guess: 345 3. Your guess: 637 4. Your guess: 783 5. Your guess: 793 You entered: 012 You entered: 345 You entered: 637 You entered: 783 You entered: 793 ***Congratulations! Exiting program...

Explanation / Answer

#include <iostream>
#include <stdlib.h>
using namespace std;

// Function to display menu, accept user choice, and return user choice
char menu()
{
// To store user choice
char ch;
// Displays menu
cout << "Program: 2 MasterMind The program selects 3 distinct random digits 0..9." << endl;
cout << "On each turn you guess 3 digits. Program indicates how many are correct." << endl;
cout << "You have 10 moves to guess the number. Good luck!" << endl;
cout << endl;
// Accepts user choice
cout << "Press 's' to set the three digits, or 'r' to randomize them: ";
cin>>ch;
// Returns user choice
return ch;
}// End of function

// Function to generate 3 random digit and stores it in parameter array
void randomDigit(int randomDigitArray[])
{
// loops 3 tiles
for(int x = 0; x < 3; x++)
// Generates random number between 0 - 9 and stores it in x index position
randomDigitArray[x] = rand() % 9 + 0;
}// End of function

// Function to return number of random digits matches with user entered digits
int compareDigit(int randomDigitArray[], int userDigitArray[])
{
// Initializes the counter to 0
int countMatch = 0;

// Loops 3 times for random digits
for(int x = 0; x < 3; x++)
{
// Loops 3 times for user entered digits
for(int y = 0; y < 3; y++)
{
// Checks if the x index position of the random array
// with y index position of the user array are equal
if(randomDigitArray[x] == userDigitArray[y])
// If equal increase the counter by one
countMatch++;
}// End of inner for loop
}// End of outer for loop
// Returns the match counter
return countMatch;
}// End of function

// Function to convert a number to digit
void findDigit(int no, int userDigitArray[])
{
// Sets the initial index position to 2
int index = 2;
// Loops till number is not equals to zero
while(no != 0)
{
// Calculates the remainder of the number and stores it at index position of the array
// Decrease the index by one
userDigitArray[index--] = no % 10;
// Stores the quotient
no /= 10;
}// End of while loop
}// End of function

// Function to play the game
void playGame()
{
// Declares an array to store randomly generated digits
int randomDigitArray[3];
// Declares an array to store user entered digits
int userDigitArray[3];
// To store the number entered by the user
int no;
// To store the number of digits matches
int counterCorrect = 0;
// To store user choice either 's' or 'r'
char choice;

// Calls the function to accept user choice
// Stores the return choice
choice = menu();
cout<<" Input of 000 displays the hidden digits. Input of 999 exits the program.";

// Calls the function to generate 3 random digits
randomDigit(randomDigitArray);
// Displays heading
cout<<" In place Out of place";
cout<<" --------- --------- ";

// Loops 10 times for 10 chances
for(int x = 1; x <= 10; x++)
{
// Loops valid digit entered by the user
do
{
// Accepts a number from the user
cout<<x<<". Your guess: ";
cin>>no;
// Checks for 3 digit number
if(no >= 100 && no <= 999)
break;
// Otherwise displays error message
else
cout<<" Invalid digits. Try again. ";
}while(1);// End of do - while loop

// Calls the function to convert the number to digits
findDigit(no, userDigitArray);

// Calls the function to check how many digits of random digit and user digit matches
// Stores the return count
counterCorrect = compareDigit(randomDigitArray, userDigitArray);

// Checks if user entered digits are 999 then stop the program
if(userDigitArray[0] == 9 && userDigitArray[1] == 9 && userDigitArray[2] == 9)
{
cout<<" Exit Program...";
exit(0);
}// End of if condition

// Displays the digits entered by the user
cout<<" You entered: "<<no;
// Checks if the number of digits match is greater than or equals to 1, then display in place value
if(counterCorrect >= 1)
cout<<" "<<counterCorrect<<" 0 ";
// Otherwise no digit match, display out of place value
else
cout<<" 0"<<" "<<3 - counterCorrect<<endl;

// Checks if the number of digit match is 3, display win and stop the program
if(counterCorrect == 3)
{
cout<<" *** Congratulations! *** ";
cout<<" Exit Program...";
exit(0);
}// End of if condition

// Otherwise
else
{
// Checks if user choice is 'r' or 'R'
if(choice == 'r'|| choice == 'R')
// Call the function to generate new 3 random digits
randomDigit(randomDigitArray);
}// End of else
}// End of for loop
cout<<" Better luck next time.";
cout<<" Exit Program...";
}// End of function

// main function definition
int main()
{
// Calls the function to start the game
playGame();
return 0;
}// End of main function

Sample Output1:

Program: 2 MasterMind The program selects 3 distinct random digits 0..9.
On each turn you guess 3 digits. Program indicates how many are correct.
You have 10 moves to guess the number. Good luck!

Press 's' to set the three digits, or 'r' to randomize them: s

Input of 000 displays the hidden digits. Input of 999 exits the program.

In place Out of place
--------- ---------
1. Your guess: 125

You entered: 125 1 0
2. Your guess: 583

You entered: 583 2 0
3. Your guess: 785

You entered: 785 3 0

*** Congratulations! ***
Exit Program...

Sample Output2:

Program: 2 MasterMind The program selects 3 distinct random digits 0..9.
On each turn you guess 3 digits. Program indicates how many are correct.
You have 10 moves to guess the number. Good luck!

Press 's' to set the three digits, or 'r' to randomize them: r

Input of 000 displays the hidden digits. Input of 999 exits the program.

In place Out of place
--------- ---------
1. Your guess: 124

You entered: 124 0 3

2. Your guess: 214

You entered: 214 2 0

3. Your guess: 589

You entered: 589 0 3

4. Your guess: 348

You entered: 348 1 0

5. Your guess: 891

You entered: 891 0 3

6. Your guess: 692

You entered: 692 0 3

7. Your guess: 123

You entered: 123 1 0

8. Your guess: 893

You entered: 893 0 3

9. Your guess: 471

You entered: 471 2 0

10. Your guess: 324

You entered: 324 0 3

Better luck next time.
Exit Program...

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