How do I print a pattern flush right?

This is a discussion on How do I print a pattern flush right? within the C Programming forums, part of the General Programming Boards category; Can anyone tell me how to program a pattern to print flush right using only nested for statements. I have ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    8

    Question How do I print a pattern flush right?

    Can anyone tell me how to program a pattern to print flush right using only nested for statements. I have been successful getting the star pattern to print flush left, but what do I need to add to have the same pattern print flush right? Here is what the patterns look like flush left.

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    Flush left
    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    Last edited by Basia; 06-11-2002 at 09:07 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Use a variable to determine how many spaces to print and a variable to determine how many fill characters to print. You can do this with three loops or a trick with printf. Assuming you use the loops, the outer loop will contain two loops that print a series of spaces and fill characters respectively. The outer loop controls how many spaces and fills to print, with each iteration the spaces decrements and the fills increments:
    Code:
    Outer
      Print Space
        print a space
      Loop while more spaces
      Print Fill
        print a fill character
      Loop while more fills
      increment fill
      decrement space
    Loop while more spaces
    ---------
    space = 4
    fill = 1
        *
    space = 3
    fill = 2
       **
    space = 2
    fill = 3
      ***
    space = 1
    fill = 4
     ****
    space = 0
    fill = 5
    *****
    
    End result:
        *
       **
      ***
     ****
    *****
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16

    Smile

    Hello Basia. Was your attempt at the code something like my effort?
    With a variety of expressions in the for loops, I came up with this:
    Code:
    #include<stdio.h>
    
    #define MAX 10 /*The maximum number of chars in a line*/
    
    int main(void)
    {
     int i, j;
    
     for(i=0; i<MAX; i++)
     {
      for(j=i+1; j>0; j--)
    	printf("%c",'*');
    	printf("%c",'\n');
      }
    
     printf("%c",'\n');
    
     for(i=MAX; i>0; i--)
     {
      for(j=i; j>0; j--)
    	printf("%c",'*');
    	printf("%c",'\n');
      }
    
     printf("%c",'\n');
    
     for(i=0; i<MAX; i++)
     {
      for(j=MAX-i-1; j>0; j--)
    	printf("%c",' ');
      for(j=i+1; j>0; j--)
    	printf("%c",'*');
    	printf("%c",'\n');
      }
    
     printf("%c",'\n');
    
     for(i=0; i<MAX; i++)
     {
      for(j=i; j>0; j--)
    	printf("%c",' ');
      for(j=MAX-i; j>0; j--)
    	printf("%c",'*');
    	printf("%c",'\n');
      }
    
    return 0;
    }
    This will produce the four possible patterns.
    And good evening to Prelude... (It's 1am in the UK...)
    Cheers from Badger!

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    14

    here's the answer

    yeah to print what you want just send all the characters to printer buffer using 'stdprn'


    you would have used printf() to print somany "*" on the screen.

    now to print them out using a printer.

    just convert every printf() that prints " * " on the screen with follwing statement.

    printf(" * "); -----> fprintf(stdprn," * ");

    you're done,

    you won't see the out put on the screen

    to see the output on the screen keep both the lines!

    okaY.....


    bye
    Ruchit,
    think big,think ahead,
    go big,go ahead,

    ISN'T IT TOO FUNNY ABOUT MICROSOFT WINDOWS,THAT
    WHENEVER YOU WANT TO DO SOMETHING YOU CLICK ON "START" BUTTON AND THE FIRST OPTION YOU ARE HAVING IS
    "SHUT DOWN"...

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    14

    Thumbs up HERE YOU ARE!

    sorry i thought that you want them to be printed on a paper by a printer!
    here you are again!


    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    int i,j,k;
    int max=9;

    clrscr();

    for(i=1;i<=max;i++)
    {
    for(j=1;j<=i;j++)
    {
    printf("*");
    }
    printf("\n");
    }

    printf("\n");

    for(i=max;i>=0;i--)
    {
    for(j=1;j<=i;j++)
    {
    printf("*");
    }
    printf("\n");
    }

    getch();
    }
    Ruchit,
    think big,think ahead,
    go big,go ahead,

    ISN'T IT TOO FUNNY ABOUT MICROSOFT WINDOWS,THAT
    WHENEVER YOU WANT TO DO SOMETHING YOU CLICK ON "START" BUTTON AND THE FIRST OPTION YOU ARE HAVING IS
    "SHUT DOWN"...

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    And here another version using the memset function:
    Code:
    #include <stdio.h> 
    #include <string.h>
    
    #define WIDTH 10
    
    void print_line(int i)
    {
    	char str[WIDTH+1];
    	memset(str, ' ', WIDTH - i);  /* place WIDTH - i spaces in buffer */
    	memset(str + WIDTH - i, '*', i);  /* add i stars to buffer */
    	str[WIDTH] = 0;  /* make string null terminated */
    	printf("%s\n", str);
    }
    
    int main() 
    { 
    	int i;
    	for(i = 1; i < WIDTH; print_line(i++)) ;
    	for(i = WIDTH-1; i > 0; print_line(i--)) ;
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 06:07 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 09:13 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21