Triangle Pattern

This is a discussion on Triangle Pattern within the C Programming forums, part of the General Programming Boards category; I am trying to write a program that will look like a triangle. This is the pattern that I am ...

  1. #1
    Unregistered
    Guest

    Red face Triangle Pattern

    I am trying to write a program that will look like a triangle. This is the pattern that I am trying to accomplish.

    8$$$$$$$
    $8$$$$$$
    $$8$$$$$
    $$$8$$$$
    $$$$8$$$
    $$$$$8$$
    $$$$$$8$
    $$$$$$$8

    here is my code.#include <stdio.h>

    int main (void)
    {
    int row;
    int col;
    int limit;

    printf("\nPlease enter a number between 2 and 9: ");
    scanf("%d", &limit);


    for (row = 1; row<=limit; row++)
    {
    for (col = 1; col<=limit; col++)
    if (row >= col)
    printf("%d", col);
    else
    printf("$");
    printf("\n");
    }
    return 0;
    }

    I know that this isn't correct. can someone help me out

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Something like this?
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main( void )
    {
      int i, j, k;
      int spaceCount, maxSpace = 3;
    
      for ( i = 1; i <= 8; i *= 2 ) {
        spaceCount = maxSpace;
        while ( spaceCount > 0 ) {
          printf ( " " );
          spaceCount--;
        }
        for ( j = 1; j <= i; j *= 2 ) {
          printf ( "%d", j );
          if ( j == i && j > 1 ) {
            for ( k = j / 2; k >= 1; k /= 2 )
              printf ( "%d", k );
          }
        }
        printf ( "\n" );
        maxSpace--;
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    With a few minutes of work I put together a small bit of code which produces this effect....in the code I use a blank "space" character instead of the 8....but you can replace it with the 8....

    Code:
    #include <stdio.h>
    
    int main (void) 
    { 
    	int row,limit,col=1,x;
    
    	printf("\n\tEnter a Number Between 2 and 9:  "); 
    	scanf("%i", &limit);
    	printf("\n\t");
    
    	if ((limit<2) || (limit>9))
    	{
    		return 0;
    	}
    	
    	while (col<=limit)
    	{
    		for (x=0; x<limit; x++) 
    			{
    				printf("%c",36);
    			
    					if ((col-1)==x) 
    						{		
    							printf("\b%c",32);
    						}
    
    			}
    
    		col++;
    		printf("\n\t");
    	}
    
    
    	return 0;
    }

    %c, 36 Just Prints the $ Character
    %c, 32 Just Prints a Space Character


    It keeps a counter of what line it's on....everytime the line number equals the character number, it goes \b to go back one space, and prints a space instead...just run it. enjoi.

  4. #4
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    ....to prelude......your code didn't do what he wanted...i don't think? he made a weird triangle shape up top..run my code...i made it print the funny triangles....yours is the ACTUAL triangle...his code was printing 2 sideways triangles..i wrote my code to the way i thought he wanted it..<shrugs> maybe i was wrong.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    To be honest I didn't even read his post too carefully, usually when they say triangle program it means the one I posted. Teachers love that one.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    mmmmmmm....who knows......i like the one i created......although it makes 2 triangles and they are tilted...i think that was the pattern he was going for....guess he will respond next time he reads.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Here's the code that prints out exactly what you described
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int i, j, max;
      printf ( "Enter a number: " );
      scanf ( "%d", &max );
      for ( i = 0; i < max; ++i ) {
        for ( j = 0; j < max; ++j ) {
          if ( j == i )
            printf ( "%d", max );
          else
            printf ( "$" );
        }
        printf ( "\n" );
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest

    Here's the triangle pattern

    I am new to this language, so bear with me if its not the neatest.


    int main (void)
    {
    int row;
    int col;
    int limit;

    printf("\nPlease enter a number between 2 and 9: ");
    scanf("%d", &limit);


    for (row = 1; row<=limit; row++)
    {
    for (col = 1; col<=limit; col++)
    if (row > col || row < col)
    printf("$");
    else
    printf("%d", limit);
    printf("\n");
    }
    return 0;
    }

    I am truely amazed that I figured this one out. I am not very good at programming

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I am not very good at programming
    It comes gradually, we were all there at one point, so no worries

    BTW, if you want to neaten up your code by indenting it try using code tags. The syntax for them can be found here. Once indented your code doesn't look bad at all.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    mm...my program did that too...i thought he was asking for help on how to do it........not showing us that he did it...*shrugs*

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    Just curious on how you would do the opposite, such as


    $$$6
    $$6$
    $6$$
    6$$$

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Just reverse the loop values.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int i, j, max;
      printf ( "Enter a number: " );
      scanf ( "%d", &max );
      for ( i = 0; i <= max; ++i ) {
        for ( j = max; j >= 0; --j ) {
          if ( j == i )
            printf ( "%d", max );
          else
            printf ( "$" );
        }
        printf ( "\n" );
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    I'm trying to get this pattern

    $$$$
    $$$4
    $$4$
    $4$$

    Here is my code:

    It prints out this ($$$$$$$4$$44$444) which is totally wrong,
    instead of what it looks like above



    #include <stdio.h>

    int main()
    {
    int limit;
    int row;
    int col;
    int var;
    int dar;

    printf("\nPlease enter a number between 2 and 9: ");
    scanf("%d", &limit);


    printf("\n");

    for(row=1; row<=limit; row++)
    if(row==1){
    for(col=1; col<=limit; col++)
    printf("$");
    }
    else if (row > 1) {
    for(var = limit; var >= row; var--)
    printf("$");
    for(dar = 1;dar < row; dar++)
    printf("%d", limit);
    }
    else
    printf("$");
    printf("\n");
    return 0;
    }


    Any help would be great.

  14. #14
    Unregistered
    Guest
    Sniff Sniff S215???? YUPPPPPPPPPPPP

  15. #15
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    .....the code i wrote along time ago (which you never responded too) easily does this backwards too:

    Code:
    #include <stdio.h>
    
    int main (void) 
    { 
    	int limit,x,col=1;
    
    	printf("\n\tEnter a Number Between 2 and 9:  "); 
    	scanf("%i", &limit);
    	printf("\n\t");
    
    	while (col<=limit)
    	{
    		for (x=limit; x>0; x--) 
    		{
    			printf("$");
    			if ((col-1)==x) 
    				{		
    					printf("\b%d",limit);
    				}
    
    			}
    
    		col++;
    		printf("\n\t");
    	}
    
    
    	return 0;
    }
    I think you're trying too hard....anyways if you want to USE your code...one problem is you are missing an important "\n"....


    Code:
    else if (row > 1) { 
    	printf("\n");
    for(var = limit; var >= row; var--) 
    printf("$"); 
    for(dar = 1;dar < row; dar++) 
    printf("%d", limit); 
    }

    By throwing in that \n, the code will atleast chop the lines correctly...although JUST doing that, your code still messes up somewhat and doesn't print the right characters towards the bottom....your code looks confusing...take a look at mine, it uses much less confusion and variables.

Page 1 of 2 12 LastLast
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, 01:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-26-2008, 11:24 PM
  3. Triangle pattern
    By dynamethod in forum C++ Programming
    Replies: 34
    Last Post: 11-10-2007, 04:54 PM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  5. Triangle pattern?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2001, 05:32 AM

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