Diamond loopy

This is a discussion on Diamond loopy within the C++ Programming forums, part of the General Programming Boards category; Hey guys iv made a program that makes diamond shape appear. It works fine and the image is perfect. But ...

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

    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
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,788
    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, 08:05 PM
  4. A solid shape diamond
    By viryonia in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2003, 09:22 PM
  5. Diamond
    By C++Nerd in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2002, 04:36 AM

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