Advice on checking a 1D array against a 2d array

Hi, I am pretty sure I am on the right track, but not positive. The below code snippet is a function that checks a dynamically allocated 1D array of user input between integers 1-4 against a dynamically allocated 2d array that is randomly filled with integers 1-4. The size of both arrays will be the same, as it is decided by the variable size from the user. if the user enters 12, both arrays will be of size 12.

The goal of the function is to see if the user has guessed a row, or a column correctly, and of course diagonally. Most of the time it seems to work, but sometimes, I see a match I should have gotten but it says no match.

Can anyone see a way to make this better? or optimize? Also, another thing I am struggling with is how to print out the number of the row or column that was a match.

I didn't want to post all the code because it's a bit long, but here is a pastebin link if one is so inclined and interested. If you do happen to look at the full program, don't hesitate to critique my coding standards, approach, or anything you think I could be doing better. All advice is greatly appreciated and welcomed.

https://pastebin.com/1LiNCsSg

Thank you for reading

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  void checkForMatch(int** arr1, int* arr2, int size){
   
    int d1Match = 0;
    int d2Match = 0;
    int rMatch = 0;
    int cMatch = 0;
   
    // checking for row matches
    for (int row = 0; row < size; row++){
        if (arr2[row] == arr1[0][row]){
            rMatch++;
        }    
    }
    // check for column matches
    for(int col = 0; col < size; col++){
        if (arr2[col] == arr1[col][0]){
                cMatch++;
        }
    }
    // check for diagonal top to bottom
    for (int i = 0; i < size; i++){
        if (arr2[i] == arr1[0][0]){
            d1Match++;
        }
    }
    // check for diagonal bottom to top?
    for(int i = 0; i < size; i++){
        if (arr2[size - i - 1] == arr1[size - 1][0]){
            d2Match++;
        }
    }
    if (rMatch == size){
        std::cout << "\n  You matched on a row\n";
    }if (cMatch == size){
        std::cout << "\n  You matched on a column\n";
    }if (d1Match == size){
        std::cout << "\n  You matched top to bottom diagonally\n";
    }if (d2Match == size){
        std::cout << "\n  You matched bottom to top diagonally\n";
    }else if (rMatch != size && cMatch != size && d1Match != size && d2Match != size){
        std::cout << "\n  No matches detected: You Lose!!\n";
    }
}
Last edited on
> std::cout << "Welcome to the lotto-bingo game\n"
Right, so create a readable program with names like
- bingoCard for a data structure
- playBingo() as the name of a function.

Why is everything a meaningless variation on 'array'. It's hard to follow whether you're doing the right thing or not.

Go through your code and eliminate all your 'array' and 'arr' and give them a meaningful name. When you can actually read what your code is doing, you might actually realise as you try to name something that what you're doing is completely dumb (and thus find the answer to your question).

checkForMatch() should return a result of some sort. The actual display of the outcome should be in another function. Keep functions short, with a clearly defined purpose.



1
2
3
4
5
6
7
8
9
    do{
        array = new int* [size];
        for (int i = 0; i < size; i++){
            array[i] = new int[size];
        }
        if (array == nullptr){
            std::cout << "Allocation failed\n";
        }
    } while (array == nullptr);

1. It's too late to check for array == nullptr, when you've already been busy using array[i].
2. You can generally assume that if new returns nullptr, that it's always going to return nullptr if you keep asking for the same size. Your while loop is kinda redundant.
3. The usual convention is to output error messages on std::cerr.
Topic archived. No new replies allowed.