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 6902071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:45:10+00:00 2026-05-27T07:45:10+00:00

I have a 2 dimensional array filled with 0s and 1s. I have to

  • 0

I have a 2 dimensional array filled with 0s and 1s. I have to display that array in way that:
– 0s are always shown
– 1s are shown one at the time.

It suppose to look like a maze where 0 is a wall and 1 is a current position.
How can I do that in c++?

EDIT:
I came up with a solution but maybe there is simpler one. What if I’d create copy of my _array and copy 0s and blank spaces instead of 1s to it. Then in loop I’d assign one of _array “1” to second array then display whole array and then make swap 1 back with blank space?

EDIT2:

int _tmain(int argc, _TCHAR* argv[])
{
    file();
    int k=0,l=0;
    for(int i=0;i<num_rows;i++)
    {
        for(int j=0;j<num_chars;j++)
        {
            if(_array[i][j] == 1)
            {
                k=i;
                l=j;
                break;
            }
        }
    }
    while(1)
    {
        for(int i=0;i<num_rows;i++)
        {
            for(int j=0;j<num_chars;j++)
            {
                if(_array[i][j] == 0) printf("%d",_array[i][j]);
                else if(_array[i][j]==1)
                {
                    if(k==i && l==j)
                    {
                        printf("1");                

                    }
                    else printf(" ");

                }               
                l++;
                if(l>num_chars) break;
            }
            k++;
            l=0;
            printf("\n");               
        }
        k=0;
        system("cls");
    }
    return 0;
}

I wrote something like that but still i don’t know how to clear screen in right moment. Function file() reads from file to 2D array.

  • 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-27T07:45:11+00:00Added an answer on May 27, 2026 at 7:45 am

    Assuming you want something like that

    000000
    0    0
    0000 0
    0 1  0
    0 0000
    000000
    

    You could print a 0 whenever it occurs and a blank space if not. To handle the current position you could use two additional variables like posX, posY. Now everytime you find a 1 in your array you check if (j == posX && i = posY) and print 1 if so…

    As you just need to visualize the maze at different possible positions I’d propose a simple display function. DisplayMaze(int x, int y) is printing the maze in the required format to the screen. If _array[y][x] == 1 there is also printed a single 1…

    void DisplayMaze(int x, int y)
    {
        for (int row = 0; row < num_rows; row++)
        {
            for (int col = 0; col < num_chars; col++)
            {
                if (_array[row][col] == 0)
                    std::cout << "0 ";
                else if (row == y && col == x)
                    std::cout << "1 ";
                else
                    std::cout << "  ";
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }
    

    In order to display all possible positions you have to iterate over all of them and check if the current position is marked with 1 in the array (otherwise displaying would’t make sense)

    for (int y = 0; y < num_rows; y++)
    {
        for (int x = 0; x < num_chars; x++)
        {
            if (_array[y][x] == 1)
            {
                DisplayMaze(x, y);
            }
        }
    }
    

    The output should look like:

    0 0 0 0 0 0
    0 1       0
    0 0 0 0   0
    0         0
    0   0 0 0 0
    0 0 0 0 0 0
    
    0 0 0 0 0 0
    0   1     0
    0 0 0 0   0
    0         0
    0   0 0 0 0
    0 0 0 0 0 0
    
    0 0 0 0 0 0
    0     1   0
    0 0 0 0   0
    0         0
    0   0 0 0 0
    0 0 0 0 0 0
    
    0 0 0 0 0 0
    0       1 0
    0 0 0 0   0
    0         0
    0   0 0 0 0
    0 0 0 0 0 0
    
    0 0 0 0 0 0
    0         0
    0 0 0 0 1 0
    0         0
    0   0 0 0 0
    0 0 0 0 0 0
    

    …

    However, i’d recommend a more C++ like approach as a maze could be implemented as a class. This class could bring it’s own display-method and would encapsulate the internal data. It could basically look like:

    class Maze
    {
    public:
        // generate empty maze with given size
        Maze(int width, int height);
    
        // destructor
        ~Maze();
    
        // print maze if the given position is marked with 1
        void printPosition(int x, int y) const;
    
        // takes a cstring as input to initialize the maze from
        Maze& operator<<(const char* input);
    
        // returns true if the given position is marked with 1
        bool isValidPosition(int x, int y) const;
    
    private:
        // this is the actual representation of the maze
        std::vector<std::vector<int> > grid_;
    };
    

    it would be used as followes:

    Maze myMaze(num_chars, num_rows);
    myMaze << "000000"
        "011110"
        "000010"
        "011110"
        "010000"
        "000000";
    
    for (int y = 0; y < num_rows; y++)
    {
        for (int x = 0; x < num_chars; x++)
        {
            if (myMaze.isValidPosition(x,y))
            {
                myMaze.printPosition(x,y);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one-dimensional array of strings in JavaScript that I'd like to turn
I have a one dimensional array in VBScript that I would like to run
I have a numpy one dimensional array c that is supposed to be filled
I have a multi-dimensional array right now that looks like this: function art_appreciation_feeds() {
I have a multi-dimensional array that I'd like to use for building an xml
I have a 2 dimensional array which is like that; $results[$i][$j]->title; $results[$i][$j]->snippet; $results[$i][$j]->link; It
I have a multi-dimensional array that looks like this: The base array is indexed
I have a 2 dimensional 9 x 9 array (twoArray) that is filled with
I have a 2 dimensional array, like so: char[,] str = new char[2,50]; Now,
I have a two dimensional array that I need to load data into. I

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.