1

I have a slight problem in finishing my code. My task is to create a program that creates a randomly generated array of size 10, with any number between 1-50. All of these numbers must be unique. The user is asked to input a number, and then the program should decipher wether the users input matches any of the randomly generated numbers within the array. I cannot use any fancy functions that come from a library, I must construct my own. Here is my code so far. I am having trouble using the check function inside of main. I am not sure if this is just because of my lack of knowledge (probably) or if I just can't do it when the parameter is an array. Any help is appreciated.

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>

using std::cin; using std::cout; using std::endl;

int check(int fillFunc[]){
    while(true){
        int val = 1 + rand() % 50; //assume it's unique
        bool unique = true;
        for (int i = 0; i < 10; ++i){  //if another number matches, it isn't unique, choose again
            if (fillFunc[i] == val){
                unique = false;
                break;
            }
        }
        //if it is unique, return it.
        if (unique){
            return val;
        }
    }

}

void draw(int fillFunc[]){
    for (int i = 0; i < 10; i++){
        fillFunc[i] = check(fillFunc);
    }
}

void printOut(int fillFunc[]){
    for (int i = 0; i < 10; i++){
        cout << " " << fillFunc[i];
    }
    cout << "\n";
}

int main(){


    srand((unsigned)time(NULL));

    const int arraySize = 10;
    int win[arraySize] = {};

    cout << "Please enter a number: ";
    int guess;
    cin >> guess;
    cout << "\n";

    draw(win);
            cout << "Congrats! Your number matches one from the lottery!";


    cout << "Your lottery numbers are: ";
    printOut(win);
    cout << "\n";

}
Breinz
  • 31
  • 4
  • Have you tried taking pointers instead of arrays? – lorro Oct 11 '17 at 06:38
  • 1
    What is "fancy functions from libraries"? You are using the standard library and it does have such facilities – Passer By Oct 11 '17 at 06:44
  • I kinda messed around with using the operator but I don't fully understand how to use them outside of a pre determined value set to a variable – Breinz Oct 11 '17 at 06:45
  • @PasserBy when i say "fancy functions" i mean things like random_shuffle, which i could use for my random number generator in my lottery array, but I am not allowed to. I meant more or less that i need to create the function myself using something like my check function. – Breinz Oct 11 '17 at 06:47
  • @lorro my problem is is that I don't know how to go from generating my random array to taking it into main and checking it against the users input. – Breinz Oct 11 '17 at 07:11
  • @Breinz: what I mean, void draw(int* fillFunc); , etc. – lorro Oct 11 '17 at 07:14
  • @lorro I am confused on what that changes, could you elaborate? – Breinz Oct 11 '17 at 07:19
  • @Breinz it's unrelated. See https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c Here, unrelated: http://coliru.stacked-crooked.com/a/d85338480ca357e8 – sehe Oct 11 '17 at 07:22
  • @sehe Thanks! In your example you made me realize that I wasn't using my win array for anything other than storing the values, and since it is in main that is the one I should have been using this entire time instead of trying to invoke the check function into main. Thanks again for opening my eyes to my idiotic mistake. – Breinz Oct 11 '17 at 07:32

2 Answers2

1

Re. Your answer

Not sure I taught you "the right thing" now then. I'd personally write it more like so: Live On Coliru

#include <algorithm>
#include <set>
#include <random>
#include <cstdlib>
#include <ctime>
#include <iostream>

using Rng = std::mt19937;

template <unsigned N = 10, unsigned min = 1, unsigned max = 50>
class LottoDraw {
    using Number = unsigned;
    using Numbers = std::set<Number>;

    Numbers _numbers;
  public:
    explicit LottoDraw(Rng& rng) {
        std::uniform_int_distribution<Number> dist(min, max);

        while(_numbers.size()<N) {
            _numbers.insert(dist(rng));
        }
    }

    Numbers allWinning() const {
        return _numbers; 
    }

    bool isWinning(Number check) const {
        return _numbers.find(check) != _numbers.end();
    };
};

struct Lottery {
    using Draw = LottoDraw<10, 1, 50>;

    Draw makeDraw() {
        return Draw(rng);
    }

  private:
    Rng rng { std::random_device{} () };
};

int main() {
    Lottery lotto;
    auto draw = lotto.makeDraw();

    std::cout << "\nEnter a number: ";
    int guess;
    while (std::cin >> guess) {
        if (draw.isWinning(guess)) {
            std::cout << "\nCongrats! Your number matches one from the lottery!";
            break;
        }
        std::cout << "\nEnter a number: ";
    }

    std::cout << "\nThe winning numbers are:";
    for (auto n : draw.allWinning())
        std::cout << " " << n;

    std::cout << "\n";
}

Which you can obviously do without the "fancy" library stuff: Live On Coliru

#include <cstdlib>
#include <ctime>
#include <iostream>

class LottoDraw {
    using Number = unsigned;
    using Numbers = Number[10];

    Numbers _numbers;
  public:
    LottoDraw() {
        for (auto& n : _numbers)
            n = rand()%51 + 1;
    }

    Numbers const& allWinning() const {
        return _numbers; 
    }

    bool isWinning(Number check) const {
        for (auto n : _numbers)
            if (check == n)
                return true;
        return false;
    };
};

struct Lottery {
    using Draw = LottoDraw;

    Lottery() {
        srand(time(0));
    }

    Draw makeDraw() {
        return Draw{};
    }
};

int main() {
    Lottery lotto;
    Lottery::Draw draw = lotto.makeDraw();

    std::cout << "\nEnter a number: ";
    int guess;
    while (std::cin >> guess) {
        if (draw.isWinning(guess)) {
            std::cout << "\nCongrats! Your number matches one from the lottery!";
            break;
        }
        std::cout << "\nEnter a number: ";
    }

    std::cout << "\nThe winning numbers are:";
    for (auto n : draw.allWinning())
        std::cout << " " << n;

    std::cout << "\n";
}

The main thing missing there is

  1. uniform random distribution
  2. unique numbers in draw

What was the problem

Your problem was about How do I use arrays in C++? : arrays are petty heritage from C and do not behave "well" as first-class types.

You can sidestep all that by using the c++11 convenience spelling of arrays: std::array which removes all of that:

#include <array>

using Number = unsigned;
using Draw = std::array<Number, 10>;

int check(Draw &draw);
void draw(Draw &draw);
void printOut(Draw const &draw);

int main() {
    srand(time(0));

    Draw win{};
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Also, if I may, you should implement something like a partial Fisher Yates algorithm, as the OP want unique 10 extracted numbers out of 50. – Bob__ Oct 11 '17 at 08:32
  • 1
    @Bob__ Completely agreed. I deemed _something_ should be out of scope for now :) – sehe Oct 11 '17 at 08:33
0

Instead of trying to call the check function, I instead used the win array that was already in main.

const int arraySize = 10;
    int win[arraySize] = {};

    cout << "Please enter a number: ";
    int guess;
    cin >> guess;


    draw(win);

    for (int i = 0; i < 10; ++i){
        if (win[i] == guess){
            cout << "\n";
            cout << "Congrats! Your number is a match! ";
        }
    }
Breinz
  • 31
  • 4