HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Guess three random numbers in order

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
threerandomordernumbersguess

Problem

We were given a homework assignment to create a C++ program that generates 3 different random numbers each time and the user is supposed to guess the three of them AND in order. I already achieved that, however I'm not very satisfied with how long my code is, especially with the fact that I had so many if statements. We were told at the beginning of the year that good programming is the ability to write less but more efficiently.
(We haven't learned anything about arrays or vectors yet) but I'm still curious to know what are other ways to compare the 3 generated numbers and the 3 user input numbers, all while keeping the order criterion in mind)

PS. 3 guesses of 3 numbers in order -> User wins. If the users fails to guess the 3 numbers in their order within 10 attempts, they lose + I also kept the generated numbers to print out just for testing purposes.
ALSO, we were required to have 3 functions so that's why I didn't write everything in just one.

Long story short, is there any way to write the conditions more concisely and
efficiently instead of having to do a for statement for each and every possibility? I still haven't learned arrays since I'm just beginning but all suggestions and methods applicable in C++ are fascinatingly admired and highly appreciated, thank you very much!

Here's my code:

#include 
using namespace std;

#include 
#include 
#include 

void randnumbers(int&, int&, int&); // Random Numbers Prototype
void guessnumbers(int&, int&, int&); // Guess Numbers Prototype

int main()
{

    // --> Guesses of Numbers and their Orders
    int guesses = 0;

    for (int counter = 0; counter = 3)
        cout 6: ";
    cin >> nb1 >> nb2 >> nb3;

}

Solution

Here is an example routine to count a number of numbers appearing in two sets, implemented as vectors of ints. Please note the routine takes data by value (i.e. a copy) so that it can modify (sort) them without affecting the caller's data.

int CountSameNumbers(std::vector A, std::vector B)
{
    std::sort(A.begin(), A.begin() + 3);
    std::sort(B.begin(), B.begin() + 3);

    int count = 0;
    for(int i = 0, j = 0; i  B[j])
            j ++;
        else
            count ++, i ++, j ++;

    return count;
}


The routine sorts both vectors first, then starts scanning them with two pointers (actually, integer indices) i and j, wandering along the data. In every iteration one or both indices are incremented, and equal numers are counted.

And this is a simple loop to count 'exact hits' by comparing values at corresponding positions in both vectors. It does not modify data, that's why it can safely take them by a reference.

int CountSamePositions(std::vector &A, std::vector &B)
{
    int count = 0;
    for(int i = 0; i < 3; i++)
        if(A[i] == B[i])
            count ++;

    return count;
}


Once you have two vectors, one with your generated data and another one with data input from a user, you can test the user's answer with these two functions:

std::vector A, B;

    // fill one vector with your data
    A.push_back(a); A.push_back(b); A.push_back(c);
    // fill the other one with user's data
    B.push_back(d); B.push_back(e); B.push_back(f);

    int samepos = CountSamePositions(A, B);
    int samenum = CountSameNumbers(A, B);

    if(samepos == 3)
        std::cout  0)
        std::cout << "You guessed " << samenum << " numbers, "
                  << samepos << " of them at right positions.\n";
    else
        std::cout << "You failed to guess any number. :(\n";


You can see a working example in Ideone.

I just wanted to explain the data processing here, so I didn't implement the 'ten attempts' loop (with an early exit in 'You won' case), but I hope that will not be a big problem.

Code Snippets

int CountSameNumbers(std::vector<int> A, std::vector<int> B)
{
    std::sort(A.begin(), A.begin() + 3);
    std::sort(B.begin(), B.begin() + 3);

    int count = 0;
    for(int i = 0, j = 0; i < 3 && j < 3;)
        if(A[i] < B[j])
            i ++;
        else if(A[i] > B[j])
            j ++;
        else
            count ++, i ++, j ++;

    return count;
}
int CountSamePositions(std::vector<int> &A, std::vector<int> &B)
{
    int count = 0;
    for(int i = 0; i < 3; i++)
        if(A[i] == B[i])
            count ++;

    return count;
}
std::vector<int> A, B;

    // fill one vector with your data
    A.push_back(a); A.push_back(b); A.push_back(c);
    // fill the other one with user's data
    B.push_back(d); B.push_back(e); B.push_back(f);

    int samepos = CountSamePositions(A, B);
    int samenum = CountSameNumbers(A, B);

    if(samepos == 3)
        std::cout << "You won!\n";
    else if(samenum > 0)
        std::cout << "You guessed " << samenum << " numbers, "
                  << samepos << " of them at right positions.\n";
    else
        std::cout << "You failed to guess any number. :(\n";

Context

StackExchange Code Review Q#145951, answer score: 2

Revisions (0)

No revisions yet.