# Thread: replace single character with matrix of char

1. ## replace single character with matrix of char

Hi, i cant find a way to replace a character of a matrix, with a matrix of characters, indeed i want to replace all the O characters of this triangle within matrix[][n] with the the
Code:
`matrix[2][3]= { {'O', 'O', 'O'},{' ', 'O', ' '}};`
for example if i have this triangle:
Code:
```              O

O O O
O O
O     O     O```
than i need to get this one:
Code:
```           OOO
O
OOOOOOO
OOOOO
OOO OOO OOO
O   O   O```
basically what i need to do is to replace every
Code:
`O`
with
Code:
```OOO
O```
any suggestion on how to do this? thanks

2. Edit: OK, I see (I believe), the point of this assignment.

You need a new array, about 11 char's wide, and 6 rows high. Then you can use a for loop on the original array, to create the new one's content, row by row, column by column.

It would help you a great deal, if you would sit down, and go through this exercise by hand. Get a good sense of how you would do this, without any computer.

Then translate what you did by hand, into pseudo-code, and finally, into a program.

I would always try and work with an odd number of columns when you're dealing with printing out triangles (or any vertically symmetrical figure), so you have that important center column. Rows don't matter unless you're dealing with horizontally symmetrical figures.

Post up some code where you try this, and tell us what the problem is that you're stuck on. Glad to help. Not glad to just write up your program for you, however.

3. If you make your original "matrix" a matrix of all pointers, you could do that with a little casting.

However, it is kind of a ridiculous tactic since then you will have a matrix of pointers to single chars. You need to rethink this based on the result you want to achieve, not the method you want to use.

If it is just to arrange characters on the screen, stick to a single 2D matrix, and come up with an algorithm to substitute spaces for O's in the appropriate way. Something to do with:

char matrix[r][c];
matrix[r][c-1], matrix[r][c+1]
matrix[r+1][c]

4. well actually what i would need is to write a recursive function or a function that calls a recursive function that draws a triangle into the matrix of base = num, the function is void triangle(int num, char m[][num]), the matrix m is originally filled up with space characters, the figure must be fractal, heres what i did:
Code:
```void triangle(int num, char m[][num])
{
int a, b, c;
for(a = 1; a <= num; a++) {
for(c = num-a; c>0; c--) {
printf(" ");
}
for(b = 1; b <= num; b++) {
if((pascal(a,b) % 2) == 0) {
m[a][b] = 'O';
}
printf("%2.c", m[a][b]);
}
printf("%c", '\n');
}
}
int pascal(int num, int c)           // recursive function, checks the position of a number into the pascal's triangle
{
if((c == 0) || (c == num)) {
return 1;
}
else if(c > num) {
return 1;
}
else
return pascal(num-1, c-1) + pascal(num-1, c);
}```
The first function checks if the position of m[a][b] into the pascal triangle is an even number, if this condition is true it draws a O character into the matrix and so on..
now that actually draws a triangle into the matrix, and it is fractal, but the previous problem remains, so im thinking that maybe i've got to come up with a new idea

5. Originally Posted by rob90
basically what i need to do is to replace every
Code:
`O`
with
Code:
```OOO
O```
any suggestion on how to do this? thanks
If that's what you're trying to do then the resultant triangle doesn't make any sense, in the sense that it is missing an entire row.
I get the substitution for the first and last rows but not for the two intermediate rows? So please explain.

6. Originally Posted by itCbitC
If that's what you're trying to do then the resultant triangle doesn't make any sense, in the sense that it is missing an entire row.
I get the substitution for the first and last rows but not for the two intermediate rows? So please explain.
The triangle that I get is really similar to this (whit O replacing the empty triangles), here it is:
Code:
```Enter an integer: 17

O

O O O
O O
O   O   O

O O O O O O O
O O O O O O
O   O O O O O   O
O O O O
O O O   O O O   O O O
O O     O O     O O
O   O   O   O   O   O   O

O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O```
you can run my code if you want to waste 1 minute hehe.. but i need somethng like this:
Code:
```               OOO
O
OOOOOOO
OOOOO
OOO OOO OOO
O   O   O
OOOOOOOOOOOOOOO
OOOOOOOOOOOOO
OOO OOOOOOOOOOO OOO
O   OOOOOOOOO   O
OOOOOOO OOOOOOO OOOOOOO
OOOOO   OOOOO   OOOOO
OOO OOO OOO OOO OOO OOO OOO
O   O   O   O   O   O   O```
you will notice the difference, i'm not trying to replace every O with a triangle of O since it cant be done and doesnt seem to be a good solution, i need to find a better way to do the job

7. I still can't find a solution to do this..it's been several days since i've been working on it.. any ideas? thanks..

8. What you really need is a huge matrix, that has your initial pattern. Then:
Code:
```for each row
for each column
if o