Thread: Triangle pattern

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    Triangle pattern

    Hi there, im trying to write some code to produce

    A
    BB
    CCC
    DDDD
    EEEEE
    FFFFFFF

    exactly like the above, however im totally stuck, i dont know how to go about this, would i need to somehow use a for loop? or an array? pointers much appreciated!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    would i need to somehow use a for loop? or an array?
    A loop and an array (or null terminated C-style string) would work, though you probably would be able to rely on ASCII values to eliminate the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No need to use an array. A loop (or two) should be fine.

    First write code to write out this (one 'x' at a time):
    xxxxxx

    Then add code to that to write out this:
    xxxxxx
    xxxxxx
    xxxxxx
    xxxxxx
    xxxxxx
    xxxxxx

    Then make a small change so that it writes out this:
    x
    xx
    xxx
    xxxx
    xxxxx
    xxxxxx

    And finally change the code one last time to write out like you're suppose to:
    A
    BB
    CCC
    DDDD
    EEEEE
    FFFFFFF

    At each step, compile and test and get it working before moving on.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    ive tried this to start with:

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	char cLetter = 'x';
    	int x;
    	
    	for(x = 0; x <= 5; x++) {
    		cout << cLetter;
    		cin.get();
    	}
    	
    	return 0;
    	
    }
    but the problem is its not outputting the 'x' on the same line:

    Code:
    x
    x
    x
    x
    x
    x
    all the x are on seperate lines, how do i keep them on the same line?
    Last edited by dynamethod; 11-08-2007 at 12:00 PM.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    char* is a pointer to an address where a character or string of characters are located. That's the only way you can declare a string of chars in c-style.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    all the x are on seperate lines, how do i keep them on the same line?
    Remove the cin.get();
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can put the cin.get() outside the loop just above the return 0; if you want to use it to keep the console window open so you can see your results.

    Otherwise, that looks like a good start.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Ok, ive got it sorted a bit better now i think

    however i think im going the wrong way about this because Daved said that a loop or two should do the trick but the way im going i will need a few 'for loops' to accomplish this :S

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	char cLetter = 'x';
    	int x;
    	
    	for(x = 0; x <= 5; x++) {
    		cout << cLetter;
    	}
    	
    	cout << "\n";
    	
    		for(x = 0; x <= 5; x++) {
    			cout << cLetter;
    	}
    	
    	cin.get();
    	
    	return 0;
    	
    }
    This outputs:

    Code:
    xxxxxx
    xxxxxx
    but thats just for two lines and ive already used two loops :S

    how do i go about using the loop so that i dont have keep using it over and over?

    or am i on the right track here?

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use nested loops:
    Code:
    for(...)
    {
        for(...)
        {
            ...
        }
    }
    Now just figure out the ... parts. And you can also do all of this with a single loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    You need two nested loops. Something like:

    Code:
    char Letter;
    int i,j;
    
    for(i=0; i<26; i++)
    {
         for(j=0; j<=i; j++)
         {
               Letter = 65+i;  //Here convert to char the ASCII decimal code 65+i, which stands for 'A' when i=0, 'B' when i=1, etc. ;)
               cout << Letter;
         }
         cout << "\n";
    }
    Try this out and let me know
    Regards,
    Tesctassa

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Awesome~!

    ok getting somewhere, heres my code now:

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	char cLetter = 'x';
    	int x;
    	int y;
    	
    	for(y = 0; y <= 4; y++) 
    	{
    		for(x = 0; x <= 4; x++) 
    		{
    		cout << cLetter;
    		}
    		cout << "\n";
    	}
    		
    	cin.get();
    	
    	return 0;
    	
    }
    I think im on the right track now, thanks for everyones input! you guys are VERY helpful :-)
    if this doesnt look right please say so though lol, thanks again!

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Awwwww!!! Tesctassa!!!! you just gave me the answer!! your code works perfectly, now i cant try and solve it :S

    Thats very interesting about the ASCII, it will come in handy! but that was tooo much info :S

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Wow, that's the first time I see a questioner complain about a violation of the "do not give full code" rule.


    Tesctassa, in this forum we don't want people to do this. It is generally an impediment to learning, since the posters no longer need to solve their own problems. As you can see, some smart ones even realize that themselves.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Quote Originally Posted by dynamethod View Post
    Awwwww!!! Tesctassa!!!! you just gave me the answer!! your code works perfectly, now i cant try and solve it :S

    Thats very interesting about the ASCII, it will come in handy! but that was tooo much info :S
    Quote Originally Posted by CornedBee View Post
    Wow, that's the first time I see a questioner complain about a violation of the "do not give full code" rule.


    Tesctassa, in this forum we don't want people to do this. It is generally an impediment to learning, since the posters no longer need to solve their own problems. As you can see, some smart ones even realize that themselves.

    Uuuuuups! ^______^'

    As you may see from my registration date I'm quite new on this forum, and I admit I didn't read the Forum Rules.... I beg pardon!!!

    dynamethod pleeeeeeease excuse me!!! Anyway, now try printing on screen this:

    Code:
        A
       BB
      CCC
     DDDD
    ...........
    :P

    Regards,
    Tesctassa



    ps: again, please excuse me!

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    dynamethod pleeeeeeease excuse me!!! Anyway, now try printing on screen this:

    Code:
        A
       BB
      CCC
     DDDD
    ...........
    :P

    Regards,
    Tesctassa



    ps: again, please excuse me!

    wow, this is quite tricky :S

    im not sure how to go about this tbh, hints much appreciated!

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. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  3. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  4. Triangle Pattern
    By Unregistered in forum C Programming
    Replies: 16
    Last Post: 02-14-2002, 12:29 AM
  5. Triangle pattern?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2001, 05:32 AM