Thread: Diamond loopy

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Diamond loopy

    Hey guys iv made a program that makes diamond shape appear. It works fine
    and the image is perfect. But I was wondering, I have broken the code into two
    sections, the top and bottom half the shape. Is it possible to use a single loop
    of deep nested for loops to make the shape appear any different and more cleaner than I have done?

    All im looking for is pointers on my coding...

    Code:
    #include <iostream>
    
    // main function - begins program execution ////////////////////////////////////
    //
    int main ( void )
    {
       int spaces, stars;
       
       // top hslf of diamond
       for ( int line = 1; line <= 5; line++ )
       {
          spaces = abs ( line -5 );
          
          for ( int loop = 1; loop <= spaces; loop++ )
             std::cout << " ";
             stars = line + ( line -1 );
             
          for ( int loop = 1; loop <= stars; loop++ )
             std::cout << "*";
             std::cout << std::endl;
          
       }
       
       // bottom half of diamond
       for ( int line = 1 ; line <= 4; line++ )
       {
          spaces = line;
    
          for ( int loop = 1; loop <= spaces; loop++ )
             std::cout << " ";
             stars = 9 - 2 * spaces;
    
          for ( int loop = 1; loop <= stars; loop++ )
             std::cout << "*";
             std::cout << std::endl;
       }
    
       std::cin.get(); // freeze console output window
       
       return 0; // return value from int main
    }
    Double Helix STL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is a "cleaner" solution using standard I/O manipulators, namely to set the fill character and the field width. I am not sure how to combine the two parts into a single loop.
    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 hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        for( int i = 0; i < 9; ++i )
            std::cout << std::string(abs(4-i),' ') << std::string(9-2*abs(4-i),'*') << std::endl;
    
        return 0;
    }
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  3. making a diamond using functions
    By Nikisha in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2003, 09:05 PM
  4. A solid shape diamond
    By viryonia in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2003, 10:22 PM
  5. Diamond
    By C++Nerd in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2002, 04:36 AM