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

This is a discussion on create a pattern from center to edge in a 2d array within the C Programming forums, part of the General Programming Boards category; Hi, This is an assignment. I am really new to C, so please have patience with me. I need to ...

1. ## 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. ..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. 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. 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. 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. 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. 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).

8. 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. 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. 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. Originally Posted by tabstop
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

12. 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. ok, thanks for your reply. If i will get my head around it, i will post the solution here.

14. Originally Posted by tabstop
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);
}
}

//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");
}

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. 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.

Page 1 of 2 12 Last