Thread: Difficult Triangles

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Difficult Triangles

    Hi, I'm fairly new to C and I'm making some triangles. The problem I have is that I need to make this:

    Code:
    *******************
      *****************
        ***************
          *************
            ***********
              *********
                *******
                  *****
                    ***
                      *
    Here is the code I've been playing around with:

    Code:
    		   for (row = 10; row >= 1; row--)
                {
                   for (spaces = 10; spaces <= row; spaces++)
                      printf(" ");
                   for (column = 1; column <= (row*2-1); column++)
                      printf("*");
                   printf("\n");
    	    }
    I've already been through loads and loads playing with different numbers etc but I can never get it to look like above. What am I supposed to do?


    While I'm here, for this triangle:
    Code:
    *******************
    *****************
    ***************
    *************
    ***********
    *********
    *******
    *****
    ***
    *
    I used this code:

    Code:
              for (row = 1; row <= 10; row++)
                {
                   for (column = 1; column <= (row*2-1); column++)
                      printf("*");
                   printf("\n");
    	    }
    Notice I missed out the spaces loop. Is that OK or should I include it (even though it does nothing).

    Thanks in advance

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    For the first case, think of it in terms of the rows. Make that outer loop. Inside, however, you have to consider that a line is made up of a number of spaces and a number of stars. Something of interest: the number of spaces plus the number of stars is going to be the same for each row.

    So to print a line you print a number of spaces and a number of stars. This is dependent upon what row you are on. You see to have this idea, but you have complicated your conditions and index numbers. Try to simplify it.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    Code:
    		for (row = 10; row >= 1; row--)
                {
                   for (spaces = 18; spaces >= (row*2-1); spaces--)
                      printf(" ");
                   for (column = 1; column <= (row*2-1); column++)
                      printf("*");
                   printf("\n");
    	    }
    That did it. Is that the best way? I had to count the number of spaces to get 18 which seems a bit odd.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Sometimes there isn't a "best" way. You have to define what you mean. In my own opinion, I think you've complicated the loop variables way too much.

    Here's my method:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i, j;
    	for(i=0;i<10;i++)
    	{
    		for(j=0;j<19;j++)
    		{
    			putchar(j < (i*2) ? ' ' : '*');
    		}
    		putchar('\n');
    	}
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-08-2009, 10:13 AM
  2. How difficult would this be - download and read file
    By spadez in forum C Programming
    Replies: 4
    Last Post: 04-12-2009, 02:05 PM
  3. Drawing triangles in C#
    By John_L in forum C# Programming
    Replies: 2
    Last Post: 03-15-2008, 08:48 AM
  4. Difficult time understanding generating terrain (from file)
    By indigo0086 in forum Game Programming
    Replies: 3
    Last Post: 06-07-2007, 11:36 PM
  5. 3D animation (difficult?)
    By maes in forum Game Programming
    Replies: 3
    Last Post: 09-08-2003, 10:44 PM