Thread: Inverse printing.

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Inverse printing.

    I'm sure the answer will be really easy, however i need to have the output of this inversed so i get
    F
    FE
    FED
    FEDC
    FEDCB
    FEDCBA
    here is my code, please let me know what is wrong with it.
    Code:
       /* alpha dependant nested loops.c */
    
    
    #include <stdio.h>
    #define ROW 6
    
    
    int main(void)
    
    
    {
        int row;
        char ltr;
    
        for (row = 0; row < ROW; row++){
            for (ltr = ('F' - row); ltr <('A' + ROW); ltr++)
                printf("%c", ltr );
             printf("\n");
    
             }
    
    
         getchar();
    
         return (0);
    
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Well, my preference would be to work with a string so that I wouldn't have to assume the ASCII character set:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char *p = "FEDCBA";
      int i, j;
    
      for ( i = 0; p[i] != '\0'; i++ ) {
        for ( j = 0; j <= i; j++ )
          printf ( "%c", p[j] );
        printf ( "\n" );
      }
    
      return 0;
    }
    But, with the right assumptions in hand, your way works too:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char i, j;
    
      for ( i = 'F'; i >= 'A'; i-- ) {
        for ( j = 'F'; j >= i; j-- )
          printf ( "%c", j );
        printf ( "\n" );
      }
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Interesting, you used too char loops since its the same count as integers anyaway. Very interesting. I never thought about doing that. Thank you very much prelude.

    Well, my preference would be to work with a string so that I wouldn't have to assume the ASCII character set:
    I haven't delved into pointers and strings yet, so i'm still a little behind. However i do understand your code with the char pointer? in it. I will be studying that code.



  4. #4
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    The 'why' it isn't working can be found using a flowchart and your knowledge of standard 'C'.
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Printing
    By Dual-Catfish in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-25-2002, 08:10 PM