Thread: printing new characters in loop

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    6

    printing new characters in loop

    hi

    i am currently writing a program that uses a function and loops, then prints out the capitol building along with the number of characters in the building like so.
    T
    |
    TTT

    value: 5

    The issue i am having is, i am able to print the building with just the letter "T" but i am having trouble printing it with "|" also.

    i know i can use an "if" statement, add a new line to print the "|" in between the letters but i am trying to use "T" for the base. if i use an "if" statement it always prints "|" as the final line and the base.

    i know its probably something really simple, however i just cant seem to understand where i am going wrong. hopefully someone here can point me in the right direction.

    here is my code:

    Code:
    #include <stdio.h>
    
    
    int drawImages(int, char);
    
    
    
    
    
    
    int main( ) {
       int rv;
       rv = drawImages(2, 'p');
       printf("%d\n", rv);
    
    
       return 0;
    }
    
    
    int drawImages(int rows, char type) {
       int i, j, k, letter, spaces, rv = 0;
    
    
       letter = 1;          
       spaces = rows-1;
    
    
       if(type == 'p') {
          if(rows >=1) {   
             for(i=0; i<rows; i++) {         
                
                for(j=0; j < spaces; j++) {  
                   printf(" ");
                }
                for(k=0; k < letter; k++) {
                
                    
                    printf("T");
                
                   rv++;
                }
                
                if (rows % 1 == 0){
                    printf("\n");
                }
                
                for(j=0; j < spaces; j++) {  
                   printf(" ");
                }
                
                for(k=0; k < letter; k++) {
                    printf("|");
                
                   rv++;
               }
                
                if(i <= rows) {           
                   spaces--;        
                   letter = letter + 2;
                }
                
                printf("\n");
             }
          }
          else {
             printf("Sorry, cannot draw image...\n");
             rv = -1;
          }
       }
       return rv;
    }
    Last edited by acoustix; 04-02-2017 at 12:41 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read this -> A development process
    Delete lines 44 through 56 inclusive.
    Realise that line 58 is always true, so you may as well delete that conditional as well.

    You start with a basic program which achieves a sub-set of the requirements.

    The point is, you start to break the problem down into manageable steps.
    Start by listing a series of intermediate steps, then write (and subsequently modify) the program to achieve those steps.

    Only trivial programs can be written, compiled and executed in a single sitting.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    6
    thanks for the advice, i removed those lines and decided i would try odd or even to print out my pattern.. it works.. kind of. however its still not working in my favor. i cant seem to get it to print like so.
    T
    |
    TTT
    |||
    TTTT
    ||||
    instead im getting

    T
    |||
    TTTTT
    |||||||



    Code:
    if(type == 'p') {
          if(rows >=1) {   
             for(i=0; i<rows; i++) {         
    
    
                for(j=0; j < spaces; j++) {  
                   printf(" ");
                }
                for(k=0; k < letter; k++) {
    
    
                     if(i%2==0)
                                     
                   printf("T");
                   
                   else if(i%1==0) 
                   printf("|");
                   
                   rv++;
                 
                   
                }
    
    
                spaces--;        
                letter = letter + 2;
    
    
                printf("\n");
             }
          }
          else {
             printf("Sorry, cannot draw image...\n");
             rv = -1;
          }
        }
        return rv;
    }
    Last edited by acoustix; 04-03-2017 at 01:54 AM.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Posts
    6
    still need assistance with this program, could anyone help me?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What are the next rows in this sequence?
    T
    |
    TTT
    |||
    TTTT
    ||||
    You have 1, 3, 4 characters per row (of 'T' followed by '|').

    I guess if you're trying to "prints out the capitol building", you want something vaguely like a tower, and not say a pyramid with an ever increasing base width.

    Perhaps post more examples (or the actual example) of what you're expected to print. It's hard to see the end goal from your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2017
    Posts
    6
    thanks for response..

    The purpose of my code is to print the capitol building, increase each tier by two each time i.e 1,3,5,7,9.
    For each tier i would like to have two lines i.e

    Code:
                 
    
    Tier 1 = T | Tier 2 = TTT ||| Tier 3 = TTTTT |||||
    value: 18
    here is my code so far. the only thing stopping me is the way i am printing the tiers. i am trying to achieve a total of 6.

    Code:
    
    #include <stdio.h>
    
    
    int drawImages(int, char);
    
    
    int main( ) {
       int rv;
       rv = drawImages(6, 'c');
       printf("%d\n", rv);
    
    
       return 0;
    }
    
    
    int drawImages(int rows, char type) {
       int i, j, k, letter, spaces, rv = 0;
    
    
       letter = 1;          
       spaces = rows-1;
     
    
    
        if(type == 'c') {
          if(rows >=1) {   
             for(i=0; i<rows; i++) {         
     
     
                for(j=0; j < spaces; j++) {  
                   printf(" ");
                }
                for(k=0; k < letter; k++) {
     
     
                    if(i%2==0){
                    
                    printf("T");
                    
                    rv++;
                    
                }
                                                   
                    else{
                    
                    printf("|");
                    
                    rv++;
                    
                }
                      
            }
    
    
                spaces--;        
                letter = letter + 2;
     
                printf("\n");
             }
             
          }
          
              else {
                printf("Sorry, cannot draw image...\n");
                rv = -1;
             
          }
          
        }
        
        return rv;
    }
    This code prints out
    Code:
    T ||| TTTTT ||||||| TTTTTTTTT ||||||||||| Value:36
    However, I am trying to achieve this..
    Code:
    T
    | TTT ||| TTTTT ||||| TTTTTTT ||||||| TTTTTTTTT ||||||||| TTTTTTTTTTT *** note here, it does not print the second line at the base*** Value:61
    Last edited by acoustix; 04-07-2017 at 12:40 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read post 2 again.

    Find small sub-tasks within the overall task.
    Code:
    #include <stdio.h>
    
    void drawRow(int centre, int rowlen, char ch) {
      for ( int i = 0 ; i < centre - rowlen / 2 ; i++ ) {
        printf(" ");
      }
      for ( int i = 0 ; i < rowlen ; i++ ) {
        printf("%c",ch);
      }
      printf("\n");
    }
    
    int main( ) {
      int centre = 10;
      drawRow(centre,1,'T');
      drawRow(centre,3,'T');
      drawRow(centre,5,'T');
      drawRow(centre,7,'T');
      return 0;
    }
    Then you might think about
    Code:
    for ( int row = 1 ; row < 11 ; row += 2 ) {
      printf("Generate a row of %d symbols\n", row );
    }
    Then you might think about
    - replacing 11 with the number the user wants
    - replacing centre=10 with a calculation based on the number the user wants.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Russian Characters
    By hafadozn in forum C Programming
    Replies: 12
    Last Post: 01-02-2013, 10:43 AM
  2. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  3. For Loop question for printing characters
    By trprince in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 09:01 PM
  4. Printing only certain characters
    By dmkanz07 in forum C Programming
    Replies: 9
    Last Post: 04-18-2007, 07:40 AM
  5. Printing different representations of characters...
    By smoothdogg00 in forum C Programming
    Replies: 3
    Last Post: 03-04-2006, 01:05 PM

Tags for this Thread