Thread: Help - Walk in a bidimensional array

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    105

    Help - Walk in a bidimensional array

    Hello everyone!
    What I have to do is to print a bidimensional array of N*N like this:

    Help - Walk in a bidimensional array-caracol1-png


    What I tried to do was:
    - a function called Right that goes to the right
    - a function called Left that goes to the left
    - a function called Up that goes up
    - a function called Down that goes down

    Like this:

    Help - Walk in a bidimensional array-caracol2-png

    As you can see, in the first bidimensional array, N=6. I have to go 3 times right, 3 times left, 2 times up and 2 times down. So, I have to go N/2 times to the left and to the right, and (N/2 - 1) times to up and down.
    In the second bidimensional array, N=7. I have to go 3 times to left, right, up and down. So, I have to go N/2 times to left, right, up and down, and, in the end, I have to add the number that is in [N/2][N/2]

    Here's the code. It works fine for this columns and rows:
    Help - Walk in a bidimensional array-caracol3-png

    but I don't know how to do a for that says the program how much times to do those 4 steps.
    I know how many times they have to be done:
    - If N/2==0, the program has to do (N/2) times to left and right, and (N/2 -1) times up and down
    - If N/2==1, the program has to do (N/2) times to left, right, up and down, and I have to add the number in the element [N/2][N/2]

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void CompleteBidimArray(int rows, int cols, int mat[rows][cols])
    {
        int i, j;
    
        for(i=0; i<rows; i++)
        {
            for(j=0; j<cols; j++)
            {
                printf("\n[%d][%d]: ", i, j);
                scanf("%d", &mat[i][j]);
            }
        }
    }
    
    void PrintOriginalBidimArray(int rows, int cols, int mat[rows][cols])
    {
        int i, j;
    
        for (i=0; i<rows; i++)
        {
            for(j=0; j<cols; j++)
            {
                printf("%4d", mat[i][j]);
            }
            printf("\n");
        }
    }
    
    void Right (int rows, int cols, int InitialRow, int mat[rows][cols])
    {
        int InitialCol=InitialRow;
        int j;
        printf("\n\nRIGHT\n");
        for(j=InitialCol; j<(cols-InitialCol); j++)
        {
            printf("%4d", mat[InitialRow][j]);
        }
    }
    
    void Down(int rows, int cols, int InitialCol, int mat[rows][cols])
    {
        int InitialRow=cols-InitialCol;
        int i;
        printf("\n\nDOWN\n");
        for(i=InitialRow; i<(rows-InitialRow); i++)
        {
            printf("%4d", mat[i][InitialCol]);
        }
    }
    
    void Left (int rows, int cols, int FinalCol, int mat[rows][cols])
    {
        int FinalRow=FinalCol;
        int j;
        printf("\n\nLEFT\n");
        for(j=FinalCol; j>(cols-FinalCol-2); j--)
        {
            printf("%4d", mat[FinalRow][j]);
        }
    }
    
    
    void Up (int rows, int cols, int FinalRow, int mat[rows][cols])
    {
        int FinalCol=cols-FinalRow-1;
        int i;
        printf("\n\nUP\n");
        for(i=FinalRow-1; i>(rows-FinalRow-1); i--)
        {
            printf("%4d", mat[i][FinalCol]);
        }
    }
    
    
    int main()
    {
        int rows, cols;
        printf("\nRows: ");
        scanf("%d", &rows);
        printf("\nColumns: ");
        scanf("%d", &cols);
    
        int mat[rows][cols];
    
        CompleteBidimArray(rows, cols, mat);
        PrintOriginalBidimArray(rows, cols, mat);
        Right(rows, cols, 0, mat);
        Down(rows, cols, cols-1, mat);
        Left(rows, cols, cols-1, mat);
        Up(rows, cols, cols-1, mat);
    
        if(rows%2==1)
        {
            printf("\n\n %d", mat[rows/2][cols/2]);
        }
    
    return 0;
    }

    Thankssssssss!

    Juan

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Follow the "right hand rule":

    1. Start by going right, as far as you can. Put a unique "have been there" value, into each square of the array, you have visited.

    2. When you can no longer move into a new square in the current direction, loop back to #1.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Adak View Post
    Follow the "right hand rule":

    1. Start by going right, as far as you can. Put a unique "have been there" value, into each square of the array, you have visited.

    2. When you can no longer move into a new square in the current direction, loop back to #1.
    I like this but I wouldn't use a "have been there" value as I don't think it's necessary (although it will work and isn't "bad"). You can easily calculate the path length for 1 entire "rotation/spiral" (re-calculate each "rotation".) The total path length is known as well, so you know when to stop pirouetting.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I like this but I wouldn't use a "have been there" value as I don't think it's necessary.
    O_o

    If I understand the problem, one could use the example you gave in the other thread to "clamp" `size-2' over center each pass while using most of the code in the original post for the "edges".

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Thanks for your answers!

    I don't know what is a "have been there" value. Do you have any example to see how to use it?

    I know how many times I have to do each step, but I don't know how to put that into a for.

    Thanks!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by juanjuanjuan View Post
    Thanks for your answers!

    I don't know what is a "have been there" value. Do you have any example to see how to use it?

    I know how many times I have to do each step, but I don't know how to put that into a for.

    Thanks!
    NO! God NO! Don't start counting steps!

    It's simple, in one loop, you have two if statements. The loop ends when there are no vacant squares left. It's EZ!

    Yes, I made a maze solver program using it. but I'm not going to post it. THIS IS EASY, and elegant in it's utter simplicity (which makes it a bit hard to see at first). Let the loop do the work for you (almost).

    A "has been there" value could be any value: -1, -99, +1, +1234. Set all the array squares to 0, and then as you visit a square, you give it a different value, which is always the same - that's your "been there" value.


  7. #7
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Sorry, I didn't understand everything. You say that, in a for, I have to write 2 if.
    If (continue with the steps)
    If (stop because I finish the "square")

    Are you saying that? I'm trying to write a program that says that but I can't

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Hodor View Post
    I like this but I wouldn't use a "have been there" value as I don't think it's necessary (although it will work and isn't "bad"). You can easily calculate the path length for 1 entire "rotation/spiral" (re-calculate each "rotation".) The total path length is known as well, so you know when to stop pirouetting.
    Nothing wrong with that approach, but it's more code. This algorithm minimizes BEAUTIFULLY, if you let it. WAY EZ.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by juanjuanjuan View Post
    Sorry, I didn't understand everything. You say that, in a for, I have to write 2 if.
    If (continue with the steps)
    If (stop because I finish the "square")

    Are you saying that? I'm trying to write a program that says that but I can't
    You can use either a for or a while or a do while loop. They all can be made to work in place of the other. I like the for loop for this one.

    You turn right on square one, and enter the loop.
    Code:
    start the loop: (stop the loop when you have no more empty squares) {
       You walk straight ahead. 
       (either the end of the array has been reached, or the square in front of you has a "been visited" value in it)
        If (you can't walk in that direction any more, && you have an empty square on your right)
           You turn right
    }
    Give it some work, Juan!

    Isn't that a thing of simple "noir" genre beauty?
    Last edited by Adak; 02-14-2014 at 11:42 PM.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Adak View Post
    WAY EZ.
    EZ PZ?

    (For those who don't get it, "easy-peasy" is common phrase in certain places.)

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Matticus View Post
    EZ PZ?

    (For those who don't get it, "easy-peasy" is common phrase in certain places.)
    DING! DING! DING! Winner winner, chicken dinner!

  12. #12
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    E-Zed, P-Zed? I don't get it (just kidding!)

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Adak View Post
    DING! DING! DING! Winner winner, chicken dinner!
    Yes, thank you - and hurry please. I'm hungry

  14. #14
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Thanks!

    I put a "have been visited" value (I chose -1 but it's not a good value.. I would love to put a 'hbv' (have been visited) but I can't)

    I don't know why it doesn't work like I want. I wrote the loops like you told me (but I used 1 while and 1 if instead of 2 if)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void CompleteBidimArray(int rows, int cols, int mat[rows][cols])
    {
        int i, j;
    
        for(i=0; i<rows; i++)
        {
            for(j=0; j<cols; j++)
            {
                printf("\n[%d][%d]: ", i, j);
                scanf("%d", &mat[i][j]);
            }
        }
    }
    
    void PrintOriginalBidimArray(int rows, int cols, int mat[rows][cols])
    {
        int i, j;
    
        for (i=0; i<rows; i++)
        {
            for(j=0; j<cols; j++)
            {
                printf("%4d", mat[i][j]);
            }
            printf("\n");
        }
    }
    
    void Right (int rows, int cols, int InitialRow, int mat[rows][cols])
    {
        int InitialCol=InitialRow;
        int j;
        printf("\n\nRIGHT\n");
        for(j=InitialCol; j<(cols-InitialCol); j++)
        {
            printf("%4d", mat[InitialRow][j]);
            mat[InitialRow][j]=-1;
        }
    }
    
    void Down(int rows, int cols, int InitialCol, int mat[rows][cols])
    {
        int InitialRow=cols-InitialCol;
        int i;
        printf("\n\nDOWN\n");
        for(i=InitialRow; i<(rows-InitialRow); i++)
        {
            printf("%4d", mat[i][InitialCol]);
            mat[i][InitialCol]=-1;
        }
    }
    
    void Left (int rows, int cols, int FinalCol, int mat[rows][cols])
    {
        int FinalRow=FinalCol;
        int j;
        printf("\n\nLEFT\n");
        for(j=FinalCol; j>(cols-FinalCol-2); j--)
        {
            printf("%4d", mat[FinalRow][j]);
            mat[FinalRow][j]=-1;
        }
    }
    
    
    void Up (int rows, int cols, int FinalRow, int mat[rows][cols])
    {
        int FinalCol=cols-FinalRow-1;
        int i;
        printf("\n\nUP\n");
        for(i=FinalRow-1; i>(rows-FinalRow-1); i--)
        {
            printf("%4d", mat[i][FinalCol]);
            mat[i][FinalCol]=-1;
        }
    }
    
    
    void PrintModifiedBidimArray(int rows, int cols, int mat[rows][cols])
    {
        int i, j;
        printf("\n");
        for (i=0; i<rows; i++)
        {
            for(j=0; j<cols; j++)
            {
                printf("%4d", mat[i][j]);
            }
            printf("\n");
        }
    }
    
    
    int main()
    {
        int rows, cols, i, j;
        printf("\nRows and Columns: ");
        scanf("%d", &rows);
        cols=rows;
    
        j=cols;
        int a=0;
        int mat[rows][cols];
    
        CompleteBidimArray(rows, cols, mat);
        PrintOriginalBidimArray(rows, cols, mat);
    
        Right(rows, cols, a, mat); //first step
        for (i=1; i<rows; i++)
        {
            while((mat[i-1][j])!=-1)
            {
                Down(rows, cols, cols-1, mat);
                Left(rows, cols, cols-1, mat);
                Up(rows, cols, cols-1, mat);
            }
            a++;
            if((mat[i-1][j])==-1)
            {
                Right(rows, cols, a, mat);
            }
    
        }
    
        if(rows%2==1)
        {
            printf("\n\n %d", mat[rows/2][cols/2]);
        }
    
        PrintModifiedBidimArray(rows, cols, mat);
    
    return 0;
    }
    Thanks!
    Last edited by juanjuanjuan; 02-15-2014 at 12:19 AM.

  15. #15
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Matticus View Post
    Yes, thank you - and hurry please. I'm hungry
    Hang on! You're not going to share? I wouldn't mind a chicken dinner myself Especially one of Adak's fine dining meals!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Bidimensional array
    By juanjuanjuan in forum C Programming
    Replies: 17
    Last Post: 02-14-2014, 08:43 PM
  2. problem with pointers and bidimensional arrays
    By Gustaff in forum C Programming
    Replies: 4
    Last Post: 02-07-2013, 12:42 AM
  3. Program that generates a "random walk" across 10*10 array
    By danieldcc in forum C Programming
    Replies: 37
    Last Post: 07-23-2011, 01:19 AM
  4. 4 questions about bidimensional table
    By Dan. in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2011, 11:18 AM
  5. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM