Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3750776
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T08:58:48+00:00 2026-05-19T08:58:48+00:00

I have an 5×10 array that is populated with random values 1-5. I want

  • 0

I have an 5×10 array that is populated with random values 1-5. I want to be able to check when 3 numbers, either horizontally, or vertically, match. I can’t figure out a way to do this without writing a ton of if statements.

Here is the code for the randomly populated array


int i;
int rowincrement = 10;
int row = 0;
int col = 5;
int board[10][5];
int randomnum = 5;


int main(int argc, char * argv[])
{
    srand(time(NULL));

    cout << "============\n";

    while(row < rowincrement)
    {

        for(i = 0; i < 5; i++)
        {
            board[row][col] = rand()%5 + 1; 
            cout << board[row][col] << " ";
        }
        cout << endl;
        cout << "============\n";
        row++;
    }
    cout << endl;
    return 0;
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T08:58:49+00:00Added an answer on May 19, 2026 at 8:58 am

    Suppose that you have some particular starting point (x, y) and you’re curious if there’s three equal numbers in a row that start at this point. Let’s consider just the case where you’re looking in the horizontal direction. Then one way to do this (ignoring bounds-checking) would be like this:

    bool IsHorizontalMatch(int x, int y) {
        /* Get the value of the start position. */
        const int startValue = board[x][y];
    
        /* Confirm the two values after it match. */
        for (int i = 1; i < 3; ++i)
            if (board[x + i][y] != startValue)
                return false;
    
        /* If we got here, then they all match! */
        return true;
    }
    

    You could similarly write a function like this for checking vertically:

    bool IsVerticalMatch(int x, int y) {
        /* Get the value of the start position. */
        const int startValue = board[x][y];
    
        /* Confirm the two values after it match. */
        for (int i = 1; i < 3; ++i)
            if (board[x][y + i] != startValue)
                return false;
    
        /* If we got here, then they all match! */
        return true;
    }
    

    And finally, one for the diagonals:

    bool IsDiagonalDownMatch(int x, int y) {
        /* Get the value of the start position. */
        const int startValue = board[x][y];
    
        /* Confirm the two values after it match. */
        for (int i = 1; i < 3; ++i)
            if (board[x + i][y + i] != startValue)
                return false;
    
        /* If we got here, then they all match! */
        return true;
    }
    
    bool IsDiagonalUpMatch(int x, int y) {
        /* Get the value of the start position. */
        const int startValue = board[x][y];
    
        /* Confirm the two values after it match. */
        for (int i = 1; i < 3; ++i)
            if (board[x + i][y - i] != startValue)
                return false;
    
        /* If we got here, then they all match! */
        return true;
    }
    

    This works, but it’s just not very elegant; all three of these functions look very similar! Fortunately, you can rewrite all of them in terms of a single unifying function. The idea is this – if you’ll notice, all three functions work by having some “step size” defined indicating what direction you move. In the horizontal case, the step is (+1, +0), in the vertical case it’s (+0, +1), and in the diagonal it’s (+1, +1) or (+1, -1). Given this, you can write one function to check if three values match in a line:

    bool IsLinearMatch(int x, int y, int stepX, int stepY) {
        /* Get the value of the start position. */
        const int startValue = board[x][y];
    
        /* Confirm the two values after it match. */
        for (int i = 1; i < 3; ++i)
            if (board[x + i * stepX][y + i * stepY] != startValue)
                return false;
    
        /* If we got here, then they all match! */
        return true;
    }
    

    You can then write

    bool IsLineStartingAt(int x, int y) {
        return (IsLinearMatch(x, y, 1,  0) ||  // Horizontal
               IsLinearMatch(x, y, 0,  1)  ||  // Vertical
               IsLinearMatch(x, y, 1,  1)  ||  // Diagonal Down
               IsLinearMatch(x, y, 1, -1));    // Diagonal Up
    }
    

    Given this primitive, you can check for all possible matches by just iterating over all possible starting points.

    Hope this helps!

    EDIT: Thanks to commenters for helping to fix my silly bugs. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableView that is being populated from an NSMutable array that has
suppose that we have three array int a[]=new int[]{4,6,8,9,11,12}; int b[]=new int[]{3,5,7,13,14}; int c[]=new
I have a PHP array that I need to sort. I have included the
I have an array that store data. If I subtract two arrays I get
I have a cell array that needs to be printed in a .txt file
Have dict like: mydict= {'a':[],'b':[],'c':[],'d':[]} list like: log = [['a',917],['b', 312],['c',303],['d',212],['a',215],['b',212].['c',213],['d',202]] How do i
I have the following numpy array: # A B C Y my_arr = np.array([
I have a text that contains usernames prepended with @ symbol. Example: One day
This is a php question. I have an array of arrays: Array ( [1]
I have to put my database's data in this format (values will differ, obviously),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.