Thread: replace single character with matrix of char

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65

    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
    Last edited by rob90; 12-21-2009 at 08:06 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 12-21-2009 at 08:43 AM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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]
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    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
    Last edited by rob90; 12-21-2009 at 10:08 AM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by rob90 View Post
    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.
    Last edited by itCbitC; 12-21-2009 at 12:21 PM.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    Quote Originally Posted by itCbitC View Post
    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
    Last edited by rob90; 12-21-2009 at 01:07 PM.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    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. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What you really need is a huge matrix, that has your initial pattern. Then:
    Code:
    for each row
        for each column
            if o
                add coordinates to list
    
    for each element in the list
        set( row, col -1 )
        set( row, col +1 )
        set( row+w, col )
    Simplify. You were over complicating.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM