Thread: Triangle pattern

  1. #16
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    Hints:

    You will need another for loop nested prior to the for loop printing the letters, and a little bit of math to figure how many spaces determined by which letter you are printing...
    Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    REALLY simple math at that. Every row is the same width x, of which y spaces are covered with letters. How many spaces?
    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

  3. #18
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Quote Originally Posted by SRS View Post
    You will need another for loop nested prior to the for loop printing the letters, and a little bit of math to figure how many spaces determined by which letter you are printing...
    Nope! No need of a third loop. Just two as before plus one more (constant ) variable and some math.


  4. #19
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    ahh, yes

    Quote Originally Posted by Tesctassa View Post
    Nope! No need of a third loop. Just two as before plus one more (constant ) variable and some math.

    dynamethod :: WARNING: do not read! may be more than a hint!
    I don't want to ruin your post or education, but I am more curious than allowing this to slip by...

    I didn't consider that possibility, but i know how... Now I want to know which way is the better way to go?
    A:
    Code:
    for()  //  cycle lines
    {
        for() {}  //  print leading spaces
        for() {}  //  print letters
    }
    B:
    Code:
    for()  //  cycle lines
    {
        for()  //  cycle prints per line
        {
            if() {}  //  print leading spaces
            else {}  //  print letters
        }
    }
    Is running a loop not better than comparing each cycle?
    Is there another way better than both of these?
    Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In the first line, you printed out 3 spaces, and then 1 letter. Second line, 2 spaces, and two letters. Third line, 1 space, and 3 letters. Last line, 0 spaces and 4 letters.

    Every line adds up to 4 char's, and the spaces are always printed (in this configuration), FIRST. Could you use a loop for the spaces, and then a nested loop inside that, for the printing of the letter?

    This can also be done in one loop, but the nesting of the loops is more educational.
    Last edited by Adak; 11-10-2007 at 01:54 AM.

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Adak: what? There's no third nesting of loops. That would be incredibly hard to get right.

    SRS: Method A is better. So yes, there's a third loop. There's a way of getting rid of both inner loops if you know the interface of std::string well enough, though.
    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

  7. #22
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	int x;
    	int y;
    	char cLetter;
    	
    	for(y = 1; y <= 4; y++) 
    	{	
    	cout << string(abs(y-5),' ');
    		
    		for(x=1; x<=y; x++)
    		{
    		cLetter=64+y; cout << cLetter;
    		}
    		
    	cout << "\n";
    	}
    	cin.get();
    	return 0;
    }
    i feel rotten about this though because ive borrowed the code from another post, which i found here:
    Code:
    http://cboard.cprogramming.com/showpost.php?p=673514&postcount=3
    after i done a search, i dont quite understand the string function fully, particularly the 'abs', but it works, still probably shouldnt have done this lol. im now doing a bit of research into the string funcion now though

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The abs(y - 5) is just stupid. Write 5 - y and it works the same, not to mention that it's easier to understand.

    Also, the loops are weird (0 to n-1 is more idiomatic in C++ than 1 to n), and the code makes a greater assumption about character values than usual. (Most code assumes that characters are sequential. This code assumes that they have ASCII codes.)

    Then there's the redundant computation of cLetter in the inner loop instead of outside the loop, where it belongs, and the string trick could be used there, too, so why isn't it?
    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

  9. #24
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    if i try the same code without the 'abs':

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	int x;
    	int y;
    	char cLetter;
    	
    	for(y = 1; y <= 4; y++) 
    	{	
    	cout << string(y-5,' ');
    		
    		for(x=1; x<=y; x++)
    		{
    		cLetter=64+y; cout << cLetter;
    		
    		}
    	cout << "\n";
    	}
    	
    	cin.get();
    	return 0;
    }
    the code compiles, but when i try execute the app i get:

    Code:
    terminate called after throwing an instance of 'std::length_error'
      what():  basic_string::_S_create
    Aborted (core dumped)

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Write 5 - y
    Look closely.
    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

  11. #26
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    for(y = 1; y <= 4; y++) 
    	{	
    	cout << string(y-5,' ');
    y-5 is going to end up being a negative value.

    And it would be a good thing to get into the habit of representing ranges as [0, n) - from 0 inclusive to n exclusive. This is the way the standard libraries do it and it can reduce adding and substracting a magic 1 in many places. The main reason is probably that it matches how arrays are indexed in C++.

    So a loop that would loop 4 times would be:
    Code:
    for (int i = 0; i < 4; ++i)
    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).

  12. #27
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by CornedBee View Post
    Look closely.
    Sorry :S, i should pay more attention

    heres what i have now:

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	int y;
    	char cLetter;
    	cLetter = 64;
    	
    	for(y = 1; y <= 4; y++) 
    	{	
    		cout << string((5-y),' ');
    		cout << string((0+y), cLetter+y);
    		cout << "\n";
    	}
    	
    	cin.get();
    	return 0;
    }
    i still think im not using the string function properly, string function is new territory for me, ive still alot to learn :S

  13. #28
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by anon View Post
    Code:
    for (int i = 0; i < 4; ++i)
    when i set y in my case to 0, i seem to have a gap at the top line of the output like this:

    Code:
        
       A
      BB
     CCC
    DDDD
    where as i set y to 1, i get:

    Code:
       A
      BB
     CCC
    DDDD

  14. #29
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's not a function, it's a class. You're constructing a temporary object.
    Last edited by CornedBee; 11-10-2007 at 07:33 AM.
    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

  15. #30
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    ok, just edited my code a bit more, i feel even more stupid for that last piece of code i posted now lol

    ive done without the char cLetter -

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	for(int y = 0; y <= 4; y++) 
    	{	
    		cout << string((4-y),' ');
    		cout << string((0+y), 64+y);
    		cout << "\n";
    	}
    	
    	cin.get();
    	return 0;
    }

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