printing a triangle

This is a discussion on printing a triangle within the C Programming forums, part of the General Programming Boards category; i have this problem : take a number from the user, and print a triangle with that number of rows. ...

  1. #1
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246

    printing a triangle

    i have this problem : take a number from the user, and print a triangle with that number of rows. everything is clear except for the loop that does the actual printing, i just can't come up with anything.

    number: 3

    ....*
    ..***
    *****

    (i used dots instead of spaces in my post)

  2. #2
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Post code please...

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Asterisk triangles are boring. Give your teacher something with a little more pizzaz!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      char buff[BUFSIZ];
      int height;
      int spaces;
      int i, j;
    
      printf ( "Enter the height (32 is a good one): " );
      fflush ( stdout );
      if ( fgets ( buff, sizeof buff, stdin ) == NULL ) {
        fprintf ( stderr, "Error reading input\n" );
        return EXIT_FAILURE;
      }
      if ( sscanf ( buff, "%d", &height ) != 1 ) {
        fprintf ( stderr, "Invalid input detected\n" );
        return EXIT_FAILURE;
      }
      spaces = height / 2;
      for ( i = 0; i < height; i += 2, spaces-- ) {
        for ( j = 0; j < spaces; j++ )
          putchar ( ' ' );
        for ( j = 0; j <= i; j++ )
          putchar ( ( ~i & j ) ? ' ' : '*' );
        putchar ( '\n' );
      }
    
      return EXIT_SUCCESS;
    }
    My best code is written with the delete key.

  4. #4
    Deleted Account
    Join Date
    Jan 2004
    Posts
    40
    Code:
    (defun pramt(c a) 
    	(unless (= a 0) 
    	(princ c) 
    	(pramt c (- a 1))))
    
    (defun lines(amt max) 
    	(unless (= amt (+ max 1)) (pramt " " (- max amt)) 
    	(pramt "*" (+ 1 (* (- amt 1) 2))) 
    	(terpri) 
    	(lines (+ amt 1) max)))
    My code owns yours, prelude. Eat lisp!

    Code:
    usage: (lines 1 5)
    output:
         *
        ***
       *****
      *******
     *********
    Last edited by Brian2; 01-20-2004 at 04:31 PM.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    If you do want asterisks:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int Rows = 0;
    	int Row_Width = 1; 
    
    	printf("%s", "Enter Number of Rows\n");
    	scanf("%d", &Rows);
    	
    	int Num_Rows = Rows-1;
    
    	for(int i = 0; i < Rows; i++)
    	{       
    		for(int k = Num_Rows; k > 0; k--)
    			{
    				printf("%s", " ");
    			}
    			Num_Rows --;
    
    		for(int j = 0; j < Row_Width; j++)
    		{
    			printf("%s", "*");			
    		}
    
    		printf("\n");	
    		Row_Width += 2;
    				
    	}
    
    	return EXIT_SUCCESS;
    }
    Piece of crap compared to the one above.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >My code owns yours, prelude. Eat lisp!
    Did you run my code? Anyway, what are you doing posting lisp on a C board?
    My best code is written with the delete key.

  7. #7
    Deleted Account
    Join Date
    Jan 2004
    Posts
    40
    > Did you run my code?

    Woah, that is cool.

    > Anyway, what are you doing posting lisp on a C board?

    I'm trying to learn it.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by Prelude
    Did you run my code?
    Prelude, you are a Goddess! And very weird...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Once again predlude showes us how ungodly good she is

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Brian2
    Code:
    (defun pramt(c a) 
    	(unless (= a 0) 
    	(princ c) 
    	(pramt c (- a 1))))
    
    (defun lines(amt max) 
    	(unless (= amt (+ max 1)) (pramt " " (- max amt)) 
    	(pramt "*" (+ 1 (* (- amt 1) 2))) 
    	(terpri) 
    	(lines (+ amt 1) max)))
    In C it would look like this:

    Code:
    void lines(int rows) {
        for(i=0; i<rows; i++) {
            for(j=0; j<rows-i-1; j++) printf(" ");
            for(k=0; k<i*2+1; k++) printf("*");
            printf("\n");
        }
    }
    I think the C solution is shorter and better readable.

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Prelude

    Code:
          putchar ( ( ~i & j ) ? ' ' : '*' );
    Nice. Can you explain how it works?

  12. #12
    Deleted Account
    Join Date
    Jan 2004
    Posts
    40
    Code:
    (defun stars (amt)
    	(dotimes (cur amt) (dotimes (num (- amt cur)) (princ " "))
    	(dotimes (num (1+ (* (1- cur) 2))) (princ "*")) (terpri)))
    HA! (oh and you didnt declare some of your variables). Also, code is not supposed to be readable. That's why they call it "code"!
    Last edited by Brian2; 01-21-2004 at 09:00 AM.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Nice. Can you explain how it works?
    Sure, but that would take all the fun out of figuring it out for yourself. But I'll give you a big hint. For each iteration of the outer loop, print the amount of spaces and then print i + 1 of i's binary digits. Like so:
    Code:
    void printbin ( unsigned bin )
    {
      int i;
    
      for ( i = 0; i <= bin; i++ )
        printf ( "%d", !!( bin & i ) );
      printf ( "\n" );
    }
    
    ...
    
    spaces = height / 2;
    for ( i = 0; i < height; i += 2, spaces-- ) {
      for ( j = 0; j < spaces; j++ )
        putchar ( ' ' );
      printbin ( i );
    }
    Be sure to notice the pattern from the bottom up, then try complimenting bin inside of the loop to rotate the pattern 180 degrees:
    Code:
    printf ( "%d", !!( ~bin & i ) );
    My best code is written with the delete key.

  14. #14
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    thank you all for the replies and your solutions. Prelude's code is great, although i don't understand the last part. Ohh well , i'll just read it a few more times...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 02:31 PM
  2. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 02:13 AM
  3. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  4. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 06:40 PM
  5. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 01:02 AM

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