Triangle pattern

This is a discussion on Triangle pattern within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by dynamethod when i set y in my case to 0, i seem to have a gap at ...

  1. #31
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Quote Originally Posted by dynamethod View Post
    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
    Whoa!!! Actually i didn't think about the constructing a string.
    The result you're getting is normal. Setting y=0 means you're creating a first string with only spaces and the second one is empty.
    The string constructor builds up a string with n specified characters. In your case the first string si a 5-y length string of ' ', second one is a 0+y length string of 64+y characters.

    To remove the white space you can create the strings this way:

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	for(int y = 0; y < 4; y++) 
    	{	
    		cout << string((3-y),' ');
    		cout << string((1+y), 65+y);
    		cout << "\n";
    	}
    	
    	cin.get();
    	return 0;
    }
    Note that I left the 4 since it's the number of characters you want to print, so if you wanto to parameterize the function... (and notice that the 3 in the first string constructor is 4-1 )

    Regards,
    Tesctassa

  2. #32
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Masses of magic numbers there!

    At least replace 65 by 'A'.
    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. #33
    SRS
    SRS is offline
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    :)

    Wow that is nice to know...
    I didn't know such a thing was possible:

    Code:
    cout << string((1+y), 65+y);
    This looks like less work on the person's part when writing it, but how would it compare as far as performance to method A listed above?
    Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2

  4. #34
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It really depends on the std::string implementation. In a typical, modern implementation (small buffer optimization, good inlining by the compiler) I'd say it compares favourably, mostly because iostreams are dog slow, and the less you call into them, the better.
    If the string implementation does heap allocation even for 4 characters, the performance balance might tip into the individual character output direction. You'd have to measure 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

  5. #35
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Second one is trickier, you can do it with 5 abstractly nested "for a while" loops but that has rarely been tried. Your safest strategy is to do it without thinking.

Page 3 of 3 FirstFirst 123
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. 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-13-2002, 11:29 PM
  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