Thread: Help - Walk in a bidimensional array

  1. #31
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by juanjuanjuan View Post
    Sorry, Adak, I didn't understand. I know you are saying when the program has to enter in each loop, but I don't know why you wrote the number of the direction in a bidimensional array...
    Run my program. Your position in the array has to move all four directions: up, right, down and left. You need only one loop, but you need some kind of logic to control the movement in all four directions.

  2. #32
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    I think I understand what you're saying. I make the 4 steps. When "Up" finishes, there is a "have been there" in one position upper (row), so it has to turn right again and the other 3 steps, and continue doing that.. isn't it?

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes!

    And since there is ALWAYS another empty square in this pirouette pattern, until they're all full, only one loop is needed.

  4. #34
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    OK. So, if you're saying that, I don't understand the code you wrote

    I don't know what to do. For Sunday afternoon (today) I have to finish this program and 2 more, but I don't know how to do this one, I can't think it in the correct way, I suppose
    Last edited by juanjuanjuan; 02-15-2014 at 10:01 PM.

  5. #35
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Speaking of spirals, I would think that the answer has something to do with polar coordinates and traversing roughly the points specified by an Archimedes spiral.

  6. #36
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Finally, I did it! It works!


    Thanks!!!!

  7. #37
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Although the question is solved, perhaps the following code may inspire a different approach to the problem. Whether a different method is "better" or "worse" than the maze walk is irrelevant because Hodor likes trying to achieve the same result in different ways. I haven't posted a complete solution, the code below is not a solution at all, but maybe it will trigger an idea.

    I will start by saying that multidimensional arrays irk me for some reason; I honestly prefer working with just one dimension and "project" that single dimension in different ways.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    enum matrixorder { ROWMAJOR, COLMAJOR, ROWMAJOR_R, COLMAJOR_R };
    
    void int_swap(int *a, int *b);
    int *matrix_init(int *matrix, int w, int h);
    void matrix_print(int *matrix, enum matrixorder order, int w, int h);
    int matrix_getelem_rowmajor(int *matrix, int w, int h, int x, int y);
    int matrix_getelem_rowmajor_rev(int *matrix, int w, int h, int x, int y);
    int matrix_getelem_colmajor(int *matrix, int w, int h, int x, int y);
    int matrix_getelem_colmajor_rev(int *matrix, int w, int h, int x, int y);
    
    int main(void)
    {
        int *matrix;
        const int w = 4, 
                  h = 2;
        
        matrix = malloc(sizeof *matrix * w * h);
        if (!matrix) {
            perror("Could not allocate memory for matrix");
            exit(1);
        }
        
        matrix_init(matrix, w, h);
        
        matrix_print(matrix, ROWMAJOR, w, h);
        printf("-------------------\n");
        matrix_print(matrix, ROWMAJOR_R, w, h);
        printf("-------------------\n");
        matrix_print(matrix, COLMAJOR, w, h);
        printf("-------------------\n");
        matrix_print(matrix, COLMAJOR_R, w, h);
        
        free(matrix);
        
        return 0;
    }
    
    void int_swap(int *a, int *b)
    {
        int t;
        
        t = *a;
        *a = *b,
        *b = t;
    }
    
    int *matrix_init(int *matrix, int w, int h)
    {
        int row, col, i;
       
        /* Initialise with test data */
        for (i = row = 0; row < h; row++)
            for (col = 0; col < w; col++)
                matrix[row * w + col] = ++i;
        return NULL;
    }
    
    
    void matrix_print(int *matrix, enum matrixorder order, int w, int h)
    {
        int row, col;
    
        if (order == COLMAJOR || order == COLMAJOR_R)
            int_swap(&w, &h);
        
        for (row = 0; row < h; row++) {
            for (col = 0; col < w; col++) {
                int val;
                if (order == ROWMAJOR)
                    val = matrix_getelem_rowmajor(matrix, w, h, col, row);
                else if (order == ROWMAJOR_R)
                    val = matrix_getelem_rowmajor_rev(matrix, w, h, col, row);
                else if (order == COLMAJOR)
                    val = matrix_getelem_colmajor(matrix, w, h, col, row);
                else
                    val = matrix_getelem_colmajor_rev(matrix, w, h, col, row);
                printf("%4d", val);
            }
            printf("\n");
        }
    }
    
    int matrix_getelem_rowmajor(int *matrix, int w, int h, int x, int y)
    {
        return matrix[x + y * w];
    }
    
    int matrix_getelem_rowmajor_rev(int *matrix, int w, int h, int x, int y)
    {
        x = w - x - 1;
        y = h - y - 1;
        return matrix_getelem_rowmajor(matrix, w, h, x, y);
    }
    
    
    int matrix_getelem_colmajor(int *matrix, int w, int h, int x, int y)
    {
        int_swap(&w, &h);
        return matrix[y + x * w];
    }
    
    int matrix_getelem_colmajor_rev(int *matrix, int w, int h, int x, int y)
    {
        x = w - x - 1;
        y = h - y - 1;
        return matrix_getelem_colmajor(matrix, w, h, x, y);
    }

  8. #38
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Whether a different method is "better" or "worse" than the maze walk is irrelevant because walking pickle pastries likes trying to achieve the same result in different ways.
    O_o

    Type with sense man...

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

  9. #39
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by phantomotap View Post
    O_o

    Type with sense man...

    Soma
    I can't. Unfortunately my pirouette resembles scrambled eggs gone wrong. O_o

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int *matrix_init(int *matrix, int w, int h);
    void matrix_print(int *matrix, int w, int h);
    void matrix_pirouette(int *matrix, int w, int h);
    
    
    int main(void)
    {
        int *matrix;
        const int w = 4, 
                  h = 3;
        
        matrix = malloc(sizeof *matrix * w * h);
        if (!matrix) {
            perror("Could not allocate memory for matrix");
            exit(1);
        }
        
        matrix_init(matrix, w, h);
        matrix_print(matrix, w, h);
        printf("\n");
        matrix_pirouette(matrix, w, h);
        
        free(matrix);
        
        return 0;
    }
    
    
    int *matrix_init(int *matrix, int w, int h)
    {
        int row, col, i;
       
        /* Initialise with test data */
        for (i = row = 0; row < h; row++)
            for (col = 0; col < w; col++)
                matrix[row * w + col] = ++i;
        return NULL;
    }
    
    void matrix_print(int *matrix, int w, int h)
    {
        int row, col;
       
        for (row = 0; row < h; row++) {
            for (col = 0; col < w; col++)
                printf("%4d", matrix[row * w + col]);
            printf("\n");
        }
    }
    
    void matrix_pirouette(int *matrix, int w, int h)
    {
        int nelems;
        int i, j, origin_x, origin_y;
        int totalw;
        
        nelems = w * h;
        totalw = w;
    
        i = 0;
        origin_x = origin_y = 0;
        
        while (i < nelems) {
        
            for (j = 0; j < w; j++) {
                printf("%d ", matrix[origin_x + j + origin_y * totalw]);
                i++;
            }
            if (i == nelems) break;
            
            for (j = 1; j < h - 1; j++) {
                printf("%d ", matrix[origin_x + w - 1 + (origin_y + j) * totalw]);
                i++;
            }
            if (i == nelems) break;
            
            for (j = w - 1; j >= 0; j--) {
                printf("%d ", matrix[origin_x + j + (origin_y + h - 1) * totalw]);
                i++;
            }
            if (i == nelems) break;
           
            for (j = h - 2; j > origin_y; j--) {
                printf("%d ", matrix[origin_x + (origin_y + j) * totalw]);
                i++;
            }
            if (i == nelems) break;
            
            w -= 2;
            h -= 2;
            if (w < 0) w = 0;
            if (h < 0) h = 0;
            
            origin_y++;
            if (w == 0) {   /* yuck */
                printf("%d ", matrix[origin_x + origin_y * totalw]);    
                i++;
            }
            origin_x++;
            
        }
        
        printf("\n");
    }

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