Home > Back-end >  How do i make my array compare and then display certain outputs based on users guess
How do i make my array compare and then display certain outputs based on users guess

Time:11-27

Newer to coding and im stuck, Need to make a array thats stores 5 numbers (1-9), then i need to check that array for duplicates if there is duplicates i need to replace that number either with a whole new random line no duplicates (seems like the easier option) or just replace that one number, after that wants me to get users 5 numbers guess store that in a array, display that array at the bottom along with these under each of the numbers

// The * = means the number is in the exact location

// The - = means the array does not contain that number

// The = means the array contains the number but its not in the right location

All the arrays want the digits to be entered one at a time repeat steps till user gets all the numbers to * then end game with completion msg.

My true problem lies with step 4 and step 7; Below is the code ive been working on today but any help would be truly appreciated

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void game_instructions(int n);
bool search_array(int a[], int n, int t);
int main()
{

    //Step 1: Declare your variables and constants
    const int SIZE = 5; //this constant stores the length/size of the code

    int randcode[SIZE]; //this array stores the code generated randomly by the computer
    int guess[SIZE];    //this array stores the code inputted by the player/user
   
    //you may add more variables as needed
  
    //Step 2: Output game instructions by calling function game_instructions
    game_instructions(SIZE);
    //Step 3: Generate a random code and store it in the array randcode one digit at a time.
    //Each digit should be between 0 and 9 and should be stored as one array element
    //Recall that rand() % 10 can be used to generate a number between 0 and 9
    srand(time(0));
    for(int i=0;i<SIZE;i  )
       randcode[i]= (rand() % 10);  //Computers random 5 numbers generated
   
    cout<<"\nRandom C-numbers::"<<endl;
   
    for(int i=0;i<SIZE;i  )
       cout<<randcode[i] << " ";
    
    //Step 4: Repeat step 3 if the array randcode contains duplicates
    //You must use function contains_duplicates to implement this step
    
    
    
    //Step 5: Ask the user for his guess and store it in the array guess.
    //Read one digit at a time, validate it to make sure it is between 0 and 9, and store it as one array element
       
    for (int i=0; i<SIZE; i  ) {
        cout<<"\nEnter Digit "<< i 1 << ": ";
        cin >> guess[i];}
            
    cout << endl;
    //Step 6: Output the array guess on a single line with a space after each element (see the sample output provided)
    for (int n=0; n < SIZE;   n) {
        cout << guess[n] << "  ";
    }
    //Step 7: Compare the randomly generated code (randcode) with the user's guess (guess)
    //and display feedback for each digit as: *,   or –, as explained below:
    //For each digit in the user's guess do the following:
        // If it matches the digit from the random code (both value and position), output *
        // Otherwise, if it appears anywhere in the random code, output   (use function search_array here)
        // Otherwise, output -
        
    //Step 8: Repeat steps 5,6,7 until all digits have been guessed correctly
    
    //Step 9: Output congratulations message
    cout << endl << endl;
    cout << "Good job! You guessed the code!";
    return 0;
}

void game_instructions(int n)
//This function outputs the game instructions.
//Its parameter n represents the length of the code.
{
    cout << "A random " << n << " digit code has been generated. You have to guess it. \n";
    cout << "For every digit you will receive feedback in the form of *,   or -  \n";
    cout << "   * means the digit is in the code and it is in the correct position.\n";
    cout << "     means the digit is in the code but it is not in the correct position.\n";
    cout << "   - means the digit is not in the code.\n";
}


bool search_array(int a[], int n, int t)
//This function searches the array a (of size n) for a target value t.
//If t is found in the array the function returns true; otherwise it returns false.
{
    for (int i = 0; i < n; i  )
    {
        if (a[i] == t) return true;
    }
    return false;
}

bool contains_duplicates(int a[], int n)
//This function searches the array a (of size n) and returns true if the array contains duplicates; otherwise, it returns false.
{
    for (int i = 0; i < n; i  )
    {
        //compare element a[i] with all the remaining elements from index i 1 to n
        for (int j = i 1; j < n; j  )
        {
            if (a[i] == a[j]) return true;
        }
    }
    return false;
}

I got most the side code done but im just stumped on how to get this array to match any help would be nice

CodePudding user response:

Here is how I would implement the program to generate the number without duplicate:

Step3 would look like this:

int tempNumber;
int i = 0;
while (i < SIZE) {
    tempNumber = (rand() % 10);

    if (contains_duplicates(randcode, tempNumber, i) == false) {
        // every time there is no duplicate we push and add i by 1;
        randcode[i] = tempNumber;  // Computers random 5 numbers generated
        i  ;
    }
}

contains_duplicates function would look like this:

bool contains_duplicates(int arr[], int tempNum, int currentArrSize)
// This function searches the array a (of size n) and returns true if the 
//array
// contains duplicates; otherwise, it returns false.
{
    for (int j = 0; j <= currentArrSize; j  ) {
        // if the array[index] not equal generated number the loop will 
        //iterate again without going to the line below
        if (arr[j] != tempNum) {
            continue;
        }
        // if the array[index] equal the function will return true and end 
        //the function

        // uncomment this to check how many time it duplicate (not 
        //necessary tho) 
        //cout << "if return true, this will appear"<< endl;
        return true;
    }
    // if the loop is done and no duplicate, function will return false
    return false;
}

With this method it is not necessary to do step 4 because we already prevent duplicate array!

Here is the last problem from your question which is step 7:

cout << endl;
for (int i = 0; i < SIZE; i  ) {
    if (guess[i] == randcode[i]) {
        cout << "* ";
    } else if (search_array(randcode, SIZE, guess[i])) {
        cout << "  ";
    } else {
        cout << "- ";
    }
}

Seems like the problem has been solved, now try to implement step 8 by yourself, good luck :)

  • Related