Thread: create a pattern from center to edge in a 2d array

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    10

    create a pattern from center to edge in a 2d array

    Hi,

    This is an assignment. I am really new to C, so please have patience with me.

    I need to create 12x12 matrix populated with random numbers(0 to 250) and 10% blank spaces. From the center of the matrix (this is odd because a 12x12 matrix is not symmetric), i need to create a movement pattern horizontally or vertically. The cell numbers hit must be greater that the starting number and of course the starting number cannot be blank.

    So far I succeed to create a 2d array with random numbers. I am really under pressure because on Friday I need to hand-out the assignment.

    This is my code and thanks in advance:

    Code:
    const int a=12;
    const int b=12;
    int matrix[a][b];
    int rnum;
    int t[a];
     
    srand(time(0)); // Initialize random number generator.
    rnum = (rand() % 100) + 1;
     
     
     
    for(int r=0; r<a; r++)//row
    { for(int c=0; c<b; c++)
    matrix [r][c] = (rand()%250) + 1;
    }
     
    for(int r=0; r<a; r++)//row
    { for(int c=0; c<b; c++) //column
     
    {
    cout << matrix[r][c] << ' '; //display table
    
     
    }
    
    cout << endl;
    }

    getchar();

    return 0;

  2. #2
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    ..with random numbers(0 to 250) and 10% blank spaces.
    I think it would be easier to understand what you needed if you posted a sample output

    From the center of the matrix (this is odd because a 12x12 matrix is not symmetric), i need to create a movement pattern horizontally or vertically.
    So it has to be animated?

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    10
    No, does not need to be animated. The pattern from the center to edge needs to be printed out and saved to a file, to be able to access it if necessary. The output should look like this one: image

  4. #4
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    The output should look like this one: image
    :/ Sorry, I still don't understand what you are looking for. Can you create a sample output in ASCII using the code tags?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you supposed to store/print/whatever: the numbers along the path; the coordinates of the path; the steps taken (right, left, down, etc.); something else?

    (Also the sample is 15x15. Also it doesn't appear to follow the directions, since the starting number was 10 and all the other numbers were smaller than that.)

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    10
    I am sorry for my English. After the creation of the matrix with random numbers from 0 to 250, I need to create a movement pattern. The start cell should be in the center of the matrix and should move toward the edge to reach an outer cell.

    I really do not know what you mean about sample output.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    10
    My matrix does not to be exactly like that. My requirements are random numbers from 0 to 250 and the movement should be horizontally or vertically. Because the center is a random number will change every time when the matrix is created and the pattern should change to meet the requirements(The cell numbers hit must be greater that the cell starting number and of course the cell starting number cannot be blank).

    I do not have a sample output for my exercise, i need to created. the image is just an idea how supposed to look. The line showing the pattern from center to the edge does not need to be printed in matrix, is just for guidance).
    Last edited by ryuti; 05-23-2011 at 01:37 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you mean "I have to output a PNG image", we're all going to just abandon this as "too hard to do in a thread". For the example you showed us, what output should the program give?

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    10
    I do not need to output a png image. The output of the matrix, following the image i have posted should be: 10,5,1,2,2,3,5,1,7(just integers)

    I am interested to move from the center of the matrix toward the edge.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So it looks like the next step is to put in the blanks. You can randomly generate (x,y) coordinates, and then change the value in the box to a "special" number.

    Then you need to solve the maze. It looks like you get a choice of 4 starting squares, although even then I think you might still end up with something unsolvable if you get unlucky with the randomness.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    10
    Quote Originally Posted by tabstop View Post
    So it looks like the next step is to put in the blanks. You can randomly generate (x,y) coordinates, and then change the value in the box to a "special" number.

    Then you need to solve the maze. It looks like you get a choice of 4 starting squares, although even then I think you might still end up with something unsolvable if you get unlucky with the randomness.
    Can you please guide me how to put in the blanks spaces? Then I will try to set the 4 squares not to be equal with zero using the location of the squares ---matrix[5][5]....---- with pointers
    Last edited by ryuti; 05-23-2011 at 02:10 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to randomly generate (x,y) coordinates, and then change the value in your grid at that spot to some otherwise-impossible number (bigger than your limit, or smaller than your minimum).

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    10
    ok, thanks for your reply. If i will get my head around it, i will post the solution here.

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    10
    Quote Originally Posted by tabstop View Post
    You need to randomly generate (x,y) coordinates, and then change the value in your grid at that spot to some otherwise-impossible number (bigger than your limit, or smaller than your minimum).
    Hi again,

    With a help of a mate I have sort out the matrix:

    Code:
    //My Variables
    int matrix[12][12];
    char dnum[3];
    int i, i2,m;
    
    //function to generate the matrix
    void generatematrix(void){
    	srand(time(NULL));//Determine Rand Base Number
    	for(i=0;i<12;i++){
    		for(i2=0;i2<12;i2++)
    			matrix[i][i2] = (rand() % 250);
    	}
    	addspaces(25);
    } 
    
    //function to display the matrix
    void displaymatrix(void){
    	clrscr();
    	for(i=0;i<12;i++){
    		for(i2=0;i2<12;i2++){
    			if(matrix[i][i2] == 250){
    				printf("[   ]");
    				continue;
    			}
    			setmovecol(i,i2);
    			itoa(matrix[i][i2],dnum,10);
    			printf("[%03s]",dnum);
    			settextbackgroundcolour(black,0);
    		}
    		printf("\n");
    	}
    
    //function to add blank spaces
    void addspaces(int max){
    	int r1,r2;
    	for(i=0;i<max;i++){
    		r1 = (rand() % 10);
    		r2 =  (rand() % 10);
    		matrix[r1][r2] = 250;
    	}
    }

    Now can you please guide me. You said before there are 4 possible starting cells. I would like to point to all of them and choose the one that does no contain a blank space and after how do i move to the next cell to sort out the maze?

    Thanks!

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you know what the original coordinates should be. You can check the value in matrix to make sure it's not blank; then to move you can adjust one coordinate plus or minus one depending on which way you want to go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  2. Wish to create a pattern of numbers using loops
    By bharat_kaushik in forum C Programming
    Replies: 12
    Last Post: 09-04-2009, 11:08 AM
  3. Edge Detect
    By 250co in forum C Programming
    Replies: 0
    Last Post: 05-22-2006, 08:51 PM
  4. storing a pattern of characters into a 2D array
    By haroonie in forum C Programming
    Replies: 2
    Last Post: 04-20-2005, 05:19 AM